Skip to content

Commit

Permalink
fix(version): update yarn lock for versions of yarn >= 2.0.0 (#3555)
Browse files Browse the repository at this point in the history
  • Loading branch information
fahslaj committed Feb 24, 2023
1 parent fdbbab9 commit ce2ceca
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
103 changes: 103 additions & 0 deletions e2e/version/src/yarn-lockfiles.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Fixture, normalizeCommitSHAs, normalizeEnvironment } from "@lerna/e2e-utils";
import { readFileSync, writeFileSync } from "fs-extra";

expect.addSnapshotSerializer({
serialize(str: string) {
return normalizeCommitSHAs(normalizeEnvironment(str));
},
test(val: string) {
return val != null && typeof val === "string";
},
});

const setupYarnBerry = async (fixture: Fixture) => {
await fixture.exec("yarn set version berry");
await fixture.exec("yarn config set nodeLinker node-modules");
await fixture.exec("yarn config set npmRegistryServer http://localhost:4872");
await fixture.exec("yarn config set unsafeHttpWhitelist localhost");

await fixture.createInitialGitCommit();

await fixture.exec("yarn install", {
env: {
...process.env,
YARN_ENABLE_IMMUTABLE_INSTALLS: "false",
},
});
writeFileSync(
fixture.getWorkspacePath(".gitignore"),
`
node_modules
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
`
);

await fixture.overrideLernaConfig({
npmClient: "yarn",
});

await fixture.exec("git add .");
await fixture.exec("git commit -m 'chore: setup yarn berry'");
await fixture.exec("git push origin test-main");
};

describe("lerna-version-yarn-lockfiles", () => {
let fixture: Fixture;

beforeEach(async () => {
fixture = await Fixture.create({
e2eRoot: process.env.E2E_ROOT,
name: "lerna-version-yarn-lockfiles",
packageManager: "yarn",
initializeGit: true,
runLernaInit: false,
installDependencies: false,
});

await fixture.lernaInit("", { keepDefaultOptions: true });
await setupYarnBerry(fixture);

await fixture.lerna("create package-a -y");
await fixture.lerna("create package-b --dependencies package-a -y");

await fixture.exec("yarn install", {
env: {
...process.env,
YARN_ENABLE_IMMUTABLE_INSTALLS: "false",
},
});
await fixture.exec("git add .");
await fixture.exec("git commit -m 'chore: add packages'");
await fixture.exec("git push origin test-main");
});
afterEach(() => fixture.destroy());

it("should update yarn.lock", async () => {
const output = await fixture.lerna("version 3.3.3 -y");
expect(output.combinedOutput).toMatchInlineSnapshot(`
lerna notice cli v999.9.9-e2e.0
lerna info current version 0.0.0
lerna info Assuming all packages changed
Changes:
- package-a: 0.0.0 => 3.3.3
- package-b: 0.0.0 => 3.3.3
lerna info auto-confirmed
lerna info execute Skipping releases
lerna info git Pushing tags...
lerna success version finished
`);
const yarnLock = readFileSync(fixture.getWorkspacePath("yarn.lock")).toString();
expect(yarnLock).toContain("package-a@^3.3.3, package-a@workspace:packages/package-a");
expect(yarnLock).toContain("package-b@workspace:packages/package-b");
expect(yarnLock).toContain("package-a: ^3.3.3");
});
});
24 changes: 24 additions & 0 deletions libs/commands/version/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,30 @@ class VersionCommand extends Command {
});
}

if (this.options.npmClient === "yarn") {
chain = chain
.then(() => childProcess.execSync("yarn", ["--version"], this.execOpts))
.then((yarnVersion) => {
this.logger.verbose("version", `Detected yarn version ${yarnVersion}`);

if (semver.gte(yarnVersion, "2.0.0")) {
this.logger.verbose("version", "Updating root yarn.lock");
return childProcess
.exec("yarn", ["install", "--mode", "update-lockfile", ...npmClientArgs], {
...this.execOpts,
env: {
...process.env,
YARN_ENABLE_SCRIPTS: false,
},
})
.then(() => {
const lockfilePath = path.join(this.project.rootPath, "yarn.lock");
changedFiles.add(lockfilePath);
});
}
});
}

if (this.options.npmClient === "npm" || !this.options.npmClient) {
const lockfilePath = path.join(this.project.rootPath, "package-lock.json");
if (fs.existsSync(lockfilePath)) {
Expand Down

0 comments on commit ce2ceca

Please sign in to comment.