File: //lib/node_modules/@angular/cli/src/command-builder/schematics-command-module.js
"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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchematicsCommandModule = exports.DEFAULT_SCHEMATICS_COLLECTION = void 0;
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const tools_1 = require("@angular-devkit/schematics/tools");
const path_1 = require("path");
const analytics_1 = require("../analytics/analytics");
const analytics_parameters_1 = require("../analytics/analytics-parameters");
const config_1 = require("../utilities/config");
const error_1 = require("../utilities/error");
const load_esm_1 = require("../utilities/load-esm");
const memoize_1 = require("../utilities/memoize");
const tty_1 = require("../utilities/tty");
const command_module_1 = require("./command-module");
const json_schema_1 = require("./utilities/json-schema");
const schematic_engine_host_1 = require("./utilities/schematic-engine-host");
const schematic_workflow_1 = require("./utilities/schematic-workflow");
exports.DEFAULT_SCHEMATICS_COLLECTION = '@schematics/angular';
class SchematicsCommandModule extends command_module_1.CommandModule {
scope = command_module_1.CommandScope.In;
allowPrivateSchematics = false;
async builder(argv) {
return argv
.option('interactive', {
describe: 'Enable interactive input prompts.',
type: 'boolean',
default: true,
})
.option('dry-run', {
describe: 'Run through and reports activity without writing out results.',
type: 'boolean',
alias: ['d'],
default: false,
})
.option('defaults', {
describe: 'Disable interactive input prompts for options with a default.',
type: 'boolean',
default: false,
})
.option('force', {
describe: 'Force overwriting of existing files.',
type: 'boolean',
default: false,
})
.strict();
}
/** Get schematic schema options.*/
async getSchematicOptions(collection, schematicName, workflow) {
const schematic = collection.createSchematic(schematicName, true);
const { schemaJson } = schematic.description;
if (!schemaJson) {
return [];
}
return (0, json_schema_1.parseJsonSchemaToOptions)(workflow.registry, schemaJson);
}
getOrCreateWorkflowForBuilder(collectionName) {
return new tools_1.NodeWorkflow(this.context.root, {
resolvePaths: this.getResolvePaths(collectionName),
engineHostCreator: (options) => new schematic_engine_host_1.SchematicEngineHost(options.resolvePaths),
});
}
async getOrCreateWorkflowForExecution(collectionName, options) {
const { logger, root, packageManager } = this.context;
const { force, dryRun, packageRegistry } = options;
const workflow = new tools_1.NodeWorkflow(root, {
force,
dryRun,
packageManager: packageManager.name,
// A schema registry is required to allow customizing addUndefinedDefaults
registry: new core_1.schema.CoreSchemaRegistry(schematics_1.formats.standardFormats),
packageRegistry,
resolvePaths: this.getResolvePaths(collectionName),
schemaValidation: true,
optionTransforms: [
// Add configuration file defaults
async (schematic, current) => {
const projectName = typeof current?.project === 'string' ? current.project : this.getProjectName();
return {
...(await (0, config_1.getSchematicDefaults)(schematic.collection.name, schematic.name, projectName)),
...current,
};
},
],
engineHostCreator: (options) => new schematic_engine_host_1.SchematicEngineHost(options.resolvePaths),
});
workflow.registry.addPostTransform(core_1.schema.transforms.addUndefinedDefaults);
workflow.registry.useXDeprecatedProvider((msg) => logger.warn(msg));
workflow.registry.addSmartDefaultProvider('projectName', () => this.getProjectName());
const workingDir = (0, core_1.normalize)((0, path_1.relative)(this.context.root, process.cwd()));
workflow.registry.addSmartDefaultProvider('workingDirectory', () => workingDir === '' ? undefined : workingDir);
let shouldReportAnalytics = true;
workflow.engineHost.registerOptionsTransform(async (schematic, options) => {
// Report analytics
if (shouldReportAnalytics) {
shouldReportAnalytics = false;
const { collection: { name: collectionName }, name: schematicName, } = schematic;
const analytics = (0, analytics_1.isPackageNameSafeForAnalytics)(collectionName)
? await this.getAnalytics()
: undefined;
analytics?.reportSchematicRunEvent({
[analytics_parameters_1.EventCustomDimension.SchematicCollectionName]: collectionName,
[analytics_parameters_1.EventCustomDimension.SchematicName]: schematicName,
...this.getAnalyticsParameters(options),
});
}
return options;
});
if (options.interactive !== false && (0, tty_1.isTTY)()) {
workflow.registry.usePromptProvider(async (definitions) => {
const questions = definitions
.filter((definition) => !options.defaults || definition.default === undefined)
.map((definition) => {
const question = {
name: definition.id,
message: definition.message,
default: definition.default,
};
const validator = definition.validator;
if (validator) {
question.validate = (input) => validator(input);
// Filter allows transformation of the value prior to validation
question.filter = async (input) => {
for (const type of definition.propertyTypes) {
let value;
switch (type) {
case 'string':
value = String(input);
break;
case 'integer':
case 'number':
value = Number(input);
break;
default:
value = input;
break;
}
// Can be a string if validation fails
const isValid = (await validator(value)) === true;
if (isValid) {
return value;
}
}
return input;
};
}
switch (definition.type) {
case 'confirmation':
question.type = 'confirm';
break;
case 'list':
question.type = definition.multiselect ? 'checkbox' : 'list';
question.choices = definition.items?.map((item) => {
return typeof item == 'string'
? item
: {
name: item.label,
value: item.value,
};
});
break;
default:
question.type = definition.type;
break;
}
return question;
});
if (questions.length) {
const { default: inquirer } = await (0, load_esm_1.loadEsmModule)('inquirer');
return inquirer.prompt(questions);
}
else {
return {};
}
});
}
return workflow;
}
async getSchematicCollections() {
// Resolve relative collections from the location of `angular.json`
const resolveRelativeCollection = (collectionName) => collectionName.charAt(0) === '.'
? (0, path_1.resolve)(this.context.root, collectionName)
: collectionName;
const getSchematicCollections = (configSection) => {
if (!configSection) {
return undefined;
}
const { schematicCollections } = configSection;
if (Array.isArray(schematicCollections)) {
return new Set(schematicCollections.map((c) => resolveRelativeCollection(c)));
}
return undefined;
};
const { workspace, globalConfiguration } = this.context;
if (workspace) {
const project = (0, config_1.getProjectByCwd)(workspace);
if (project) {
const value = getSchematicCollections(workspace.getProjectCli(project));
if (value) {
return value;
}
}
}
const value = getSchematicCollections(workspace?.getCli()) ??
getSchematicCollections(globalConfiguration.getCli());
if (value) {
return value;
}
return new Set([exports.DEFAULT_SCHEMATICS_COLLECTION]);
}
parseSchematicInfo(schematic) {
if (schematic?.includes(':')) {
const [collectionName, schematicName] = schematic.split(':', 2);
return [collectionName, schematicName];
}
return [undefined, schematic];
}
async runSchematic(options) {
const { logger } = this.context;
const { schematicOptions, executionOptions, collectionName, schematicName } = options;
const workflow = await this.getOrCreateWorkflowForExecution(collectionName, executionOptions);
if (!schematicName) {
throw new Error('schematicName cannot be undefined.');
}
const { unsubscribe, files } = (0, schematic_workflow_1.subscribeToWorkflow)(workflow, logger);
try {
await workflow
.execute({
collection: collectionName,
schematic: schematicName,
options: schematicOptions,
logger,
allowPrivate: this.allowPrivateSchematics,
})
.toPromise();
if (!files.size) {
logger.info('Nothing to be done.');
}
if (executionOptions.dryRun) {
logger.warn(`\nNOTE: The "--dry-run" option means no changes were made.`);
}
}
catch (err) {
// In case the workflow was not successful, show an appropriate error message.
if (err instanceof schematics_1.UnsuccessfulWorkflowExecution) {
// "See above" because we already printed the error.
logger.fatal('The Schematic workflow failed. See above.');
}
else {
(0, error_1.assertIsError)(err);
logger.fatal(err.message);
}
return 1;
}
finally {
unsubscribe();
}
return 0;
}
getProjectName() {
const { workspace, logger } = this.context;
if (!workspace) {
return undefined;
}
const projectName = (0, config_1.getProjectByCwd)(workspace);
if (projectName) {
return projectName;
}
return undefined;
}
getResolvePaths(collectionName) {
const { workspace, root } = this.context;
return workspace
? // Workspace
collectionName === exports.DEFAULT_SCHEMATICS_COLLECTION
? // Favor __dirname for @schematics/angular to use the build-in version
[__dirname, process.cwd(), root]
: [process.cwd(), root, __dirname]
: // Global
[__dirname, process.cwd()];
}
}
exports.SchematicsCommandModule = SchematicsCommandModule;
__decorate([
memoize_1.memoize,
__metadata("design:type", Function),
__metadata("design:paramtypes", [String]),
__metadata("design:returntype", tools_1.NodeWorkflow)
], SchematicsCommandModule.prototype, "getOrCreateWorkflowForBuilder", null);
__decorate([
memoize_1.memoize,
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, Object]),
__metadata("design:returntype", Promise)
], SchematicsCommandModule.prototype, "getOrCreateWorkflowForExecution", null);
__decorate([
memoize_1.memoize,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], SchematicsCommandModule.prototype, "getSchematicCollections", null);