UNPKG

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