/
generate.command.ts
130 lines (122 loc) · 3.68 KB
/
generate.command.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as chalk from 'chalk';
import * as Table from 'cli-table3';
import { Command, CommanderStatic } from 'commander';
import { NestCollection } from '../lib/schematics/nest.collection';
import { AbstractCommand } from './abstract.command';
import { Input } from './command.input';
export class GenerateCommand extends AbstractCommand {
public load(program: CommanderStatic) {
program
.command('generate <schematic> [name] [path]')
.alias('g')
.description(this.buildDescription())
.option(
'-d, --dry-run',
'Report actions that would be taken without writing out results.',
)
.option('-p, --project [project]', 'Project in which to generate files.')
.option(
'--flat',
'Enforce flat structure of generated element.',
() => true,
)
.option(
'--no-flat',
'Enforce that directories are generated.',
() => false,
)
.option(
'--spec',
'Enforce spec files generation.',
() => {
return { value: true, passedAsInput: true };
},
true,
)
.option('--skip-import', 'Skip importing', () => true, false)
.option('--no-spec', 'Disable spec files generation.', () => {
return { value: false, passedAsInput: true };
})
.option(
'-c, --collection [collectionName]',
'Schematics collection to use.',
)
.action(
async (
schematic: string,
name: string,
path: string,
command: Command,
) => {
const options: Input[] = [];
options.push({ name: 'dry-run', value: !!command.dryRun });
if (command.flat !== undefined) {
options.push({ name: 'flat', value: command.flat });
}
options.push({
name: 'spec',
value:
typeof command.spec === 'boolean'
? command.spec
: command.spec.value,
options: {
passedAsInput:
typeof command.spec === 'boolean'
? false
: command.spec.passedAsInput,
},
});
options.push({
name: 'collection',
value: command.collection,
});
options.push({
name: 'project',
value: command.project,
});
options.push({
name: 'skipImport',
value: command.skipImport,
});
const inputs: Input[] = [];
inputs.push({ name: 'schematic', value: schematic });
inputs.push({ name: 'name', value: name });
inputs.push({ name: 'path', value: path });
await this.action.handle(inputs, options);
},
);
}
private buildDescription(): string {
return (
'Generate a Nest element.\n' +
` Schematics available on ${chalk.bold(
'@nestjs/schematics',
)} collection:\n` +
this.buildSchematicsListAsTable()
);
}
private buildSchematicsListAsTable(): string {
const leftMargin = ' ';
const tableConfig = {
head: ['name', 'alias', 'description'],
chars: {
'left': leftMargin.concat('│'),
'top-left': leftMargin.concat('┌'),
'bottom-left': leftMargin.concat('└'),
'mid': '',
'left-mid': '',
'mid-mid': '',
'right-mid': '',
},
};
const table: any = new Table(tableConfig);
for (const schematic of NestCollection.getSchematics()) {
table.push([
chalk.green(schematic.name),
chalk.cyan(schematic.alias),
schematic.description,
]);
}
return table.toString();
}
}