From 2d80f128def8f1dc4b1b7a70b094ed6039ccd073 Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Tue, 16 Apr 2024 13:34:05 -0700 Subject: [PATCH] fix: disable updating peer dependencies when updatePeerDependencies is false (#2270) --- __snapshots__/package-json.js | 56 +++++++++++++++++++++++++++++++ src/plugins/node-workspace.ts | 3 ++ src/updaters/node/package-json.ts | 14 ++++++-- test/updaters/package-json.ts | 19 +++++++++++ 4 files changed, 90 insertions(+), 2 deletions(-) diff --git a/__snapshots__/package-json.js b/__snapshots__/package-json.js index 3e01933d5..5676325bb 100644 --- a/__snapshots__/package-json.js +++ b/__snapshots__/package-json.js @@ -1,3 +1,59 @@ +exports['PackageJson updateContent does not update peer dependencies by default 1'] = ` +{ +\t"name": "yargs-parser", +\t"version": "14.0.0", +\t"description": "the mighty option parser used by yargs", +\t"main": "index.js", +\t"scripts": { +\t\t"test": "nyc mocha test/*.js", +\t\t"posttest": "standard", +\t\t"coverage": "nyc report --reporter=text-lcov | coveralls", +\t\t"release": "standard-version" +\t}, +\t"repository": { +\t\t"url": "git@github.com:yargs/yargs-parser.git" +\t}, +\t"keywords": [ +\t\t"argument", +\t\t"parser", +\t\t"yargs", +\t\t"command", +\t\t"cli", +\t\t"parsing", +\t\t"option", +\t\t"args", +\t\t"argument" +\t], +\t"author": "Ben Coe ", +\t"license": "ISC", +\t"devDependencies": { +\t\t"chai": "^4.2.1", +\t\t"coveralls": "^3.0.2", +\t\t"mocha": "^5.2.0", +\t\t"nyc": "^13.0.1", +\t\t"standard": "^12.0.1" +\t}, +\t"dependencies": { +\t\t"camelcase": "^6.0.0", +\t\t"decamelize": "^1.2.0" +\t}, +\t"optionalDependencies": { +\t\t"foo": "~0.1.0" +\t}, +\t"peerDependencies": { +\t\t"bar": ">= 1.0.0" +\t}, +\t"files": [ +\t\t"lib", +\t\t"index.js" +\t], +\t"engine": { +\t\t"node": ">=6" +\t} +} + +` + exports['PackageJson updateContent updates dependency versions 1'] = ` { \t"name": "yargs-parser", diff --git a/src/plugins/node-workspace.ts b/src/plugins/node-workspace.ts index 1d309ebd9..3e942899a 100644 --- a/src/plugins/node-workspace.ts +++ b/src/plugins/node-workspace.ts @@ -191,6 +191,7 @@ export class NodeWorkspace extends WorkspacePlugin { const updater = new PackageJson({ version: newVersion, versionsMap: updatedVersions, + updatePeerDependencies: this.updatePeerDependencies, }); const dependencyNotes = getChangelogDepsNotes( pkg, @@ -310,6 +311,7 @@ export class NodeWorkspace extends WorkspacePlugin { updater: new PackageJson({ version: newVersion, versionsMap: updatedVersions, + updatePeerDependencies: this.updatePeerDependencies, }), }, { @@ -318,6 +320,7 @@ export class NodeWorkspace extends WorkspacePlugin { updater: new PackageJson({ version: newVersion, versionsMap: updatedVersions, + updatePeerDependencies: this.updatePeerDependencies, }), }, { diff --git a/src/updaters/node/package-json.ts b/src/updaters/node/package-json.ts index 9eb5cf104..025b3b225 100644 --- a/src/updaters/node/package-json.ts +++ b/src/updaters/node/package-json.ts @@ -15,7 +15,7 @@ import {jsonStringify} from '../../util/json-stringify'; import {logger as defaultLogger, Logger} from '../../util/logger'; import {Version, VersionsMap} from '../../version'; -import {DefaultUpdater} from '../default'; +import {DefaultUpdater, UpdateOptions} from '../default'; export type PackageJsonDescriptor = { name?: string; @@ -28,10 +28,20 @@ export type PackageJsonDescriptor = { optionalDependencies?: Record; }; +export interface PackageJsonOptions extends UpdateOptions { + updatePeerDependencies?: boolean; +} + /** * This updates a Node.js package.json file's main version. */ export class PackageJson extends DefaultUpdater { + private updatePeerDependencies = false; + + constructor(options: PackageJsonOptions) { + super(options); + this.updatePeerDependencies = options.updatePeerDependencies || false; + } /** * Given initial file contents, return updated contents. * @param {string} content The initial content @@ -52,7 +62,7 @@ export class PackageJson extends DefaultUpdater { if (parsed.devDependencies) { updateDependencies(parsed.devDependencies, this.versionsMap); } - if (parsed.peerDependencies) { + if (parsed.peerDependencies && this.updatePeerDependencies) { updateDependencies(parsed.peerDependencies, this.versionsMap); } if (parsed.optionalDependencies) { diff --git a/test/updaters/package-json.ts b/test/updaters/package-json.ts index 00a4861a9..a7c1536f6 100644 --- a/test/updaters/package-json.ts +++ b/test/updaters/package-json.ts @@ -36,6 +36,25 @@ describe('PackageJson', () => { }); it('updates dependency versions', async () => { + const oldContent = readFileSync( + resolve(fixturesPath, './package-with-dependencies.json'), + 'utf8' + ); + const versionsMap: VersionsMap = new Map(); + versionsMap.set('camelcase', Version.parse('6.0.0')); + versionsMap.set('chai', Version.parse('4.2.1')); + versionsMap.set('foo', Version.parse('0.1.0')); + versionsMap.set('bar', Version.parse('2.3.4')); + const packageJson = new PackageJson({ + version: Version.parse('14.0.0'), + versionsMap, + updatePeerDependencies: true, + }); + const newContent = packageJson.updateContent(oldContent); + snapshot(newContent.replace(/\r\n/g, '\n')); + }); + + it('does not update peer dependencies by default', async () => { const oldContent = readFileSync( resolve(fixturesPath, './package-with-dependencies.json'), 'utf8'