UNPKG

3.69 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.GenerateCommand = void 0;
4const schematic_command_1 = require("../models/schematic-command");
5const color_1 = require("../utilities/color");
6const json_schema_1 = require("../utilities/json-schema");
7class GenerateCommand extends schematic_command_1.SchematicCommand {
8 async initialize(options) {
9 // Fill up the schematics property of the command description.
10 const [collectionName, schematicName] = await this.parseSchematicInfo(options);
11 this.collectionName = collectionName;
12 this.schematicName = schematicName;
13 await super.initialize(options);
14 const collection = this.getCollection(collectionName);
15 const subcommands = {};
16 const schematicNames = schematicName ? [schematicName] : collection.listSchematicNames();
17 // Sort as a courtesy for the user.
18 schematicNames.sort();
19 for (const name of schematicNames) {
20 const schematic = this.getSchematic(collection, name, true);
21 this.longSchematicName = schematic.description.name;
22 let subcommand;
23 if (schematic.description.schemaJson) {
24 subcommand = await json_schema_1.parseJsonSchemaToSubCommandDescription(name, schematic.description.path, this._workflow.registry, schematic.description.schemaJson);
25 }
26 else {
27 continue;
28 }
29 if ((await this.getDefaultSchematicCollection()) == collectionName) {
30 subcommands[name] = subcommand;
31 }
32 else {
33 subcommands[`${collectionName}:${name}`] = subcommand;
34 }
35 }
36 this.description.options.forEach(option => {
37 if (option.name == 'schematic') {
38 option.subcommands = subcommands;
39 }
40 });
41 }
42 async run(options) {
43 if (!this.schematicName || !this.collectionName) {
44 return this.printHelp();
45 }
46 return this.runSchematic({
47 collectionName: this.collectionName,
48 schematicName: this.schematicName,
49 schematicOptions: options['--'] || [],
50 debug: !!options.debug || false,
51 dryRun: !!options.dryRun || false,
52 force: !!options.force || false,
53 });
54 }
55 async reportAnalytics(paths, options) {
56 const [collectionName, schematicName] = await this.parseSchematicInfo(options);
57 if (!schematicName || !collectionName) {
58 return;
59 }
60 const escapedSchematicName = (this.longSchematicName || schematicName).replace(/\//g, '_');
61 return super.reportAnalytics(['generate', collectionName.replace(/\//g, '_'), escapedSchematicName], options);
62 }
63 async parseSchematicInfo(options) {
64 let collectionName = await this.getDefaultSchematicCollection();
65 let schematicName = options.schematic;
66 if (schematicName && schematicName.includes(':')) {
67 [collectionName, schematicName] = schematicName.split(':', 2);
68 }
69 return [collectionName, schematicName];
70 }
71 async printHelp() {
72 await super.printHelp();
73 this.logger.info('');
74 // Find the generate subcommand.
75 const subcommand = this.description.options.filter(x => x.subcommands)[0];
76 if (Object.keys((subcommand && subcommand.subcommands) || {}).length == 1) {
77 this.logger.info(`\nTo see help for a schematic run:`);
78 this.logger.info(color_1.colors.cyan(` ng generate <schematic> --help`));
79 }
80 return 0;
81 }
82}
83exports.GenerateCommand = GenerateCommand;