forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddAction.ts
More file actions
162 lines (144 loc) · 6.55 KB
/
AddAction.ts
File metadata and controls
162 lines (144 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as os from 'os';
import * as semver from 'semver';
import {
CommandLineFlagParameter,
CommandLineStringParameter
} from '@rushstack/ts-command-line';
import { RushConfigurationProject } from '../../api/RushConfigurationProject';
import { BaseRushAction } from './BaseRushAction';
import { RushCommandLineParser } from '../RushCommandLineParser';
import { PackageJsonUpdater, SemVerStyle } from '../../logic/PackageJsonUpdater';
import { PackageName } from '@rushstack/node-core-library';
export class AddAction extends BaseRushAction {
private _allFlag: CommandLineFlagParameter;
private _exactFlag: CommandLineFlagParameter;
private _caretFlag: CommandLineFlagParameter;
private _devDependencyFlag: CommandLineFlagParameter;
private _makeConsistentFlag: CommandLineFlagParameter;
private _skipUpdateFlag: CommandLineFlagParameter;
private _packageName: CommandLineStringParameter;
public constructor(parser: RushCommandLineParser) {
const documentation: string[] = [
'Adds a specified package as a dependency of the current project (as determined by the current working directory)'
+ ' and then runs "rush update". If no version is specified, a version will be automatically detected (typically'
+ ' either the latest version or a version that won\'t break the "ensureConsistentVersions" policy). If a version'
+ ' range is specified, the latest version in the range will be used. The version will be automatically prepended'
+ ' with a tilde, unless the "--exact" or "--caret" flags are used. The "--make-consistent" flag can be used to'
+ ' update all packages with the dependency.'
];
super({
actionName: 'add',
summary: 'Adds a dependency to the package.json and runs rush upgrade.',
documentation: documentation.join(os.EOL),
safeForSimultaneousRushProcesses: false,
parser
});
}
public onDefineParameters(): void {
this._packageName = this.defineStringParameter({
parameterLongName: '--package',
parameterShortName: '-p',
required: true,
argumentName: 'PACKAGE',
description: '(Required) The name of the package which should be added as a dependency.'
+ ' A SemVer version specifier can be appended after an "@" sign. WARNING: Symbol characters'
+ ' are usually interpreted by your shell, so it\'s recommended to use quotes.'
+ ' For example, write "rush add --package "example@^1.2.3"" instead of "rush add --package example@^1.2.3".'
});
this._exactFlag = this.defineFlagParameter({
parameterLongName: '--exact',
description: 'If specified, the SemVer specifier added to the'
+ ' package.json will be an exact version (e.g. without tilde or caret).'
});
this._caretFlag = this.defineFlagParameter({
parameterLongName: '--caret',
description: 'If specified, the SemVer specifier added to the'
+ ' package.json will be a prepended with a "caret" specifier ("^").'
});
this._devDependencyFlag = this.defineFlagParameter({
parameterLongName: '--dev',
description: 'If specified, the package will be added to the "devDependencies" section of'
+ ' the package.json'
});
this._makeConsistentFlag = this.defineFlagParameter({
parameterLongName: '--make-consistent',
parameterShortName: '-m',
description: 'If specified, other packages with this dependency will have their package.json'
+ ' files updated to use the same version of the dependency.'
});
this._skipUpdateFlag = this.defineFlagParameter({
parameterLongName: '--skip-update',
parameterShortName: '-s',
description: 'If specified, the "rush update" command will not be run after updating the'
+ ' package.json files.'
});
this._allFlag = this.defineFlagParameter({
parameterLongName: '--all',
description: 'If specified, the dependency will be added to all projects.'
});
}
public async run(): Promise<void> {
let projects: RushConfigurationProject[];
if (this._allFlag.value) {
projects = this.rushConfiguration.projects;
} else {
const currentProject: RushConfigurationProject | undefined = this.rushConfiguration.tryGetProjectForPath(
process.cwd()
);
if (!currentProject) {
throw new Error('The "rush add" command must be invoked under a project'
+ ` folder that is registered in rush.json unless the ${this._allFlag.longName} is used.`);
}
projects = [currentProject];
}
if (this._caretFlag.value && this._exactFlag.value) {
throw new Error(
`Only one of "${this._caretFlag.longName}" and "${this._exactFlag.longName}" should be specified`
);
}
let version: string | undefined = undefined;
let packageName: string | undefined = this._packageName.value!;
const parts: string[] = packageName.split('@');
if (parts[0] === '') {
// this is a scoped package
packageName = '@' + parts[1];
version = parts[2];
} else {
packageName = parts[0];
version = parts[1];
}
if (!PackageName.isValidName(packageName)) {
throw new Error(`The package name "${packageName}" is not valid.`);
}
if (version && version !== 'latest' && !semver.validRange(version) && !semver.valid(version)) {
throw new Error(`The SemVer specifier "${version}" is not valid.`);
}
const updater: PackageJsonUpdater = new PackageJsonUpdater(this.rushConfiguration, this.rushGlobalFolder);
let rangeStyle: SemVerStyle;
if (version && version !== 'latest') {
if (this._exactFlag.value || this._caretFlag.value) {
throw new Error(
`The "${this._caretFlag.longName}" and "${this._exactFlag.longName}" flags may not be specified if a ` +
`version is provided in the ${this._packageName.longName} specifier. In this case "${version}" was provided.`
);
}
rangeStyle = SemVerStyle.Passthrough;
} else {
rangeStyle = this._caretFlag.value
? SemVerStyle.Caret
: (this._exactFlag.value ? SemVerStyle.Exact : SemVerStyle.Tilde);
}
await updater.doRushAdd({
projects: projects,
packageName: packageName,
initialVersion: version,
devDependency: this._devDependencyFlag.value,
updateOtherPackages: this._makeConsistentFlag.value,
skipUpdate: this._skipUpdateFlag.value,
debugInstall: this.parser.isDebug,
rangeStyle: rangeStyle
});
}
}