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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
"use strict";
/**
* @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.io/license
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArchitectCommand = void 0;
const architect_1 = require("@angular-devkit/architect");
const node_1 = require("@angular-devkit/architect/node");
const core_1 = require("@angular-devkit/core");
const fs_1 = require("fs");
const path = __importStar(require("path"));
const json_schema_1 = require("../utilities/json-schema");
const package_manager_1 = require("../utilities/package-manager");
const analytics_1 = require("./analytics");
const command_1 = require("./command");
const parser_1 = require("./parser");
class ArchitectCommand extends command_1.Command {
constructor() {
super(...arguments);
this.useReportAnalytics = false;
// If this command supports running multiple targets.
this.multiTarget = false;
}
async onMissingTarget(projectName) {
if (this.missingTargetError) {
this.logger.fatal(this.missingTargetError);
return 1;
}
if (projectName) {
this.logger.fatal(`Project '${projectName}' does not support the '${this.target}' target.`);
}
else {
this.logger.fatal(`No projects support the '${this.target}' target.`);
}
return 1;
}
// eslint-disable-next-line max-lines-per-function
async initialize(options) {
this._registry = new core_1.json.schema.CoreSchemaRegistry();
this._registry.addPostTransform(core_1.json.schema.transforms.addUndefinedDefaults);
this._registry.useXDeprecatedProvider((msg) => this.logger.warn(msg));
if (!this.workspace) {
this.logger.fatal('A workspace is required for this command.');
return 1;
}
this._architectHost = new node_1.WorkspaceNodeModulesArchitectHost(this.workspace, this.workspace.basePath);
this._architect = new architect_1.Architect(this._architectHost, this._registry);
if (!this.target) {
if (options.help) {
// This is a special case where we just return.
return;
}
const specifier = this._makeTargetSpecifier(options);
if (!specifier.project || !specifier.target) {
this.logger.fatal('Cannot determine project or target for command.');
return 1;
}
return;
}
let projectName = options.project;
if (projectName && !this.workspace.projects.has(projectName)) {
this.logger.fatal(`Project '${projectName}' does not exist.`);
return 1;
}
const commandLeftovers = options['--'];
const targetProjectNames = [];
for (const [name, project] of this.workspace.projects) {
if (project.targets.has(this.target)) {
targetProjectNames.push(name);
}
}
if (projectName && !targetProjectNames.includes(projectName)) {
return await this.onMissingTarget(projectName);
}
if (targetProjectNames.length === 0) {
return await this.onMissingTarget();
}
if (!projectName && commandLeftovers && commandLeftovers.length > 0) {
const builderNames = new Set();
const leftoverMap = new Map();
let potentialProjectNames = new Set(targetProjectNames);
for (const name of targetProjectNames) {
const builderName = await this._architectHost.getBuilderNameForTarget({
project: name,
target: this.target,
});
if (this.multiTarget) {
builderNames.add(builderName);
}
let builderDesc;
try {
builderDesc = await this._architectHost.resolveBuilder(builderName);
}
catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
await this.warnOnMissingNodeModules(this.workspace.basePath);
this.logger.fatal(`Could not find the '${builderName}' builder's node package.`);
return 1;
}
throw e;
}
const optionDefs = await (0, json_schema_1.parseJsonSchemaToOptions)(this._registry, builderDesc.optionSchema);
const parsedOptions = (0, parser_1.parseArguments)([...commandLeftovers], optionDefs);
const builderLeftovers = parsedOptions['--'] || [];
leftoverMap.set(name, { optionDefs, parsedOptions });
potentialProjectNames = new Set(builderLeftovers.filter((x) => potentialProjectNames.has(x)));
}
if (potentialProjectNames.size === 1) {
projectName = [...potentialProjectNames][0];
// remove the project name from the leftovers
const optionInfo = leftoverMap.get(projectName);
if (optionInfo) {
const locations = [];
let i = 0;
while (i < commandLeftovers.length) {
i = commandLeftovers.indexOf(projectName, i + 1);
if (i === -1) {
break;
}
locations.push(i);
}
delete optionInfo.parsedOptions['--'];
for (const location of locations) {
const tempLeftovers = [...commandLeftovers];
tempLeftovers.splice(location, 1);
const tempArgs = (0, parser_1.parseArguments)([...tempLeftovers], optionInfo.optionDefs);
delete tempArgs['--'];
if (JSON.stringify(optionInfo.parsedOptions) === JSON.stringify(tempArgs)) {
options['--'] = tempLeftovers;
break;
}
}
}
}
if (!projectName && this.multiTarget && builderNames.size > 1) {
this.logger.fatal(core_1.tags.oneLine `
Architect commands with command line overrides cannot target different builders. The
'${this.target}' target would run on projects ${targetProjectNames.join()} which have the
following builders: ${'\n ' + [...builderNames].join('\n ')}
`);
return 1;
}
}
if (!projectName && !this.multiTarget) {
const defaultProjectName = this.workspace.extensions['defaultProject'];
if (targetProjectNames.length === 1) {
projectName = targetProjectNames[0];
}
else if (defaultProjectName && targetProjectNames.includes(defaultProjectName)) {
projectName = defaultProjectName;
}
else if (options.help) {
// This is a special case where we just return.
return;
}
else {
this.logger.fatal(this.missingTargetError || 'Cannot determine project or target for command.');
return 1;
}
}
options.project = projectName;
const builderConf = await this._architectHost.getBuilderNameForTarget({
project: projectName || (targetProjectNames.length > 0 ? targetProjectNames[0] : ''),
target: this.target,
});
let builderDesc;
try {
builderDesc = await this._architectHost.resolveBuilder(builderConf);
}
catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
await this.warnOnMissingNodeModules(this.workspace.basePath);
this.logger.fatal(`Could not find the '${builderConf}' builder's node package.`);
return 1;
}
throw e;
}
this.description.options.push(...(await (0, json_schema_1.parseJsonSchemaToOptions)(this._registry, builderDesc.optionSchema)));
// Update options to remove analytics from options if the builder isn't safelisted.
for (const o of this.description.options) {
if (o.userAnalytics && !(0, analytics_1.isPackageNameSafeForAnalytics)(builderConf)) {
o.userAnalytics = undefined;
}
}
}
async warnOnMissingNodeModules(basePath) {
// Check for a `node_modules` directory (npm, yarn non-PnP, etc.)
if ((0, fs_1.existsSync)(path.resolve(basePath, 'node_modules'))) {
return;
}
// Check for yarn PnP files
if ((0, fs_1.existsSync)(path.resolve(basePath, '.pnp.js')) ||
(0, fs_1.existsSync)(path.resolve(basePath, '.pnp.cjs')) ||
(0, fs_1.existsSync)(path.resolve(basePath, '.pnp.mjs'))) {
return;
}
const packageManager = await (0, package_manager_1.getPackageManager)(basePath);
let installSuggestion = 'Try installing with ';
switch (packageManager) {
case 'npm':
installSuggestion += `'npm install'`;
break;
case 'yarn':
installSuggestion += `'yarn'`;
break;
default:
installSuggestion += `the project's package manager`;
break;
}
this.logger.warn(`Node packages may not be installed. ${installSuggestion}.`);
}
async run(options) {
return await this.runArchitectTarget(options);
}
async runSingleTarget(target, targetOptions) {
// We need to build the builderSpec twice because architect does not understand
// overrides separately (getting the configuration builds the whole project, including
// overrides).
const builderConf = await this._architectHost.getBuilderNameForTarget(target);
let builderDesc;
try {
builderDesc = await this._architectHost.resolveBuilder(builderConf);
}
catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await this.warnOnMissingNodeModules(this.workspace.basePath);
this.logger.fatal(`Could not find the '${builderConf}' builder's node package.`);
return 1;
}
throw e;
}
const targetOptionArray = await (0, json_schema_1.parseJsonSchemaToOptions)(this._registry, builderDesc.optionSchema);
const overrides = (0, parser_1.parseArguments)(targetOptions, targetOptionArray, this.logger);
const allowAdditionalProperties = typeof builderDesc.optionSchema === 'object' && builderDesc.optionSchema.additionalProperties;
if (overrides['--'] && !allowAdditionalProperties) {
(overrides['--'] || []).forEach((additional) => {
this.logger.fatal(`Unknown option: '${additional.split(/=/)[0]}'`);
});
return 1;
}
await this.reportAnalytics([this.description.name], {
...(await this._architectHost.getOptionsForTarget(target)),
...overrides,
});
const run = await this._architect.scheduleTarget(target, overrides, {
logger: this.logger,
analytics: (0, analytics_1.isPackageNameSafeForAnalytics)(builderConf) ? this.analytics : undefined,
});
const { error, success } = await run.output.toPromise();
await run.stop();
if (error) {
this.logger.error(error);
}
return success ? 0 : 1;
}
async runArchitectTarget(options) {
var _a;
const extra = options['--'] || [];
try {
const targetSpec = this._makeTargetSpecifier(options);
if (!targetSpec.project && this.target) {
// This runs each target sequentially.
// Running them in parallel would jumble the log messages.
let result = 0;
for (const project of this.getProjectNamesByTarget(this.target)) {
result |= await this.runSingleTarget({ ...targetSpec, project }, extra);
}
return result;
}
else {
return await this.runSingleTarget(targetSpec, extra);
}
}
catch (e) {
if (e instanceof core_1.schema.SchemaValidationException) {
const newErrors = [];
for (const schemaError of e.errors) {
if (schemaError.keyword === 'additionalProperties') {
const unknownProperty = (_a = schemaError.params) === null || _a === void 0 ? void 0 : _a.additionalProperty;
if (unknownProperty in options) {
const dashes = unknownProperty.length === 1 ? '-' : '--';
this.logger.fatal(`Unknown option: '${dashes}${unknownProperty}'`);
continue;
}
}
newErrors.push(schemaError);
}
if (newErrors.length > 0) {
this.logger.error(new core_1.schema.SchemaValidationException(newErrors).message);
}
return 1;
}
else {
throw e;
}
}
}
getProjectNamesByTarget(targetName) {
const allProjectsForTargetName = [];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
for (const [name, project] of this.workspace.projects) {
if (project.targets.has(targetName)) {
allProjectsForTargetName.push(name);
}
}
if (this.multiTarget) {
// For multi target commands, we always list all projects that have the target.
return allProjectsForTargetName;
}
else {
// For single target commands, we try the default project first,
// then the full list if it has a single project, then error out.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const maybeDefaultProject = this.workspace.extensions['defaultProject'];
if (maybeDefaultProject && allProjectsForTargetName.includes(maybeDefaultProject)) {
return [maybeDefaultProject];
}
if (allProjectsForTargetName.length === 1) {
return allProjectsForTargetName;
}
throw new Error(`Could not determine a single project for the '${targetName}' target.`);
}
}
_makeTargetSpecifier(commandOptions) {
var _a, _b, _c;
let project, target, configuration;
if (commandOptions.target) {
[project, target, configuration] = commandOptions.target.split(':');
if (commandOptions.configuration) {
configuration = commandOptions.configuration;
}
}
else {
project = commandOptions.project;
target = this.target;
if (commandOptions.prod) {
const defaultConfig = project &&
target &&
((_c = (_b = (_a = this.workspace) === null || _a === void 0 ? void 0 : _a.projects.get(project)) === null || _b === void 0 ? void 0 : _b.targets.get(target)) === null || _c === void 0 ? void 0 : _c.defaultConfiguration);
this.logger.warn(defaultConfig === 'production'
? 'Option "--prod" is deprecated: No need to use this option as this builder defaults to configuration "production".'
: 'Option "--prod" is deprecated: Use "--configuration production" instead.');
// The --prod flag will always be the first configuration, available to be overwritten
// by following configurations.
configuration = 'production';
}
if (commandOptions.configuration) {
configuration = `${configuration ? `${configuration},` : ''}${commandOptions.configuration}`;
}
}
if (!project) {
project = '';
}
if (!target) {
target = '';
}
return {
project,
configuration: configuration || '',
target,
};
}
}
exports.ArchitectCommand = ArchitectCommand;
|