Skip to content

Commit

Permalink
feat: update package-lock.json workspace entry versions (#2088)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeff Ching <chingor@google.com>

Fixes #1993
  • Loading branch information
wesleytodd authored Oct 26, 2023
1 parent 97b0542 commit dbb11bc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
20 changes: 20 additions & 0 deletions __snapshots__/package-lock-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 10 additions & 1 deletion src/updaters/node/package-lock-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {DefaultUpdater} from '../default';
type LockFileV2 = {
version: string;
lockfileVersion?: number;
packages: Record<string, {version: string}>;
packages: Record<string, {version: string; name: string}>;
};

/**
Expand All @@ -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);
}
}
16 changes: 16 additions & 0 deletions test/updaters/fixtures/package-lock-v3-workspace.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
17 changes: 17 additions & 0 deletions test/updaters/package-lock-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
});
});

0 comments on commit dbb11bc

Please sign in to comment.