Skip to content

Commit

Permalink
fix(node): do not update versions of packages installed using a protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
niieani committed May 4, 2024
1 parent cb106cd commit ac76d7c
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
56 changes: 56 additions & 0 deletions __snapshots__/package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ben@npmjs.com>",
\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",
Expand Down
6 changes: 5 additions & 1 deletion src/updaters/node/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
}
Expand Down
52 changes: 52 additions & 0 deletions test/updaters/fixtures/package-with-protocol-dependencies.json
Original file line number Diff line number Diff line change
@@ -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 <ben@npmjs.com>",
"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"
}
}
20 changes: 20 additions & 0 deletions test/updaters/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit ac76d7c

Please sign in to comment.