1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | exports.GenerateAction = void 0;
|
13 | const chalk = require("chalk");
|
14 | const get_value_or_default_1 = require("../lib/compiler/helpers/get-value-or-default");
|
15 | const schematics_1 = require("../lib/schematics");
|
16 | const ui_1 = require("../lib/ui");
|
17 | const load_configuration_1 = require("../lib/utils/load-configuration");
|
18 | const project_utils_1 = require("../lib/utils/project-utils");
|
19 | const abstract_action_1 = require("./abstract.action");
|
20 | class GenerateAction extends abstract_action_1.AbstractAction {
|
21 | handle(inputs, options) {
|
22 | return __awaiter(this, void 0, void 0, function* () {
|
23 | yield generateFiles(inputs.concat(options));
|
24 | });
|
25 | }
|
26 | }
|
27 | exports.GenerateAction = GenerateAction;
|
28 | const generateFiles = (inputs) => __awaiter(void 0, void 0, void 0, function* () {
|
29 | const configuration = yield load_configuration_1.loadConfiguration();
|
30 | const collectionOption = inputs.find((option) => option.name === 'collection').value;
|
31 | const schematic = inputs.find((option) => option.name === 'schematic')
|
32 | .value;
|
33 | const appName = inputs.find((option) => option.name === 'project')
|
34 | .value;
|
35 | const spec = inputs.find((option) => option.name === 'spec');
|
36 | const collection = schematics_1.CollectionFactory.create(collectionOption || configuration.collection);
|
37 | const schematicOptions = mapSchematicOptions(inputs);
|
38 | schematicOptions.push(new schematics_1.SchematicOption('language', configuration.language));
|
39 | const configurationProjects = configuration.projects;
|
40 | let sourceRoot = appName
|
41 | ? get_value_or_default_1.getValueOrDefault(configuration, 'sourceRoot', appName)
|
42 | : configuration.sourceRoot;
|
43 | const specValue = spec.value;
|
44 | const specOptions = spec.options;
|
45 | let generateSpec = project_utils_1.shouldGenerateSpec(configuration, schematic, appName, specValue, specOptions.passedAsInput);
|
46 |
|
47 |
|
48 | if (project_utils_1.shouldAskForProject(schematic, configurationProjects, appName)) {
|
49 | const defaultLabel = ' [ Default ]';
|
50 | let defaultProjectName = configuration.sourceRoot + defaultLabel;
|
51 | for (const property in configurationProjects) {
|
52 | if (configurationProjects[property].sourceRoot === configuration.sourceRoot) {
|
53 | defaultProjectName = property + defaultLabel;
|
54 | break;
|
55 | }
|
56 | }
|
57 | const projects = project_utils_1.moveDefaultProjectToStart(configuration, defaultProjectName, defaultLabel);
|
58 | const answers = yield project_utils_1.askForProjectName(ui_1.MESSAGES.PROJECT_SELECTION_QUESTION, projects);
|
59 | const project = answers.appName.replace(defaultLabel, '');
|
60 | if (project !== configuration.sourceRoot) {
|
61 | sourceRoot = configurationProjects[project].sourceRoot;
|
62 | }
|
63 | if (answers.appName !== defaultProjectName) {
|
64 |
|
65 | generateSpec = project_utils_1.shouldGenerateSpec(configuration, schematic, answers.appName, specValue, specOptions.passedAsInput);
|
66 | }
|
67 | }
|
68 | schematicOptions.push(new schematics_1.SchematicOption('sourceRoot', sourceRoot));
|
69 | schematicOptions.push(new schematics_1.SchematicOption('spec', generateSpec));
|
70 | try {
|
71 | const schematicInput = inputs.find((input) => input.name === 'schematic');
|
72 | if (!schematicInput) {
|
73 | throw new Error('Unable to find a schematic for this configuration');
|
74 | }
|
75 | yield collection.execute(schematicInput.value, schematicOptions);
|
76 | }
|
77 | catch (error) {
|
78 | if (error && error.message) {
|
79 | console.error(chalk.red(error.message));
|
80 | }
|
81 | }
|
82 | });
|
83 | const mapSchematicOptions = (inputs) => {
|
84 | const excludedInputNames = ['schematic', 'spec'];
|
85 | const options = [];
|
86 | inputs.forEach((input) => {
|
87 | if (!excludedInputNames.includes(input.name) && input.value !== undefined) {
|
88 | options.push(new schematics_1.SchematicOption(input.name, input.value));
|
89 | }
|
90 | });
|
91 | return options;
|
92 | };
|