From eeb14111849aefa7024f754121f2c05a4e9ab1ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bazyli=20Brz=C3=B3ska?= Date: Thu, 6 Jun 2024 09:28:50 -0700 Subject: [PATCH] fix(node): do not update versions of packages installed using a protocol (#2281) fixes #2173 Co-authored-by: Jeff Ching --- __snapshots__/package-json.js | 56 +++++++++++++++++++ src/updaters/node/package-json.ts | 6 +- .../package-with-protocol-dependencies.json | 52 +++++++++++++++++ test/updaters/package-json.ts | 20 +++++++ 4 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 test/updaters/fixtures/package-with-protocol-dependencies.json diff --git a/__snapshots__/package-json.js b/__snapshots__/package-json.js index 5676325bb..686fc26ed 100644 --- a/__snapshots__/package-json.js +++ b/__snapshots__/package-json.js @@ -54,6 +54,62 @@ exports['PackageJson updateContent does not update peer dependencies by default ` +exports['PackageJson updateContent does not update versions of dependencies that are protocols 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.0", +\t\t"coveralls": "^3.0.2", +\t\t"mocha": "^5.2.0", +\t\t"nyc": "workspace:*", +\t\t"standard": "link:../standard" +\t}, +\t"dependencies": { +\t\t"camelcase": "^5.0.0", +\t\t"decamelize": "portal:../decamelize" +\t}, +\t"optionalDependencies": { +\t\t"foo": "~0.1.0" +\t}, +\t"peerDependencies": { +\t\t"bar": "workspace:^" +\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/updaters/node/package-json.ts b/src/updaters/node/package-json.ts index 025b3b225..402598933 100644 --- a/src/updaters/node/package-json.ts +++ b/src/updaters/node/package-json.ts @@ -105,6 +105,7 @@ export function newVersionWithRange( } return newVersion.toString(); } +export const NPM_PROTOCOL_REGEXP = /^[a-z]+:/; /** * Helper function to update dependency versions for all new versions specified * in the updated versions map. Note that this mutates the existing input. @@ -117,9 +118,12 @@ export function updateDependencies( updatedVersions: VersionsMap ) { for (const depName of Object.keys(dependencies)) { + const oldVersion = dependencies[depName]; + if (NPM_PROTOCOL_REGEXP.test(oldVersion)) { + continue; + } const newVersion = updatedVersions.get(depName); if (newVersion) { - const oldVersion = dependencies[depName]; dependencies[depName] = newVersionWithRange(oldVersion, newVersion); } } diff --git a/test/updaters/fixtures/package-with-protocol-dependencies.json b/test/updaters/fixtures/package-with-protocol-dependencies.json new file mode 100644 index 000000000..e0d9a0114 --- /dev/null +++ b/test/updaters/fixtures/package-with-protocol-dependencies.json @@ -0,0 +1,52 @@ +{ + "name": "yargs-parser", + "version": "13.0.0", + "description": "the mighty option parser used by yargs", + "main": "index.js", + "scripts": { + "test": "nyc mocha test/*.js", + "posttest": "standard", + "coverage": "nyc report --reporter=text-lcov | coveralls", + "release": "standard-version" + }, + "repository": { + "url": "git@github.com:yargs/yargs-parser.git" + }, + "keywords": [ + "argument", + "parser", + "yargs", + "command", + "cli", + "parsing", + "option", + "args", + "argument" + ], + "author": "Ben Coe ", + "license": "ISC", + "devDependencies": { + "chai": "^4.2.0", + "coveralls": "^3.0.2", + "mocha": "^5.2.0", + "nyc": "workspace:*", + "standard": "link:../standard" + }, + "dependencies": { + "camelcase": "^5.0.0", + "decamelize": "portal:../decamelize" + }, + "optionalDependencies": { + "foo": "~0.0.7" + }, + "peerDependencies": { + "bar": "workspace:^" + }, + "files": [ + "lib", + "index.js" + ], + "engine": { + "node": ">=6" + } +} diff --git a/test/updaters/package-json.ts b/test/updaters/package-json.ts index a7c1536f6..10c5e31e0 100644 --- a/test/updaters/package-json.ts +++ b/test/updaters/package-json.ts @@ -54,6 +54,26 @@ describe('PackageJson', () => { snapshot(newContent.replace(/\r\n/g, '\n')); }); + it('does not update versions of dependencies that are protocols', async () => { + const oldContent = readFileSync( + resolve(fixturesPath, './package-with-protocol-dependencies.json'), + 'utf8' + ); + const versionsMap: VersionsMap = new Map(); + versionsMap.set('nyc', Version.parse('6.0.0')); + versionsMap.set('standard', Version.parse('4.2.1')); + versionsMap.set('decamelize', Version.parse('4.4.4')); + 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'),