Skip to content

Commit

Permalink
fix: yarn v4 support for monorepos (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone authored Jul 5, 2024
1 parent 0ce8359 commit aff2d0f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Placeholder for the next version (at the beginning of the line):
## **WORK IN PROGRESS**
-->
## **WORK IN PROGRESS**
* `package` plugin: Support monorepos managed with Yarn v4

## 3.7.2 (2024-06-24)
* `iobroker` plugin: Fixed issue in changelog cleanup routine introduced in `3.7.1`

Expand Down
15 changes: 10 additions & 5 deletions packages/plugin-package/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ describe("Package plugin", () => {
".yarnrc.yml": fixtures.yarnrc_commented_out,
});

context.sys.mockExec((cmd) => {
if (cmd === "yarn --version") return "3.4.5";
return "";
});

await assertReleaseError(() => pkgPlugin.executeStage(context, DefaultStages.check), {
fatal: true,
messageMatches: /plugin import version/i,
Expand Down Expand Up @@ -240,11 +245,10 @@ describe("Package plugin", () => {
context.setData("version_new", newVersion);
context.setData("monorepo", "yarn");

context.sys.mockExec((cmd) =>
cmd.includes("changed list")
? "" // no changes!
: "",
);
context.sys.mockExec((cmd) => {
if (cmd === "yarn --version") return "3.4.5";
return "";
});

await assertReleaseError(() => pkgPlugin.executeStage(context, DefaultStages.check), {
fatal: true,
Expand Down Expand Up @@ -405,6 +409,7 @@ describe("Package plugin", () => {
"yarn",
"changed",
"foreach",
"--all",
`--git-range=v${pack.version}`,
"version",
newVersion,
Expand Down
32 changes: 29 additions & 3 deletions packages/plugin-package/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ async function getUpdatePackages(
return updatePackages;
}

async function getYarnVersion(context: Context): Promise<string> {
const { stdout: output } = await context.sys.exec("yarn", ["--version"], { cwd: context.cwd });
const version = output.trim();
if (!semver.valid(version)) {
context.cli.fatal(`Invalid yarn version "${version}"`);
}
return version;
}

class PackagePlugin implements Plugin {
public readonly id = "package";
public readonly stages = [DefaultStages.check, DefaultStages.edit, DefaultStages.commit];
Expand Down Expand Up @@ -100,6 +109,8 @@ class PackagePlugin implements Plugin {
// we need some yarn plugins to be able to handle this
const yarnRcPath = path.join(context.cwd, ".yarnrc.yml");
if (await fs.pathExists(yarnRcPath)) {
const yarnVersion = await getYarnVersion(context);

const yarnRc = await fs.readFile(yarnRcPath, "utf8");
const yarnPlugins = yarnRc
.split("\n")
Expand All @@ -114,11 +125,16 @@ class PackagePlugin implements Plugin {
);
// A list of required plugins and how to import them
const requiredPlugins: Record<string, string> = {
"workspace-tools": "workspace-tools",
version: "version",
changed:
"https://github.com/Dcard/yarn-plugins/releases/latest/download/plugin-changed.js",
};
if (semver.lt(yarnVersion, "4.0.0")) {
// Yarn v4 includes these plugins by default
Object.assign(requiredPlugins, {
"workspace-tools": "workspace-tools",
version: "version",
});
}
const missingPlugins = Object.keys(requiredPlugins).filter(
(plugin) => !yarnPlugins.includes(plugin),
);
Expand All @@ -137,6 +153,7 @@ Alternatively, you can use ${context.cli.colors.blue("lerna")} to manage the mon

// All good, remember that we use yarn to manage the monorepo
context.setData("monorepo", "yarn");
context.setData("yarn_version", yarnVersion);

// One last check: make sure there is anything to publish
// We cannot use getEffectivePublishAllFlag here without introducing a circular dependency
Expand Down Expand Up @@ -280,11 +297,20 @@ Alternatively, you can use ${context.cli.colors.blue("lerna")} to manage the mon
await deleteStableVersions();
const commands = [
publishAll
? ["yarn", "workspaces", "foreach", "version", newVersion, "--deferred"]
? [
"yarn",
"workspaces",
"foreach",
"--all",
"version",
newVersion,
"--deferred",
]
: [
"yarn",
"changed",
"foreach",
"--all",
`--git-range=v${pack.version}`,
"version",
newVersion,
Expand Down

0 comments on commit aff2d0f

Please sign in to comment.