forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.ts
More file actions
111 lines (94 loc) · 3.44 KB
/
generate.ts
File metadata and controls
111 lines (94 loc) · 3.44 KB
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
// tslint:disable:no-global-tslint-disable no-any file-header
import { tags, terminal } from '@angular-devkit/core';
import { CommandScope, Option } from '../models/command';
import { SchematicCommand } from '../models/schematic-command';
import { getDefaultSchematicCollection } from '../utilities/config';
import {
getCollection,
getEngineHost,
} from '../utilities/schematics';
export default class GenerateCommand extends SchematicCommand {
public readonly name = 'generate';
public readonly description = 'Generates and/or modifies files based on a schematic.';
public static aliases = ['g'];
public readonly scope = CommandScope.inProject;
public arguments = ['schematic'];
public options: Option[] = [
...this.coreOptions,
];
private initialized = false;
public async initialize(options: any) {
if (this.initialized) {
return;
}
await super.initialize(options);
this.initialized = true;
const [collectionName, schematicName] = this.parseSchematicInfo(options);
if (!!schematicName) {
const schematicOptions = await this.getOptions({
schematicName,
collectionName,
});
this.options = this.options.concat(schematicOptions.options);
this.arguments = this.arguments.concat(schematicOptions.arguments.map(a => a.name));
}
}
validate(options: any): boolean | Promise<boolean> {
if (!options._[0]) {
this.logger.error(tags.oneLine`
The "ng generate" command requires a
schematic name to be specified.
For more details, use "ng help".`);
return false;
}
return true;
}
public run(options: any) {
const [collectionName, schematicName] = this.parseSchematicInfo(options);
// remove the schematic name from the options
options._ = options._.slice(1);
return this.runSchematic({
collectionName,
schematicName,
schematicOptions: options,
debug: options.debug,
dryRun: options.dryRun,
force: options.force,
});
}
private parseSchematicInfo(options: any) {
let collectionName = getDefaultSchematicCollection();
let schematicName: string = options._[0];
if (schematicName) {
if (schematicName.includes(':')) {
[collectionName, schematicName] = schematicName.split(':', 2);
}
}
return [collectionName, schematicName];
}
public printHelp(options: any) {
const schematicName = options._[0];
if (schematicName) {
const argDisplay = this.arguments && this.arguments.length > 0
? ' ' + this.arguments.filter(a => a !== 'schematic').map(a => `<${a}>`).join(' ')
: '';
const optionsDisplay = this.options && this.options.length > 0
? ' [options]'
: '';
this.logger.info(`usage: ng generate ${schematicName}${argDisplay}${optionsDisplay}`);
this.printHelpOptions(options);
} else {
this.printHelpUsage(this.name, this.arguments, this.options);
const engineHost = getEngineHost();
const [collectionName] = this.parseSchematicInfo(options);
const collection = getCollection(collectionName);
const schematicNames: string[] = engineHost.listSchematics(collection);
this.logger.info('Available schematics:');
schematicNames.forEach(schematicName => {
this.logger.info(` ${schematicName}`);
});
this.logger.warn(`\nTo see help for a schematic run:`);
this.logger.info(terminal.cyan(` ng generate <schematic> --help`));
}
}
}