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.AddAction = void 0;
|
13 | const chalk = require("chalk");
|
14 | const get_value_or_default_1 = require("../lib/compiler/helpers/get-value-or-default");
|
15 | const package_managers_1 = require("../lib/package-managers");
|
16 | const schematics_1 = require("../lib/schematics");
|
17 | const ui_1 = require("../lib/ui");
|
18 | const load_configuration_1 = require("../lib/utils/load-configuration");
|
19 | const project_utils_1 = require("../lib/utils/project-utils");
|
20 | const abstract_action_1 = require("./abstract.action");
|
21 | const schematicName = 'nest-add';
|
22 | class AddAction extends abstract_action_1.AbstractAction {
|
23 | handle(inputs, options, extraFlags) {
|
24 | return __awaiter(this, void 0, void 0, function* () {
|
25 | const libraryName = this.getLibraryName(inputs);
|
26 | const packageName = this.getPackageName(libraryName);
|
27 | const collectionName = this.getCollectionName(libraryName, packageName);
|
28 | const tagName = this.getTagName(packageName);
|
29 | const packageInstallSuccess = yield this.installPackage(collectionName, tagName);
|
30 | if (packageInstallSuccess) {
|
31 | const sourceRootOption = yield this.getSourceRoot(inputs.concat(options));
|
32 | options.push(sourceRootOption);
|
33 | yield this.addLibrary(collectionName, options, extraFlags);
|
34 | }
|
35 | else {
|
36 | console.error(chalk.red(ui_1.MESSAGES.LIBRARY_INSTALLATION_FAILED_BAD_PACKAGE(libraryName)));
|
37 | throw new Error(ui_1.MESSAGES.LIBRARY_INSTALLATION_FAILED_BAD_PACKAGE(libraryName));
|
38 | }
|
39 | });
|
40 | }
|
41 | getSourceRoot(inputs) {
|
42 | return __awaiter(this, void 0, void 0, function* () {
|
43 | const configuration = yield (0, load_configuration_1.loadConfiguration)();
|
44 | const configurationProjects = configuration.projects;
|
45 | const appName = inputs.find((option) => option.name === 'project')
|
46 | .value;
|
47 | let sourceRoot = appName
|
48 | ? (0, get_value_or_default_1.getValueOrDefault)(configuration, 'sourceRoot', appName)
|
49 | : configuration.sourceRoot;
|
50 | const shouldAsk = (0, project_utils_1.shouldAskForProject)(schematicName, configurationProjects, appName);
|
51 | if (shouldAsk) {
|
52 | const defaultLabel = ' [ Default ]';
|
53 | let defaultProjectName = configuration.sourceRoot + defaultLabel;
|
54 | for (const property in configurationProjects) {
|
55 | if (configurationProjects[property].sourceRoot ===
|
56 | configuration.sourceRoot) {
|
57 | defaultProjectName = property + defaultLabel;
|
58 | break;
|
59 | }
|
60 | }
|
61 | const projects = (0, project_utils_1.moveDefaultProjectToStart)(configuration, defaultProjectName, defaultLabel);
|
62 | const answers = yield (0, project_utils_1.askForProjectName)(ui_1.MESSAGES.LIBRARY_PROJECT_SELECTION_QUESTION, projects);
|
63 | const project = answers.appName.replace(defaultLabel, '');
|
64 | if (project !== configuration.sourceRoot) {
|
65 | sourceRoot = configurationProjects[project].sourceRoot;
|
66 | }
|
67 | }
|
68 | return { name: 'sourceRoot', value: sourceRoot };
|
69 | });
|
70 | }
|
71 | installPackage(collectionName, tagName) {
|
72 | return __awaiter(this, void 0, void 0, function* () {
|
73 | const manager = yield package_managers_1.PackageManagerFactory.find();
|
74 | tagName = tagName || 'latest';
|
75 | let installResult = false;
|
76 | try {
|
77 | installResult = yield manager.addProduction([collectionName], tagName);
|
78 | }
|
79 | catch (error) {
|
80 | if (error && error.message) {
|
81 | console.error(chalk.red(error.message));
|
82 | }
|
83 | }
|
84 | return installResult;
|
85 | });
|
86 | }
|
87 | addLibrary(collectionName, options, extraFlags) {
|
88 | return __awaiter(this, void 0, void 0, function* () {
|
89 | console.info(ui_1.MESSAGES.LIBRARY_INSTALLATION_STARTS);
|
90 | const schematicOptions = [];
|
91 | schematicOptions.push(new schematics_1.SchematicOption('sourceRoot', options.find((option) => option.name === 'sourceRoot').value));
|
92 | const extraFlagsString = extraFlags ? extraFlags.join(' ') : undefined;
|
93 | try {
|
94 | const collection = schematics_1.CollectionFactory.create(collectionName);
|
95 | yield collection.execute(schematicName, schematicOptions, extraFlagsString);
|
96 | }
|
97 | catch (error) {
|
98 | if (error && error.message) {
|
99 | console.error(chalk.red(error.message));
|
100 | return Promise.reject();
|
101 | }
|
102 | }
|
103 | });
|
104 | }
|
105 | getLibraryName(inputs) {
|
106 | const libraryInput = inputs.find((input) => input.name === 'library');
|
107 | if (!libraryInput) {
|
108 | throw new Error('No library found in command input');
|
109 | }
|
110 | return libraryInput.value;
|
111 | }
|
112 | getPackageName(library) {
|
113 | return library.startsWith('@')
|
114 | ? library.split('/', 2).join('/')
|
115 | : library.split('/', 1)[0];
|
116 | }
|
117 | getCollectionName(library, packageName) {
|
118 | return ((packageName.startsWith('@')
|
119 | ? packageName.split('@', 2).join('@')
|
120 | : packageName.split('@', 1).join('@')) +
|
121 | library.slice(packageName.length));
|
122 | }
|
123 | getTagName(packageName) {
|
124 | return packageName.startsWith('@')
|
125 | ? packageName.split('@', 3)[2]
|
126 | : packageName.split('@', 2)[1];
|
127 | }
|
128 | }
|
129 | exports.AddAction = AddAction;
|