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

fix: Disable updating peer dependencies when updatePeerDependencies is false. #2270

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test that validates the peer dependency is not updated.

\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
1 change: 1 addition & 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
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test implicitly dependended on the updater always updating the peer dependencies. So it was updated and produces the same output.

});
const newContent = packageJson.updateContent(oldContent);
snapshot(newContent.replace(/\r\n/g, '\n'));
});

it('does not update peer dependencies by default', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New test which does not update peer dependencies.

const oldContent = readFileSync(
resolve(fixturesPath, './package-with-dependencies.json'),
'utf8'
Expand Down
Loading