Skip to content

Commit

Permalink
fix(version): Update lockfile version, if present
Browse files Browse the repository at this point in the history
Fixes #1998
Closes #2160
Refs #1415
  • Loading branch information
evocateur committed Oct 21, 2019
1 parent 840392f commit 5b1b40b
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 3 deletions.
3 changes: 3 additions & 0 deletions __fixtures__/lockfile-leaf/lerna.json
@@ -0,0 +1,3 @@
{
"version": "1.0.0"
}
5 changes: 5 additions & 0 deletions __fixtures__/lockfile-leaf/package.json
@@ -0,0 +1,5 @@
{
"name": "leaf-lockfile",
"version": "0.0.0-lerna",
"private": true
}
13 changes: 13 additions & 0 deletions __fixtures__/lockfile-leaf/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/packages/package-1/package.json
@@ -0,0 +1,7 @@
{
"name": "package-1",
"version": "1.0.0",
"dependencies": {
"tiny-tarball": "^1.0.0"
}
}
40 changes: 40 additions & 0 deletions commands/version/__tests__/update-lockfile-version.test.js
@@ -0,0 +1,40 @@
"use strict";

const path = require("path");
const fs = require("fs-extra");

// mocked or stubbed modules
const loadJsonFile = require("load-json-file");

// helpers
const { getPackages } = require("@lerna/project");
const initFixture = require("@lerna-test/init-fixture")(__dirname);

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

test("updateLockfileVersion", async () => {
const cwd = await initFixture("lockfile-leaf");
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"]);
expect(fs.readJSONSync(returnedLockfilePath)).toHaveProperty("version", "2.0.0");
});

test("updateLockfileVersion without sibling lockfile", async () => {
const cwd = await initFixture("lifecycle", false);
const [pkg] = await getPackages(cwd);

pkg.version = "1.1.0";

loadJsonFile.mockImplementationOnce(() => Promise.reject(new Error("file not found")));

const returnedLockfilePath = await updateLockfileVersion(pkg);

expect(returnedLockfilePath).toBeUndefined();
expect(fs.pathExistsSync(path.join(pkg.location, "package-lock.json"))).toBe(false);
});
10 changes: 10 additions & 0 deletions commands/version/__tests__/version-command.test.js
Expand Up @@ -724,4 +724,14 @@ describe("VersionCommand", () => {
expect(message).toBe("v1.0.1");
});
});

describe("with leaf lockfiles", () => {
it("updates lockfile version to new package version", async () => {
const cwd = await initFixture("lockfile-leaf");
await lernaVersion(cwd)("--yes", "major");

const changedFiles = await showCommit(cwd, "--name-only");
expect(changedFiles).toContain("packages/package-1/package-lock.json");
});
});
});
7 changes: 6 additions & 1 deletion commands/version/index.js
Expand Up @@ -32,6 +32,7 @@ const isBreakingChange = require("./lib/is-breaking-change");
const isAnythingCommitted = require("./lib/is-anything-committed");
const makePromptVersion = require("./lib/prompt-version");
const createRelease = require("./lib/create-release");
const { updateLockfileVersion } = require("./lib/update-lockfile-version");

const { collectPackages, getPackagesForOption } = collectUpdates;

Expand Down Expand Up @@ -511,10 +512,14 @@ class VersionCommand extends Command {
}
}

return pkg.serialize().then(() => {
return Promise.all([updateLockfileVersion(pkg), pkg.serialize()]).then(([lockfilePath]) => {
// commit the updated manifest
changedFiles.add(pkg.manifestLocation);

if (lockfilePath) {
changedFiles.add(lockfilePath);
}

return pkg;
});
},
Expand Down
27 changes: 27 additions & 0 deletions commands/version/lib/update-lockfile-version.js
@@ -0,0 +1,27 @@
"use strict";

const path = require("path");
const loadJsonFile = require("load-json-file");
const writeJsonFile = require("write-json-file");

module.exports.updateLockfileVersion = updateLockfileVersion;

function updateLockfileVersion(pkg) {
const lockfilePath = path.join(pkg.location, "package-lock.json");

let chain = Promise.resolve();

chain = chain.then(() => loadJsonFile(lockfilePath).catch(() => {}));
chain = chain.then(obj => {
if (obj) {
obj.version = pkg.version;

return writeJsonFile(lockfilePath, obj, {
detectIndent: true,
indent: 2,
}).then(() => lockfilePath);
}
});

return chain;
}
4 changes: 3 additions & 1 deletion commands/version/package.json
Expand Up @@ -49,6 +49,7 @@
"@lerna/validation-error": "file:../../core/validation-error",
"chalk": "^2.3.1",
"dedent": "^0.7.0",
"load-json-file": "^5.3.0",
"minimatch": "^3.0.4",
"npmlog": "^4.1.2",
"p-map": "^2.1.0",
Expand All @@ -57,6 +58,7 @@
"p-waterfall": "^1.0.0",
"semver": "^6.2.0",
"slash": "^2.0.0",
"temp-write": "^3.4.0"
"temp-write": "^3.4.0",
"write-json-file": "^3.2.0"
}
}
4 changes: 3 additions & 1 deletion package-lock.json

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

0 comments on commit 5b1b40b

Please sign in to comment.