From 293420ce36383e5bac4fc10700e00fd8ca41829b Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Mon, 1 May 2023 11:49:01 +1200 Subject: [PATCH] feat: support package-lock.json version 3 V3 lock files are backwards compatible with V2 lock files and also need the `""` package updated on release. Fixes #1939 --- __snapshots__/package-lock-json.js | 16 ++++++++++++++++ src/updaters/node/package-lock-json.ts | 2 +- test/updaters/fixtures/package-lock-v3.json | 12 ++++++++++++ test/updaters/package-lock-json.ts | 14 ++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/updaters/fixtures/package-lock-v3.json diff --git a/__snapshots__/package-lock-json.js b/__snapshots__/package-lock-json.js index 088161d09..fd128b0fe 100644 --- a/__snapshots__/package-lock-json.js +++ b/__snapshots__/package-lock-json.js @@ -23,3 +23,19 @@ exports['PackageLockJson updateContent v2 updates the package version 1'] = ` } ` + +exports['PackageLockJson updateContent v3 updates the package version 1'] = ` +{ + "name": "release-please", + "version": "14.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "release-please", + "version": "14.0.0" + } + } +} + +` diff --git a/src/updaters/node/package-lock-json.ts b/src/updaters/node/package-lock-json.ts index 5a1c7c188..26e809207 100644 --- a/src/updaters/node/package-lock-json.ts +++ b/src/updaters/node/package-lock-json.ts @@ -31,7 +31,7 @@ export class PackageLockJson extends DefaultUpdater { const parsed = JSON.parse(content) as LockFileV2; logger.info(`updating from ${parsed.version} to ${this.version}`); parsed.version = this.version.toString(); - if (parsed.lockfileVersion === 2) { + if (parsed.lockfileVersion === 2 || parsed.lockfileVersion === 3) { parsed.packages[''].version = this.version.toString(); } return jsonStringify(parsed, content); diff --git a/test/updaters/fixtures/package-lock-v3.json b/test/updaters/fixtures/package-lock-v3.json new file mode 100644 index 000000000..43f2ce1b4 --- /dev/null +++ b/test/updaters/fixtures/package-lock-v3.json @@ -0,0 +1,12 @@ +{ + "name": "release-please", + "version": "11.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "release-please", + "version": "11.1.0" + } + } +} diff --git a/test/updaters/package-lock-json.ts b/test/updaters/package-lock-json.ts index 1c5a4cc30..acd6dcc94 100644 --- a/test/updaters/package-lock-json.ts +++ b/test/updaters/package-lock-json.ts @@ -49,4 +49,18 @@ describe('PackageLockJson', () => { snapshot(newContent.replace(/\r\n/g, '\n')); }); }); + + describe('updateContent v3', () => { + it('updates the package version', async () => { + const oldContent = readFileSync( + resolve(fixturesPath, './package-lock-v3.json'), + 'utf8' + ); + const packageJson = new PackageLockJson({ + version: Version.parse('14.0.0'), + }); + const newContent = packageJson.updateContent(oldContent); + snapshot(newContent.replace(/\r\n/g, '\n')); + }); + }); });