forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharchitect-base-command-module.ts
300 lines (256 loc) · 8.45 KB
/
architect-base-command-module.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { Architect, Target } from '@angular-devkit/architect';
import {
NodeModulesBuilderInfo,
WorkspaceNodeModulesArchitectHost,
} from '@angular-devkit/architect/node';
import { json } from '@angular-devkit/core';
import { existsSync } from 'node:fs';
import { resolve } from 'node:path';
import { isPackageNameSafeForAnalytics } from '../analytics/analytics';
import { EventCustomDimension, EventCustomMetric } from '../analytics/analytics-parameters';
import { assertIsError } from '../utilities/error';
import { askConfirmation, askQuestion } from '../utilities/prompt';
import { isTTY } from '../utilities/tty';
import {
CommandModule,
CommandModuleError,
CommandModuleImplementation,
CommandScope,
OtherOptions,
} from './command-module';
import { Option, parseJsonSchemaToOptions } from './utilities/json-schema';
export interface MissingTargetChoice {
name: string;
value: string;
}
export abstract class ArchitectBaseCommandModule<T extends object>
extends CommandModule<T>
implements CommandModuleImplementation<T>
{
override scope = CommandScope.In;
protected readonly missingTargetChoices: MissingTargetChoice[] | undefined;
protected async runSingleTarget(target: Target, options: OtherOptions): Promise<number> {
const architectHost = this.getArchitectHost();
let builderName: string;
try {
builderName = await architectHost.getBuilderNameForTarget(target);
} catch (e) {
assertIsError(e);
return this.onMissingTarget(e.message);
}
const isAngularBuild = builderName.startsWith('@angular/build:');
const { logger } = this.context;
const run = await this.getArchitect(isAngularBuild).scheduleTarget(
target,
options as json.JsonObject,
{
logger,
},
);
const analytics = isPackageNameSafeForAnalytics(builderName)
? await this.getAnalytics()
: undefined;
let outputSubscription;
if (analytics) {
analytics.reportArchitectRunEvent({
[EventCustomDimension.BuilderTarget]: builderName,
});
let firstRun = true;
outputSubscription = run.output.subscribe(({ stats }) => {
const parameters = this.builderStatsToAnalyticsParameters(stats, builderName);
if (!parameters) {
return;
}
if (firstRun) {
firstRun = false;
analytics.reportBuildRunEvent(parameters);
} else {
analytics.reportRebuildRunEvent(parameters);
}
});
}
try {
const { error, success } = await run.lastOutput;
if (error) {
logger.error(error);
}
return success ? 0 : 1;
} finally {
await run.stop();
outputSubscription?.unsubscribe();
}
}
private builderStatsToAnalyticsParameters(
stats: json.JsonValue,
builderName: string,
): Partial<
| Record<EventCustomDimension & EventCustomMetric, string | number | undefined | boolean>
| undefined
> {
if (!stats || typeof stats !== 'object' || !('durationInMs' in stats)) {
return undefined;
}
const {
optimization,
allChunksCount,
aot,
lazyChunksCount,
initialChunksCount,
durationInMs,
changedChunksCount,
cssSizeInBytes,
jsSizeInBytes,
ngComponentCount,
} = stats;
return {
[EventCustomDimension.BuilderTarget]: builderName,
[EventCustomDimension.Aot]: aot,
[EventCustomDimension.Optimization]: optimization,
[EventCustomMetric.AllChunksCount]: allChunksCount,
[EventCustomMetric.LazyChunksCount]: lazyChunksCount,
[EventCustomMetric.InitialChunksCount]: initialChunksCount,
[EventCustomMetric.ChangedChunksCount]: changedChunksCount,
[EventCustomMetric.DurationInMs]: durationInMs,
[EventCustomMetric.JsSizeInBytes]: jsSizeInBytes,
[EventCustomMetric.CssSizeInBytes]: cssSizeInBytes,
[EventCustomMetric.NgComponentCount]: ngComponentCount,
};
}
private _architectHost: WorkspaceNodeModulesArchitectHost | undefined;
protected getArchitectHost(): WorkspaceNodeModulesArchitectHost {
if (this._architectHost) {
return this._architectHost;
}
const workspace = this.getWorkspaceOrThrow();
return (this._architectHost = new WorkspaceNodeModulesArchitectHost(
workspace,
workspace.basePath,
));
}
private _architect: Architect | undefined;
protected getArchitect(skipUndefinedArrayTransform: boolean): Architect {
if (this._architect) {
return this._architect;
}
const registry = new json.schema.CoreSchemaRegistry();
if (skipUndefinedArrayTransform) {
registry.addPostTransform(json.schema.transforms.addUndefinedObjectDefaults);
} else {
registry.addPostTransform(json.schema.transforms.addUndefinedDefaults);
}
registry.useXDeprecatedProvider((msg) => this.context.logger.warn(msg));
const architectHost = this.getArchitectHost();
return (this._architect = new Architect(architectHost, registry));
}
protected async getArchitectTargetOptions(target: Target): Promise<Option[]> {
const architectHost = this.getArchitectHost();
let builderConf: string;
try {
builderConf = await architectHost.getBuilderNameForTarget(target);
} catch {
return [];
}
let builderDesc: NodeModulesBuilderInfo;
try {
builderDesc = await architectHost.resolveBuilder(builderConf);
} catch (e) {
assertIsError(e);
if (e.code === 'MODULE_NOT_FOUND') {
this.warnOnMissingNodeModules();
throw new CommandModuleError(`Could not find the '${builderConf}' builder's node package.`);
}
throw e;
}
return parseJsonSchemaToOptions(
new json.schema.CoreSchemaRegistry(),
builderDesc.optionSchema as json.JsonObject,
true,
);
}
private warnOnMissingNodeModules(): void {
const basePath = this.context.workspace?.basePath;
if (!basePath) {
return;
}
// Check if yarn PnP is used. https://yarnpkg.com/advanced/pnpapi#processversionspnp
if (process.versions.pnp) {
return;
}
// Check for a `node_modules` directory (npm, yarn non-PnP, etc.)
if (existsSync(resolve(basePath, 'node_modules'))) {
return;
}
this.context.logger.warn(
`Node packages may not be installed. Try installing with '${this.context.packageManager.name} install'.`,
);
}
protected getArchitectTarget(): string {
return this.commandName;
}
protected async onMissingTarget(defaultMessage: string): Promise<1> {
const { logger } = this.context;
const choices = this.missingTargetChoices;
if (!choices?.length) {
logger.error(defaultMessage);
return 1;
}
const missingTargetMessage =
`Cannot find "${this.getArchitectTarget()}" target for the specified project.\n` +
`You can add a package that implements these capabilities.\n\n` +
`For example:\n` +
choices.map(({ name, value }) => ` ${name}: ng add ${value}`).join('\n') +
'\n';
if (isTTY()) {
// Use prompts to ask the user if they'd like to install a package.
logger.warn(missingTargetMessage);
const packageToInstall = await this.getMissingTargetPackageToInstall(choices);
if (packageToInstall) {
// Example run: `ng add angular-eslint`.
const AddCommandModule = (await import('../commands/add/cli')).default;
await new AddCommandModule(this.context).run({
interactive: true,
force: false,
dryRun: false,
defaults: false,
collection: packageToInstall,
});
}
} else {
// Non TTY display error message.
logger.error(missingTargetMessage);
}
return 1;
}
private async getMissingTargetPackageToInstall(
choices: MissingTargetChoice[],
): Promise<string | null> {
if (choices.length === 1) {
// Single choice
const { name, value } = choices[0];
if (await askConfirmation(`Would you like to add ${name} now?`, true, false)) {
return value;
}
return null;
}
// Multiple choice
return askQuestion(
`Would you like to add a package with "${this.getArchitectTarget()}" capabilities now?`,
[
{
name: 'No',
value: null,
},
...choices,
],
0,
null,
);
}
}