Skip to content

Commit

Permalink
fix: properly update lockfile v2 (#3091)
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-abbott committed Jun 14, 2022
1 parent 4b81a98 commit 1e07a88
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions __fixtures__/lockfile-leaf-v2/lerna.json
@@ -0,0 +1,3 @@
{
"version": "1.0.0"
}
5 changes: 5 additions & 0 deletions __fixtures__/lockfile-leaf-v2/package.json
@@ -0,0 +1,5 @@
{
"name": "leaf-lockfile",
"version": "0.0.0-lerna",
"private": true
}
27 changes: 27 additions & 0 deletions __fixtures__/lockfile-leaf-v2/packages/package-1/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions __fixtures__/lockfile-leaf-v2/packages/package-1/package.json
@@ -0,0 +1,7 @@
{
"name": "package-1",
"version": "1.0.0",
"dependencies": {
"tiny-tarball": "^1.0.0"
}
}
17 changes: 16 additions & 1 deletion commands/version/__tests__/update-lockfile-version.test.js
Expand Up @@ -12,7 +12,7 @@ const initFixture = require("@lerna-test/init-fixture")(__dirname);

const { updateLockfileVersion } = require("../lib/update-lockfile-version");

test("updateLockfileVersion", async () => {
test("updateLockfileVersion with lockfile v1", async () => {
const cwd = await initFixture("lockfile-leaf");
const [pkg] = await getPackages(cwd);

Expand All @@ -25,6 +25,21 @@ test("updateLockfileVersion", async () => {
expect(fs.readJSONSync(returnedLockfilePath)).toHaveProperty("version", "2.0.0");
});

test("updateLockfileVersion with lockfile v2", async () => {
const cwd = await initFixture("lockfile-leaf-v2");
const [pkg] = await getPackages(cwd);

pkg.version = "2.0.0";

const returnedLockfilePath = await updateLockfileVersion(pkg);

expect(returnedLockfilePath).toBe(path.join(pkg.location, "package-lock.json"));
expect(Array.from(loadJsonFile.registry.keys())).toStrictEqual(["/packages/package-1"]);
const updatedLockfile = fs.readJSONSync(returnedLockfilePath);
expect(updatedLockfile).toHaveProperty("version", "2.0.0");
expect(updatedLockfile).toHaveProperty(["packages", "", "version"], "2.0.0");
});

test("updateLockfileVersion without sibling lockfile", async () => {
const cwd = await initFixture("lifecycle", false);
const [pkg] = await getPackages(cwd);
Expand Down
4 changes: 4 additions & 0 deletions commands/version/lib/update-lockfile-version.js
Expand Up @@ -16,6 +16,10 @@ function updateLockfileVersion(pkg) {
if (obj) {
obj.version = pkg.version;

if (obj.packages && obj.packages[""]) {
obj.packages[""].version = pkg.version;
}

return writeJsonFile(lockfilePath, obj, {
detectIndent: true,
indent: 2,
Expand Down

0 comments on commit 1e07a88

Please sign in to comment.