-
Notifications
You must be signed in to change notification settings - Fork 378
/
generate.command.ts
109 lines (104 loc) · 3.22 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
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.')
.option(
'--spec',
'Enforce spec files generation.',
() => {
return { value: true, passedAsInput: true };
},
true,
)
.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 });
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,
});
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' +
' Available schematics:\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();
}
}