diff --git a/__snapshots__/package-lock-json.js b/__snapshots__/package-lock-json.js index fd128b0fe..90c5cdabe 100644 --- a/__snapshots__/package-lock-json.js +++ b/__snapshots__/package-lock-json.js @@ -24,6 +24,26 @@ exports['PackageLockJson updateContent v2 updates the package version 1'] = ` ` +exports['PackageLockJson updateContent v3 monorepo updates the package version 1'] = ` +{ + "name": "release-please", + "version": "14.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "release-please", + "version": "14.0.0" + }, + "packages/foo": { + "name": "release-please-foo", + "version": "2.0.0" + } + } +} + +` + exports['PackageLockJson updateContent v3 updates the package version 1'] = ` { "name": "release-please", diff --git a/src/updaters/node/package-lock-json.ts b/src/updaters/node/package-lock-json.ts index 26e809207..f540fb49d 100644 --- a/src/updaters/node/package-lock-json.ts +++ b/src/updaters/node/package-lock-json.ts @@ -19,7 +19,7 @@ import {DefaultUpdater} from '../default'; type LockFileV2 = { version: string; lockfileVersion?: number; - packages: Record; + packages: Record; }; /** @@ -34,6 +34,15 @@ export class PackageLockJson extends DefaultUpdater { if (parsed.lockfileVersion === 2 || parsed.lockfileVersion === 3) { parsed.packages[''].version = this.version.toString(); } + if (this.versionsMap) { + for (const [, obj] of Object.entries(parsed.packages)) { + const ver = this.versionsMap.get(obj.name); + if (ver) { + obj.version = ver.toString(); + } + } + } + return jsonStringify(parsed, content); } } diff --git a/test/updaters/fixtures/package-lock-v3-workspace.json b/test/updaters/fixtures/package-lock-v3-workspace.json new file mode 100644 index 000000000..c8d2d8979 --- /dev/null +++ b/test/updaters/fixtures/package-lock-v3-workspace.json @@ -0,0 +1,16 @@ +{ + "name": "release-please", + "version": "11.1.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "release-please", + "version": "11.1.0" + }, + "packages/foo": { + "name": "release-please-foo", + "version": "1.0.0" + } + } +} diff --git a/test/updaters/package-lock-json.ts b/test/updaters/package-lock-json.ts index acd6dcc94..873591455 100644 --- a/test/updaters/package-lock-json.ts +++ b/test/updaters/package-lock-json.ts @@ -63,4 +63,21 @@ describe('PackageLockJson', () => { snapshot(newContent.replace(/\r\n/g, '\n')); }); }); + + describe('updateContent v3 monorepo', () => { + it('updates the package version', async () => { + const oldContent = readFileSync( + resolve(fixturesPath, './package-lock-v3-workspace.json'), + 'utf8' + ); + const versionsMap = new Map(); + versionsMap.set('release-please-foo', new Version(2, 0, 0)); + const packageJson = new PackageLockJson({ + version: Version.parse('14.0.0'), + versionsMap, + }); + const newContent = packageJson.updateContent(oldContent); + snapshot(newContent.replace(/\r\n/g, '\n')); + }); + }); });