UNPKG

5.17 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 });
12exports.GenerateAction = void 0;
13const chalk = require("chalk");
14const get_value_or_default_1 = require("../lib/compiler/helpers/get-value-or-default");
15const schematics_1 = require("../lib/schematics");
16const ui_1 = require("../lib/ui");
17const load_configuration_1 = require("../lib/utils/load-configuration");
18const project_utils_1 = require("../lib/utils/project-utils");
19const abstract_action_1 = require("./abstract.action");
20class 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}
27exports.GenerateAction = GenerateAction;
28const generateFiles = (inputs) => __awaiter(void 0, void 0, void 0, function* () {
29 const configuration = yield (0, 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 || schematics_1.Collection.NESTJS);
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 ? (0, 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 = (0, 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 ((0, 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 = (0, project_utils_1.moveDefaultProjectToStart)(configuration, defaultProjectName, defaultLabel);
58 const answers = yield (0, 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 = (0, 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 const keepInputName = input.options
89 ? 'keepInputNameFormat' in input.options
90 : false;
91 options.push(new schematics_1.SchematicOption(input.name, input.value, keepInputName));
92 }
93 });
94 return options;
95};