UNPKG

4.21 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.GenerateCommand = void 0;
13const chalk = require("chalk");
14const Table = require("cli-table3");
15const schematics_1 = require("../lib/schematics");
16const nest_collection_1 = require("../lib/schematics/nest.collection");
17const abstract_command_1 = require("./abstract.command");
18class GenerateCommand extends abstract_command_1.AbstractCommand {
19 load(program) {
20 program
21 .command('generate <schematic> [name] [path]')
22 .alias('g')
23 .description(this.buildDescription())
24 .option('-d, --dry-run', 'Report actions that would be taken without writing out results.')
25 .option('-p, --project [project]', 'Project in which to generate files.')
26 .option('--flat', 'Enforce flat structure of generated element.')
27 .option('--spec', 'Enforce spec files generation.', () => {
28 return { value: true, passedAsInput: true };
29 }, true)
30 .option('--no-spec', 'Disable spec files generation.', () => {
31 return { value: false, passedAsInput: true };
32 })
33 .option('-c, --collection [collectionName]', 'Schematics collection to use.')
34 .action((schematic, name, path, command) => __awaiter(this, void 0, void 0, function* () {
35 const options = [];
36 options.push({ name: 'dry-run', value: !!command.dryRun });
37 options.push({ name: 'flat', value: command.flat });
38 options.push({
39 name: 'spec',
40 value: typeof command.spec === 'boolean'
41 ? command.spec
42 : command.spec.value,
43 options: {
44 passedAsInput: typeof command.spec === 'boolean'
45 ? false
46 : command.spec.passedAsInput,
47 },
48 });
49 options.push({
50 name: 'collection',
51 value: command.collection || schematics_1.Collection.NESTJS,
52 });
53 options.push({
54 name: 'project',
55 value: command.project,
56 });
57 const inputs = [];
58 inputs.push({ name: 'schematic', value: schematic });
59 inputs.push({ name: 'name', value: name });
60 inputs.push({ name: 'path', value: path });
61 yield this.action.handle(inputs, options);
62 }));
63 }
64 buildDescription() {
65 return ('Generate a Nest element.\n' +
66 ' Available schematics:\n' +
67 this.buildSchematicsListAsTable());
68 }
69 buildSchematicsListAsTable() {
70 const leftMargin = ' ';
71 const tableConfig = {
72 head: ['name', 'alias', 'description'],
73 chars: {
74 'left': leftMargin.concat('│'),
75 'top-left': leftMargin.concat('┌'),
76 'bottom-left': leftMargin.concat('└'),
77 'mid': '',
78 'left-mid': '',
79 'mid-mid': '',
80 'right-mid': '',
81 },
82 };
83 const table = new Table(tableConfig);
84 for (const schematic of nest_collection_1.NestCollection.getSchematics()) {
85 table.push([
86 chalk.green(schematic.name),
87 chalk.cyan(schematic.alias),
88 schematic.description,
89 ]);
90 }
91 return table.toString();
92 }
93}
94exports.GenerateCommand = GenerateCommand;