Skip to content

Commit

Permalink
fix: disable updating peer dependencies when updatePeerDependencies i…
Browse files Browse the repository at this point in the history
…s false (#2270)
  • Loading branch information
kinyoklion authored Apr 16, 2024
1 parent 08331fa commit 2d80f12
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
56 changes: 56 additions & 0 deletions __snapshots__/package-json.js
Original file line number Diff line number Diff line change
@@ -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 <ben@npmjs.com>",
\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",
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/node-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
const updater = new PackageJson({
version: newVersion,
versionsMap: updatedVersions,
updatePeerDependencies: this.updatePeerDependencies,
});
const dependencyNotes = getChangelogDepsNotes(
pkg,
Expand Down Expand Up @@ -310,6 +311,7 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
updater: new PackageJson({
version: newVersion,
versionsMap: updatedVersions,
updatePeerDependencies: this.updatePeerDependencies,
}),
},
{
Expand All @@ -318,6 +320,7 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
updater: new PackageJson({
version: newVersion,
versionsMap: updatedVersions,
updatePeerDependencies: this.updatePeerDependencies,
}),
},
{
Expand Down
14 changes: 12 additions & 2 deletions src/updaters/node/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,10 +28,20 @@ export type PackageJsonDescriptor = {
optionalDependencies?: Record<string, string>;
};

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
Expand All @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions test/updaters/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 2d80f12

Please sign in to comment.