Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(version): Update lockfile version, if present
- Loading branch information
Showing
10 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"version": "1.0.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "leaf-lockfile", | ||
"version": "0.0.0-lerna", | ||
"private": true | ||
} |
13 changes: 13 additions & 0 deletions
13
__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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "package-1", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"tiny-tarball": "^1.0.0" | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
commands/version/__tests__/update-lockfile-version.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.