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.GenerateCommand = void 0;
|
13 | const chalk = require("chalk");
|
14 | const Table = require("cli-table3");
|
15 | const schematics_1 = require("../lib/schematics");
|
16 | const load_configuration_1 = require("../lib/utils/load-configuration");
|
17 | const abstract_command_1 = require("./abstract.command");
|
18 | class GenerateCommand extends abstract_command_1.AbstractCommand {
|
19 | load(program) {
|
20 | return __awaiter(this, void 0, void 0, function* () {
|
21 | program
|
22 | .command('generate <schematic> [name] [path]')
|
23 | .alias('g')
|
24 | .description(yield this.buildDescription())
|
25 | .option('-d, --dry-run', 'Report actions that would be taken without writing out results.')
|
26 | .option('-p, --project [project]', 'Project in which to generate files.')
|
27 | .option('--flat', 'Enforce flat structure of generated element.', () => true)
|
28 | .option('--no-flat', 'Enforce that directories are generated.', () => false)
|
29 | .option('--spec', 'Enforce spec files generation.', () => {
|
30 | return { value: true, passedAsInput: true };
|
31 | }, true)
|
32 | .option('--spec-file-suffix [suffix]', 'Use a custom suffix for spec files.')
|
33 | .option('--skip-import', 'Skip importing', () => true, false)
|
34 | .option('--no-spec', 'Disable spec files generation.', () => {
|
35 | return { value: false, passedAsInput: true };
|
36 | })
|
37 | .option('-c, --collection [collectionName]', 'Schematics collection to use.')
|
38 | .action((schematic, name, path, command) => __awaiter(this, void 0, void 0, function* () {
|
39 | const options = [];
|
40 | options.push({ name: 'dry-run', value: !!command.dryRun });
|
41 | if (command.flat !== undefined) {
|
42 | options.push({ name: 'flat', value: command.flat });
|
43 | }
|
44 | options.push({
|
45 | name: 'spec',
|
46 | value: typeof command.spec === 'boolean'
|
47 | ? command.spec
|
48 | : command.spec.value,
|
49 | options: {
|
50 | passedAsInput: typeof command.spec === 'boolean'
|
51 | ? false
|
52 | : command.spec.passedAsInput,
|
53 | },
|
54 | });
|
55 | options.push({
|
56 | name: 'specFileSuffix',
|
57 | value: command.specFileSuffix,
|
58 | });
|
59 | options.push({
|
60 | name: 'collection',
|
61 | value: command.collection,
|
62 | });
|
63 | options.push({
|
64 | name: 'project',
|
65 | value: command.project,
|
66 | });
|
67 | options.push({
|
68 | name: 'skipImport',
|
69 | value: command.skipImport,
|
70 | });
|
71 | const inputs = [];
|
72 | inputs.push({ name: 'schematic', value: schematic });
|
73 | inputs.push({ name: 'name', value: name });
|
74 | inputs.push({ name: 'path', value: path });
|
75 | yield this.action.handle(inputs, options);
|
76 | }));
|
77 | });
|
78 | }
|
79 | buildDescription() {
|
80 | return __awaiter(this, void 0, void 0, function* () {
|
81 | const collection = yield this.getCollection();
|
82 | return ('Generate a Nest element.\n' +
|
83 | ` Schematics available on ${chalk.bold(collection)} collection:\n` +
|
84 | this.buildSchematicsListAsTable(yield this.getSchematics(collection)));
|
85 | });
|
86 | }
|
87 | buildSchematicsListAsTable(schematics) {
|
88 | const leftMargin = ' ';
|
89 | const tableConfig = {
|
90 | head: ['name', 'alias', 'description'],
|
91 | chars: {
|
92 | 'left': leftMargin.concat('│'),
|
93 | 'top-left': leftMargin.concat('┌'),
|
94 | 'bottom-left': leftMargin.concat('└'),
|
95 | 'mid': '',
|
96 | 'left-mid': '',
|
97 | 'mid-mid': '',
|
98 | 'right-mid': '',
|
99 | },
|
100 | };
|
101 | const table = new Table(tableConfig);
|
102 | for (const schematic of schematics) {
|
103 | table.push([
|
104 | chalk.green(schematic.name),
|
105 | chalk.cyan(schematic.alias),
|
106 | schematic.description,
|
107 | ]);
|
108 | }
|
109 | return table.toString();
|
110 | }
|
111 | getCollection() {
|
112 | return __awaiter(this, void 0, void 0, function* () {
|
113 | const configuration = yield (0, load_configuration_1.loadConfiguration)();
|
114 | return configuration.collection;
|
115 | });
|
116 | }
|
117 | getSchematics(collection) {
|
118 | return __awaiter(this, void 0, void 0, function* () {
|
119 | const abstractCollection = schematics_1.CollectionFactory.create(collection);
|
120 | return abstractCollection.getSchematics();
|
121 | });
|
122 | }
|
123 | }
|
124 | exports.GenerateCommand = GenerateCommand;
|