Skip to content

Commit

Permalink
Merge pull request microsoft#3986 from moegelin/feature/rush-add-peer…
Browse files Browse the repository at this point in the history
…-flag

[rush-lib] Support --peer flag for rush add command
  • Loading branch information
iclanton committed Jun 11, 2023
2 parents 2367f36 + a63e802 commit 4c48a8f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
10 changes: 10 additions & 0 deletions common/changes/@microsoft/rush/main_2023-02-23-17-02.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Add `--peer` flag to `rush add` command to add peerDependencies",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
7 changes: 7 additions & 0 deletions libraries/rush-lib/src/cli/actions/AddAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class AddAction extends BaseAddAndRemoveAction {
private readonly _exactFlag: CommandLineFlagParameter;
private readonly _caretFlag: CommandLineFlagParameter;
private readonly _devDependencyFlag: CommandLineFlagParameter;
private readonly _peerDependencyFlag: CommandLineFlagParameter;
private readonly _makeConsistentFlag: CommandLineFlagParameter;

public constructor(parser: RushCommandLineParser) {
Expand Down Expand Up @@ -68,6 +69,11 @@ export class AddAction extends BaseAddAndRemoveAction {
description:
'If specified, the package will be added to the "devDependencies" section of the package.json'
});
this._peerDependencyFlag = this.defineFlagParameter({
parameterLongName: '--peer',
description:
'If specified, the package will be added to the "peerDependencies" section of the package.json'
});
this._makeConsistentFlag = this.defineFlagParameter({
parameterLongName: '--make-consistent',
parameterShortName: '-m',
Expand Down Expand Up @@ -147,6 +153,7 @@ export class AddAction extends BaseAddAndRemoveAction {
projects: projects,
packagesToUpdate: packagesToAdd,
devDependency: this._devDependencyFlag.value,
peerDependency: this._peerDependencyFlag.value,
updateOtherPackages: this._makeConsistentFlag.value,
skipUpdate: this._skipUpdateFlag.value,
debugInstall: this.parser.isDebug,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ Optional arguments:
`;
exports[`CommandLineHelp prints the help for each action: add 1`] = `
"usage: rush add [-h] [-s] -p PACKAGE [--exact] [--caret] [--dev] [-m] [--all]
"usage: rush add [-h] [-s] -p PACKAGE [--exact] [--caret] [--dev] [--peer] [-m]
[--all]
Adds specified package(s) to the dependencies of the current project (as
determined by the current working directory) and then runs \\"rush update\\". If
Expand Down Expand Up @@ -111,6 +113,8 @@ Optional arguments:
specifier (\\"^\\").
--dev If specified, the package will be added to the
\\"devDependencies\\" section of the package.json
--peer If specified, the package will be added to the
\\"peerDependencies\\" section of the package.json
-m, --make-consistent
If specified, other packages with this dependency
will have their package.json files updated to use the
Expand Down
21 changes: 16 additions & 5 deletions libraries/rush-lib/src/logic/PackageJsonUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,15 @@ export class PackageJsonUpdater {

const dependenciesToUpdate: Record<string, string> = {};
const devDependenciesToUpdate: Record<string, string> = {};

for (const { moduleName, latest: latestVersion, packageJson, devDependency } of packagesToAdd) {
const peerDependenciesToUpdate: Record<string, string> = {};

for (const {
moduleName,
latest: latestVersion,
packageJson,
devDependency,
peerDependency
} of packagesToAdd) {
const inferredRangeStyle: SemVerStyle = this._cheaplyDetectSemVerRangeStyle(packageJson);
const implicitlyPreferredVersion: string | undefined =
implicitlyPreferredVersionByPackageName.get(moduleName);
Expand All @@ -152,6 +159,8 @@ export class PackageJsonUpdater {

if (devDependency) {
devDependenciesToUpdate[moduleName] = version;
} else if (peerDependency) {
peerDependenciesToUpdate[moduleName] = version;
} else {
dependenciesToUpdate[moduleName] = version;
}
Expand Down Expand Up @@ -183,7 +192,8 @@ export class PackageJsonUpdater {
const allPackageUpdates: Map<string, VersionMismatchFinderEntity> = new Map();
const allDependenciesToUpdate: [string, string][] = [
...Object.entries(dependenciesToUpdate),
...Object.entries(devDependenciesToUpdate)
...Object.entries(devDependenciesToUpdate),
...Object.entries(peerDependenciesToUpdate)
];

for (const project of projects) {
Expand Down Expand Up @@ -318,7 +328,8 @@ export class PackageJsonUpdater {
private async _doRushAddAsync(
options: IPackageJsonUpdaterRushAddOptions
): Promise<IUpdateProjectOptions[]> {
const { projects, packagesToUpdate, devDependency, updateOtherPackages, variant } = options;
const { projects, packagesToUpdate, devDependency, peerDependency, updateOtherPackages, variant } =
options;

const { DependencyAnalyzer } = await import(
/* webpackChunkName: 'DependencyAnalyzer' */
Expand Down Expand Up @@ -384,7 +395,7 @@ export class PackageJsonUpdater {
const currentProjectUpdate: IUpdateProjectOptions = {
project: new VersionMismatchFinderProject(project),
dependenciesToAddOrUpdateOrRemove: dependenciesToAddOrUpdate,
dependencyType: devDependency ? DependencyType.Dev : undefined
dependencyType: devDependency ? DependencyType.Dev : peerDependency ? DependencyType.Peer : undefined
};
this.updateProject(currentProjectUpdate);

Expand Down
4 changes: 4 additions & 0 deletions libraries/rush-lib/src/logic/PackageJsonUpdaterTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export interface IPackageJsonUpdaterRushAddOptions extends IPackageJsonUpdaterRu
* Whether or not this dependency should be added as a devDependency or a regular dependency.
*/
devDependency: boolean;
/**
* Whether or not this dependency should be added as a peerDependency or a regular dependency.
*/
peerDependency: boolean;
/**
* If specified, other packages that use this dependency will also have their package.json's updated.
*/
Expand Down
1 change: 1 addition & 0 deletions libraries/rush-lib/src/npm-check-typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare module 'npm-check' {
packageWanted: string; // Requested version from the package.json.
packageJson: string; // Version or range requested in the parent package.json.
devDependency: boolean; // Is this a devDependency?
peerDependency: boolean; // Is this a peerDependency?
usedInScripts: undefined | string[]; // Array of `scripts` in package.json that use this module.
mismatch: boolean; // Does the version installed not match the range in package.json?
semverValid: string; // Is the installed version valid semver?
Expand Down

0 comments on commit 4c48a8f

Please sign in to comment.