[6a3a178] | 1 | "use strict";
|
---|
| 2 | /**
|
---|
| 3 | * @license
|
---|
| 4 | * Copyright Google LLC All Rights Reserved.
|
---|
| 5 | *
|
---|
| 6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 7 | * found in the LICENSE file at https://angular.io/license
|
---|
| 8 | */
|
---|
| 9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 10 | exports.ArchitectCommand = void 0;
|
---|
| 11 | const architect_1 = require("@angular-devkit/architect");
|
---|
| 12 | const node_1 = require("@angular-devkit/architect/node");
|
---|
| 13 | const core_1 = require("@angular-devkit/core");
|
---|
| 14 | const json_schema_1 = require("../utilities/json-schema");
|
---|
| 15 | const analytics_1 = require("./analytics");
|
---|
| 16 | const command_1 = require("./command");
|
---|
| 17 | const parser_1 = require("./parser");
|
---|
| 18 | class ArchitectCommand extends command_1.Command {
|
---|
| 19 | constructor() {
|
---|
| 20 | super(...arguments);
|
---|
| 21 | this.useReportAnalytics = false;
|
---|
| 22 | // If this command supports running multiple targets.
|
---|
| 23 | this.multiTarget = false;
|
---|
| 24 | }
|
---|
| 25 | async initialize(options) {
|
---|
| 26 | this._registry = new core_1.json.schema.CoreSchemaRegistry();
|
---|
| 27 | this._registry.addPostTransform(core_1.json.schema.transforms.addUndefinedDefaults);
|
---|
| 28 | this._registry.useXDeprecatedProvider((msg) => this.logger.warn(msg));
|
---|
| 29 | if (!this.workspace) {
|
---|
| 30 | this.logger.fatal('A workspace is required for this command.');
|
---|
| 31 | return 1;
|
---|
| 32 | }
|
---|
| 33 | this._architectHost = new node_1.WorkspaceNodeModulesArchitectHost(this.workspace, this.workspace.basePath);
|
---|
| 34 | this._architect = new architect_1.Architect(this._architectHost, this._registry);
|
---|
| 35 | if (!this.target) {
|
---|
| 36 | if (options.help) {
|
---|
| 37 | // This is a special case where we just return.
|
---|
| 38 | return;
|
---|
| 39 | }
|
---|
| 40 | const specifier = this._makeTargetSpecifier(options);
|
---|
| 41 | if (!specifier.project || !specifier.target) {
|
---|
| 42 | this.logger.fatal('Cannot determine project or target for command.');
|
---|
| 43 | return 1;
|
---|
| 44 | }
|
---|
| 45 | return;
|
---|
| 46 | }
|
---|
| 47 | let projectName = options.project;
|
---|
| 48 | if (projectName && !this.workspace.projects.has(projectName)) {
|
---|
| 49 | this.logger.fatal(`Project '${projectName}' does not exist.`);
|
---|
| 50 | return 1;
|
---|
| 51 | }
|
---|
| 52 | const commandLeftovers = options['--'];
|
---|
| 53 | const targetProjectNames = [];
|
---|
| 54 | for (const [name, project] of this.workspace.projects) {
|
---|
| 55 | if (project.targets.has(this.target)) {
|
---|
| 56 | targetProjectNames.push(name);
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | if (targetProjectNames.length === 0) {
|
---|
| 60 | this.logger.fatal(this.missingTargetError || `No projects support the '${this.target}' target.`);
|
---|
| 61 | return 1;
|
---|
| 62 | }
|
---|
| 63 | if (projectName && !targetProjectNames.includes(projectName)) {
|
---|
| 64 | this.logger.fatal(this.missingTargetError ||
|
---|
| 65 | `Project '${projectName}' does not support the '${this.target}' target.`);
|
---|
| 66 | return 1;
|
---|
| 67 | }
|
---|
| 68 | if (!projectName && commandLeftovers && commandLeftovers.length > 0) {
|
---|
| 69 | const builderNames = new Set();
|
---|
| 70 | const leftoverMap = new Map();
|
---|
| 71 | let potentialProjectNames = new Set(targetProjectNames);
|
---|
| 72 | for (const name of targetProjectNames) {
|
---|
| 73 | const builderName = await this._architectHost.getBuilderNameForTarget({
|
---|
| 74 | project: name,
|
---|
| 75 | target: this.target,
|
---|
| 76 | });
|
---|
| 77 | if (this.multiTarget) {
|
---|
| 78 | builderNames.add(builderName);
|
---|
| 79 | }
|
---|
| 80 | const builderDesc = await this._architectHost.resolveBuilder(builderName);
|
---|
| 81 | const optionDefs = await json_schema_1.parseJsonSchemaToOptions(this._registry, builderDesc.optionSchema);
|
---|
| 82 | const parsedOptions = parser_1.parseArguments([...commandLeftovers], optionDefs);
|
---|
| 83 | const builderLeftovers = parsedOptions['--'] || [];
|
---|
| 84 | leftoverMap.set(name, { optionDefs, parsedOptions });
|
---|
| 85 | potentialProjectNames = new Set(builderLeftovers.filter((x) => potentialProjectNames.has(x)));
|
---|
| 86 | }
|
---|
| 87 | if (potentialProjectNames.size === 1) {
|
---|
| 88 | projectName = [...potentialProjectNames][0];
|
---|
| 89 | // remove the project name from the leftovers
|
---|
| 90 | const optionInfo = leftoverMap.get(projectName);
|
---|
| 91 | if (optionInfo) {
|
---|
| 92 | const locations = [];
|
---|
| 93 | let i = 0;
|
---|
| 94 | while (i < commandLeftovers.length) {
|
---|
| 95 | i = commandLeftovers.indexOf(projectName, i + 1);
|
---|
| 96 | if (i === -1) {
|
---|
| 97 | break;
|
---|
| 98 | }
|
---|
| 99 | locations.push(i);
|
---|
| 100 | }
|
---|
| 101 | delete optionInfo.parsedOptions['--'];
|
---|
| 102 | for (const location of locations) {
|
---|
| 103 | const tempLeftovers = [...commandLeftovers];
|
---|
| 104 | tempLeftovers.splice(location, 1);
|
---|
| 105 | const tempArgs = parser_1.parseArguments([...tempLeftovers], optionInfo.optionDefs);
|
---|
| 106 | delete tempArgs['--'];
|
---|
| 107 | if (JSON.stringify(optionInfo.parsedOptions) === JSON.stringify(tempArgs)) {
|
---|
| 108 | options['--'] = tempLeftovers;
|
---|
| 109 | break;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | if (!projectName && this.multiTarget && builderNames.size > 1) {
|
---|
| 115 | this.logger.fatal(core_1.tags.oneLine `
|
---|
| 116 | Architect commands with command line overrides cannot target different builders. The
|
---|
| 117 | '${this.target}' target would run on projects ${targetProjectNames.join()} which have the
|
---|
| 118 | following builders: ${'\n ' + [...builderNames].join('\n ')}
|
---|
| 119 | `);
|
---|
| 120 | return 1;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | if (!projectName && !this.multiTarget) {
|
---|
| 124 | const defaultProjectName = this.workspace.extensions['defaultProject'];
|
---|
| 125 | if (targetProjectNames.length === 1) {
|
---|
| 126 | projectName = targetProjectNames[0];
|
---|
| 127 | }
|
---|
| 128 | else if (defaultProjectName && targetProjectNames.includes(defaultProjectName)) {
|
---|
| 129 | projectName = defaultProjectName;
|
---|
| 130 | }
|
---|
| 131 | else if (options.help) {
|
---|
| 132 | // This is a special case where we just return.
|
---|
| 133 | return;
|
---|
| 134 | }
|
---|
| 135 | else {
|
---|
| 136 | this.logger.fatal(this.missingTargetError || 'Cannot determine project or target for command.');
|
---|
| 137 | return 1;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | options.project = projectName;
|
---|
| 141 | const builderConf = await this._architectHost.getBuilderNameForTarget({
|
---|
| 142 | project: projectName || (targetProjectNames.length > 0 ? targetProjectNames[0] : ''),
|
---|
| 143 | target: this.target,
|
---|
| 144 | });
|
---|
| 145 | const builderDesc = await this._architectHost.resolveBuilder(builderConf);
|
---|
| 146 | this.description.options.push(...(await json_schema_1.parseJsonSchemaToOptions(this._registry, builderDesc.optionSchema)));
|
---|
| 147 | // Update options to remove analytics from options if the builder isn't safelisted.
|
---|
| 148 | for (const o of this.description.options) {
|
---|
| 149 | if (o.userAnalytics && !analytics_1.isPackageNameSafeForAnalytics(builderConf)) {
|
---|
| 150 | o.userAnalytics = undefined;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | async run(options) {
|
---|
| 155 | return await this.runArchitectTarget(options);
|
---|
| 156 | }
|
---|
| 157 | async runSingleTarget(target, targetOptions) {
|
---|
| 158 | // We need to build the builderSpec twice because architect does not understand
|
---|
| 159 | // overrides separately (getting the configuration builds the whole project, including
|
---|
| 160 | // overrides).
|
---|
| 161 | const builderConf = await this._architectHost.getBuilderNameForTarget(target);
|
---|
| 162 | const builderDesc = await this._architectHost.resolveBuilder(builderConf);
|
---|
| 163 | const targetOptionArray = await json_schema_1.parseJsonSchemaToOptions(this._registry, builderDesc.optionSchema);
|
---|
| 164 | const overrides = parser_1.parseArguments(targetOptions, targetOptionArray, this.logger);
|
---|
| 165 | const allowAdditionalProperties = typeof builderDesc.optionSchema === 'object' && builderDesc.optionSchema.additionalProperties;
|
---|
| 166 | if (overrides['--'] && !allowAdditionalProperties) {
|
---|
| 167 | (overrides['--'] || []).forEach((additional) => {
|
---|
| 168 | this.logger.fatal(`Unknown option: '${additional.split(/=/)[0]}'`);
|
---|
| 169 | });
|
---|
| 170 | return 1;
|
---|
| 171 | }
|
---|
| 172 | await this.reportAnalytics([this.description.name], {
|
---|
| 173 | ...(await this._architectHost.getOptionsForTarget(target)),
|
---|
| 174 | ...overrides,
|
---|
| 175 | });
|
---|
| 176 | const run = await this._architect.scheduleTarget(target, overrides, {
|
---|
| 177 | logger: this.logger,
|
---|
| 178 | analytics: analytics_1.isPackageNameSafeForAnalytics(builderConf) ? this.analytics : undefined,
|
---|
| 179 | });
|
---|
| 180 | const { error, success } = await run.output.toPromise();
|
---|
| 181 | await run.stop();
|
---|
| 182 | if (error) {
|
---|
| 183 | this.logger.error(error);
|
---|
| 184 | }
|
---|
| 185 | return success ? 0 : 1;
|
---|
| 186 | }
|
---|
| 187 | async runArchitectTarget(options) {
|
---|
| 188 | var _a;
|
---|
| 189 | const extra = options['--'] || [];
|
---|
| 190 | try {
|
---|
| 191 | const targetSpec = this._makeTargetSpecifier(options);
|
---|
| 192 | if (!targetSpec.project && this.target) {
|
---|
| 193 | // This runs each target sequentially.
|
---|
| 194 | // Running them in parallel would jumble the log messages.
|
---|
| 195 | let result = 0;
|
---|
| 196 | for (const project of this.getProjectNamesByTarget(this.target)) {
|
---|
| 197 | result |= await this.runSingleTarget({ ...targetSpec, project }, extra);
|
---|
| 198 | }
|
---|
| 199 | return result;
|
---|
| 200 | }
|
---|
| 201 | else {
|
---|
| 202 | return await this.runSingleTarget(targetSpec, extra);
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | catch (e) {
|
---|
| 206 | if (e instanceof core_1.schema.SchemaValidationException) {
|
---|
| 207 | const newErrors = [];
|
---|
| 208 | for (const schemaError of e.errors) {
|
---|
| 209 | if (schemaError.keyword === 'additionalProperties') {
|
---|
| 210 | const unknownProperty = (_a = schemaError.params) === null || _a === void 0 ? void 0 : _a.additionalProperty;
|
---|
| 211 | if (unknownProperty in options) {
|
---|
| 212 | const dashes = unknownProperty.length === 1 ? '-' : '--';
|
---|
| 213 | this.logger.fatal(`Unknown option: '${dashes}${unknownProperty}'`);
|
---|
| 214 | continue;
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | newErrors.push(schemaError);
|
---|
| 218 | }
|
---|
| 219 | if (newErrors.length > 0) {
|
---|
| 220 | this.logger.error(new core_1.schema.SchemaValidationException(newErrors).message);
|
---|
| 221 | }
|
---|
| 222 | return 1;
|
---|
| 223 | }
|
---|
| 224 | else {
|
---|
| 225 | throw e;
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | getProjectNamesByTarget(targetName) {
|
---|
| 230 | const allProjectsForTargetName = [];
|
---|
| 231 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
---|
| 232 | for (const [name, project] of this.workspace.projects) {
|
---|
| 233 | if (project.targets.has(targetName)) {
|
---|
| 234 | allProjectsForTargetName.push(name);
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 | if (this.multiTarget) {
|
---|
| 238 | // For multi target commands, we always list all projects that have the target.
|
---|
| 239 | return allProjectsForTargetName;
|
---|
| 240 | }
|
---|
| 241 | else {
|
---|
| 242 | // For single target commands, we try the default project first,
|
---|
| 243 | // then the full list if it has a single project, then error out.
|
---|
| 244 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
---|
| 245 | const maybeDefaultProject = this.workspace.extensions['defaultProject'];
|
---|
| 246 | if (maybeDefaultProject && allProjectsForTargetName.includes(maybeDefaultProject)) {
|
---|
| 247 | return [maybeDefaultProject];
|
---|
| 248 | }
|
---|
| 249 | if (allProjectsForTargetName.length === 1) {
|
---|
| 250 | return allProjectsForTargetName;
|
---|
| 251 | }
|
---|
| 252 | throw new Error(`Could not determine a single project for the '${targetName}' target.`);
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | _makeTargetSpecifier(commandOptions) {
|
---|
| 256 | var _a, _b, _c;
|
---|
| 257 | let project, target, configuration;
|
---|
| 258 | if (commandOptions.target) {
|
---|
| 259 | [project, target, configuration] = commandOptions.target.split(':');
|
---|
| 260 | if (commandOptions.configuration) {
|
---|
| 261 | configuration = commandOptions.configuration;
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 | else {
|
---|
| 265 | project = commandOptions.project;
|
---|
| 266 | target = this.target;
|
---|
| 267 | if (commandOptions.prod) {
|
---|
| 268 | const defaultConfig = project &&
|
---|
| 269 | target &&
|
---|
| 270 | ((_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);
|
---|
| 271 | this.logger.warn(defaultConfig === 'production'
|
---|
| 272 | ? 'Option "--prod" is deprecated: No need to use this option as this builder defaults to configuration "production".'
|
---|
| 273 | : 'Option "--prod" is deprecated: Use "--configuration production" instead.');
|
---|
| 274 | // The --prod flag will always be the first configuration, available to be overwritten
|
---|
| 275 | // by following configurations.
|
---|
| 276 | configuration = 'production';
|
---|
| 277 | }
|
---|
| 278 | if (commandOptions.configuration) {
|
---|
| 279 | configuration = `${configuration ? `${configuration},` : ''}${commandOptions.configuration}`;
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 | if (!project) {
|
---|
| 283 | project = '';
|
---|
| 284 | }
|
---|
| 285 | if (!target) {
|
---|
| 286 | target = '';
|
---|
| 287 | }
|
---|
| 288 | return {
|
---|
| 289 | project,
|
---|
| 290 | configuration: configuration || '',
|
---|
| 291 | target,
|
---|
| 292 | };
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 | exports.ArchitectCommand = ArchitectCommand;
|
---|