-
Notifications
You must be signed in to change notification settings - Fork 406
/
Copy pathgenerate.command.ts
150 lines (140 loc) · 4.49 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { bold, cyan, green } from 'ansis';
import * as Table from 'cli-table3';
import { Command, CommanderStatic } from 'commander';
import { AbstractCollection, CollectionFactory } from '../lib/schematics';
import { Schematic } from '../lib/schematics/nest.collection';
import { loadConfiguration } from '../lib/utils/load-configuration';
import { AbstractCommand } from './abstract.command';
import { Input } from './command.input';
export class GenerateCommand extends AbstractCommand {
public async load(program: CommanderStatic): Promise<void> {
program
.command('generate <schematic> [name] [path]')
.alias('g')
.description(await 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(
'--spec-file-suffix [suffix]',
'Use a custom suffix for spec files.',
)
.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: 'specFileSuffix',
value: command.specFileSuffix,
});
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 async buildDescription(): Promise<string> {
const collection = await this.getCollection();
return (
'Generate a Nest element.\n' +
` Schematics available on ${bold(collection)} collection:\n` +
this.buildSchematicsListAsTable(await this.getSchematics(collection))
);
}
private buildSchematicsListAsTable(schematics: Schematic[]): 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 schematics) {
table.push([
green(schematic.name),
cyan(schematic.alias),
schematic.description,
]);
}
return table.toString();
}
private async getCollection(): Promise<string> {
const configuration = await loadConfiguration();
return configuration.collection;
}
private async getSchematics(collection: string): Promise<Schematic[]> {
const abstractCollection: AbstractCollection =
CollectionFactory.create(collection);
return abstractCollection.getSchematics();
}
}