-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
generate.js
169 lines (136 loc) · 4.69 KB
/
generate.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
const path = require('path');
const chalk = require('chalk');
const Command = require('../models/command');
const Blueprint = require('../models/blueprint');
const mergeBlueprintOptions = require('../utilities/merge-blueprint-options');
const { merge, reject } = require('ember-cli-lodash-subset');
const EOL = require('os').EOL;
const SilentError = require('silent-error');
const UNKNOWN_BLUEPRINT_ERROR =
'The `ember generate` command requires a ' +
'blueprint name to be specified. ' +
'For more details, use `ember help`';
module.exports = Command.extend({
name: 'generate',
description: 'Generates new code from blueprints.',
aliases: ['g'],
works: 'insideProject',
availableOptions: [
{ name: 'dry-run', type: Boolean, default: false, aliases: ['d'] },
{ name: 'verbose', type: Boolean, default: false, aliases: ['v'] },
{ name: 'pod', type: Boolean, default: false, aliases: ['p', 'pods'] },
{ name: 'classic', type: Boolean, default: false, aliases: ['c'] },
{ name: 'dummy', type: Boolean, default: false, aliases: ['dum', 'id'] },
{ name: 'in-repo-addon', type: String, default: null, aliases: ['in-repo', 'ir'] },
{ name: 'lint-fix', type: Boolean, default: true },
{
name: 'in',
type: String,
default: null,
description:
'Runs a blueprint against an in repo addon. ' + 'A path is expected, relative to the root of the project.',
},
{
name: 'typescript',
type: Boolean,
aliases: ['ts'],
description: 'Generates a version of the blueprint written in TypeScript (if available).',
},
],
anonymousOptions: ['<blueprint>'],
beforeRun: mergeBlueprintOptions,
run(commandOptions, rawArgs) {
let blueprintName = rawArgs[0];
if (!blueprintName) {
return Promise.reject(new SilentError(UNKNOWN_BLUEPRINT_ERROR));
}
let taskArgs = {
args: rawArgs,
};
if (this.settings && this.settings.usePods && !commandOptions.classic) {
commandOptions.pod = true;
}
if (commandOptions.in) {
let relativePath = path.relative(this.project.root, commandOptions.in);
commandOptions.in = path.resolve(relativePath);
}
let taskOptions = merge(taskArgs, commandOptions || {});
if (this.project.initializeAddons) {
this.project.initializeAddons();
}
return this.runTask('GenerateFromBlueprint', taskOptions);
},
printDetailedHelp(options) {
this.ui.writeLine(this.getAllBlueprints(options));
},
addAdditionalJsonForHelp(json, options) {
json.availableBlueprints = this.getAllBlueprints(options);
},
getAllBlueprints(options) {
let lookupPaths = this.project.blueprintLookupPaths();
let blueprintList = Blueprint.list({ paths: lookupPaths });
let output = '';
let singleBlueprintName;
if (options.rawArgs) {
singleBlueprintName = options.rawArgs[0];
}
if (!singleBlueprintName && !options.json) {
output += `${EOL} Available blueprints:${EOL}`;
}
let collectionsJson = [];
blueprintList.forEach(function (collection) {
let result = this.getPackageBlueprints(collection, options, singleBlueprintName);
if (options.json) {
let collectionJson = {};
collectionJson[collection.source] = result;
collectionsJson.push(collectionJson);
} else {
output += result;
}
}, this);
if (singleBlueprintName && !output && !options.json) {
output = chalk.yellow(`The '${singleBlueprintName}' blueprint does not exist in this project.`) + EOL;
}
if (options.json) {
return collectionsJson;
} else {
return output;
}
},
getPackageBlueprints(collection, options, singleBlueprintName) {
let verbose = options.verbose;
let blueprints = collection.blueprints;
if (!verbose) {
blueprints = reject(blueprints, 'overridden');
}
let output = '';
if (blueprints.length && !singleBlueprintName && !options.json) {
output += ` ${collection.source}:${EOL}`;
}
let blueprintsJson = [];
blueprints.forEach(function (blueprint) {
let singleMatch = singleBlueprintName === blueprint.name;
if (singleMatch) {
verbose = true;
}
if (!singleBlueprintName || singleMatch) {
// this may add default keys for printing
blueprint.availableOptions.forEach(this.normalizeOption);
if (options.json) {
blueprintsJson.push(blueprint.getJson(verbose));
} else {
output += blueprint.printBasicHelp(verbose) + EOL;
}
}
}, this);
if (options.json) {
return blueprintsJson;
} else {
return output;
}
},
});
module.exports.ERRORS = {
UNKNOWN_BLUEPRINT_ERROR,
};