UNPKG

4.92 kBJavaScriptView Raw
1"use strict";
2var __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};
11Object.defineProperty(exports, "__esModule", { value: true });
12const chalk = require("chalk");
13const get_value_or_default_1 = require("../lib/compiler/helpers/get-value-or-default");
14const schematics_1 = require("../lib/schematics");
15const ui_1 = require("../lib/ui");
16const load_configuration_1 = require("../lib/utils/load-configuration");
17const project_utils_1 = require("../lib/utils/project-utils");
18const abstract_action_1 = require("./abstract.action");
19class GenerateAction extends abstract_action_1.AbstractAction {
20 handle(inputs, options) {
21 return __awaiter(this, void 0, void 0, function* () {
22 yield generateFiles(inputs.concat(options));
23 });
24 }
25}
26exports.GenerateAction = GenerateAction;
27const generateFiles = (inputs) => __awaiter(void 0, void 0, void 0, function* () {
28 const configuration = yield load_configuration_1.loadConfiguration();
29 const collectionOption = inputs.find(option => option.name === 'collection')
30 .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 // If you only add a `lib` we actually don't have monorepo: true BUT we do have "projects"
47 // Ensure we don't run for new app/libs schematics
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 // Only overwrite if the appName is not the default- as it has already been loaded above
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});
83const 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};