33
44import * as semver from 'semver' ;
55
6- import type {
7- CommandLineFlagParameter ,
8- CommandLineStringListParameter ,
9- CommandLineStringParameter
10- } from '@rushstack/ts-command-line' ;
6+ import type { CommandLineFlagParameter } from '@rushstack/ts-command-line' ;
117
12- import { BaseAddAndRemoveAction } from './BaseAddAndRemoveAction' ;
8+ import { BaseAddAndRemoveAction , PACKAGE_PARAMETER_NAME } from './BaseAddAndRemoveAction' ;
139import type { RushCommandLineParser } from '../RushCommandLineParser' ;
1410import { DependencySpecifier } from '../../logic/DependencySpecifier' ;
1511import type { RushConfigurationProject } from '../../api/RushConfigurationProject' ;
@@ -18,55 +14,52 @@ import {
1814 type IPackageJsonUpdaterRushAddOptions ,
1915 SemVerStyle
2016} from '../../logic/PackageJsonUpdaterTypes' ;
21- import { getVariantAsync , VARIANT_PARAMETER } from '../../api/Variants' ;
17+ import { getVariantAsync } from '../../api/Variants' ;
18+
19+ const ADD_ACTION_NAME : 'add' = 'add' ;
20+ export const MAKE_CONSISTENT_FLAG_NAME : '--make-consistent' = '--make-consistent' ;
21+ const EXACT_FLAG_NAME : '--exact' = '--exact' ;
22+ const CARET_FLAG_NAME : '--caret' = '--caret' ;
2223
2324export class AddAction extends BaseAddAndRemoveAction {
24- protected readonly _allFlag : CommandLineFlagParameter ;
25- protected readonly _packageNameList : CommandLineStringListParameter ;
2625 private readonly _exactFlag : CommandLineFlagParameter ;
2726 private readonly _caretFlag : CommandLineFlagParameter ;
2827 private readonly _devDependencyFlag : CommandLineFlagParameter ;
2928 private readonly _peerDependencyFlag : CommandLineFlagParameter ;
3029 private readonly _makeConsistentFlag : CommandLineFlagParameter ;
31- private readonly _variantParameter : CommandLineStringParameter ;
3230
3331 public constructor ( parser : RushCommandLineParser ) {
3432 const documentation : string = [
3533 'Adds specified package(s) to the dependencies of the current project (as determined by the current working directory)' +
3634 ' and then runs "rush update". If no version is specified, a version will be automatically detected (typically' +
3735 ' either the latest version or a version that won\'t break the "ensureConsistentVersions" policy). If a version' +
3836 ' range (or a workspace range) is specified, the latest version in the range will be used. The version will be' +
39- ' automatically prepended with a tilde, unless the "--exact " or "--caret " flags are used. The "--make-consistent"' +
40- ' flag can be used to update all packages with the dependency.'
37+ ` automatically prepended with a tilde, unless the "${ EXACT_FLAG_NAME } " or "${ CARET_FLAG_NAME } " flags are used.` +
38+ ` The " ${ MAKE_CONSISTENT_FLAG_NAME } " flag can be used to update all packages with the dependency.`
4139 ] . join ( '\n' ) ;
4240 super ( {
43- actionName : 'add' ,
41+ actionName : ADD_ACTION_NAME ,
4442 summary : 'Adds one or more dependencies to the package.json and runs rush update.' ,
4543 documentation,
4644 safeForSimultaneousRushProcesses : false ,
47- parser
48- } ) ;
49-
50- this . _packageNameList = this . defineStringListParameter ( {
51- parameterLongName : '--package' ,
52- parameterShortName : '-p' ,
53- required : true ,
54- argumentName : 'PACKAGE' ,
55- description :
45+ parser,
46+ allFlagDescription : 'If specified, the dependency will be added to all projects.' ,
47+ packageNameListParameterDescription :
5648 'The name of the package which should be added as a dependency.' +
5749 ' A SemVer version specifier can be appended after an "@" sign. WARNING: Symbol characters' +
5850 " are usually interpreted by your shell, so it's recommended to use quotes." +
59- ' For example, write "rush add --package "example@^1.2.3"" instead of "rush add --package example@^1.2.3".' +
60- ' To add multiple packages, write "rush add --package foo --package bar".'
51+ ` For example, write "rush add ${ PACKAGE_PARAMETER_NAME } "example@^1.2.3"" instead of "rush add ${ PACKAGE_PARAMETER_NAME } example@^1.2.3".` +
52+ ` To add multiple packages, write "rush add ${ PACKAGE_PARAMETER_NAME } foo ${ PACKAGE_PARAMETER_NAME } bar".`
6153 } ) ;
54+
6255 this . _exactFlag = this . defineFlagParameter ( {
63- parameterLongName : '--exact' ,
56+ parameterLongName : EXACT_FLAG_NAME ,
6457 description :
6558 'If specified, the SemVer specifier added to the' +
6659 ' package.json will be an exact version (e.g. without tilde or caret).'
6760 } ) ;
6861 this . _caretFlag = this . defineFlagParameter ( {
69- parameterLongName : '--caret' ,
62+ parameterLongName : CARET_FLAG_NAME ,
7063 description :
7164 'If specified, the SemVer specifier added to the' +
7265 ' package.json will be a prepended with a "caret" specifier ("^").'
@@ -82,17 +75,12 @@ export class AddAction extends BaseAddAndRemoveAction {
8275 'If specified, the package will be added to the "peerDependencies" section of the package.json'
8376 } ) ;
8477 this . _makeConsistentFlag = this . defineFlagParameter ( {
85- parameterLongName : '--make-consistent' ,
78+ parameterLongName : MAKE_CONSISTENT_FLAG_NAME ,
8679 parameterShortName : '-m' ,
8780 description :
8881 'If specified, other packages with this dependency will have their package.json' +
8982 ' files updated to use the same version of the dependency.'
9083 } ) ;
91- this . _allFlag = this . defineFlagParameter ( {
92- parameterLongName : '--all' ,
93- description : 'If specified, the dependency will be added to all projects.'
94- } ) ;
95- this . _variantParameter = this . defineStringParameter ( VARIANT_PARAMETER ) ;
9684 }
9785
9886 public async getUpdateOptionsAsync ( ) : Promise < IPackageJsonUpdaterRushAddOptions > {
@@ -142,7 +130,7 @@ export class AddAction extends BaseAddAndRemoveAction {
142130 if ( this . _exactFlag . value || this . _caretFlag . value ) {
143131 throw new Error (
144132 `The "${ this . _caretFlag . longName } " and "${ this . _exactFlag . longName } " flags may not be specified if a ` +
145- `version is provided in the ${ this . _packageNameList . longName } specifier. In this case "${ version } " was provided.`
133+ `version is provided in the ${ this . _packageNameListParameter . longName } specifier. In this case "${ version } " was provided.`
146134 ) ;
147135 }
148136
@@ -165,7 +153,7 @@ export class AddAction extends BaseAddAndRemoveAction {
165153 ) ;
166154
167155 return {
168- projects : projects ,
156+ projects,
169157 packagesToUpdate : packagesToAdd ,
170158 devDependency : this . _devDependencyFlag . value ,
171159 peerDependency : this . _peerDependencyFlag . value ,
0 commit comments