Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update package-lock.json workspace entiry versions #2088

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
});
});
});