Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
260 lines (216 loc) · 6.35 KB
/
Copy pathcli.js
File metadata and controls
260 lines (216 loc) · 6.35 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'use strict';
const lookupCommand = require('./lookup-command');
const getOptionArgs = require('../utilities/get-option-args');
let logger = require('heimdalljs-logger')('ember-cli:cli');
let loggerTesting = require('heimdalljs-logger')('ember-cli:testing');
const heimdall = require('heimdalljs');
// Disabled until e2e and serve command can be evaluated/corrected -- require('../utilities/will-interrupt-process');
const onProcessInterrupt = { addHandler: (_handler) => { }, removeHandler: (_handler) => { } };
class CLI {
/**
* @private
* @class CLI
* @constructor
* @param options
*/
constructor(options) {
/**
* @private
* @property name
*/
this.name = options.name;
/**
* @private
* @property ui
* @type UI
*/
this.ui = options.ui;
/**
* @private
* @property analytics
*/
this.analytics = options.analytics;
/**
* @private
* @property testing
* @type Boolean
*/
this.testing = options.testing;
/**
* @private
* @property disableDependencyChecker
* @type Boolean
*/
this.disableDependencyChecker = options.disableDependencyChecker;
/**
* @private
* @property root
*/
this.root = options.root;
/**
* @private
* @property npmPackage
*/
this.npmPackage = options.npmPackage;
/**
* @private
* @property instrumentation
*/
this.instrumentation = {
stopAndReport: (...args) => { },
start: (...args) => { },
};
logger.info('testing %o', !!this.testing);
}
/**
* @private
* @method run
* @param environment
* @return {Promise}
*/
run(environment) {
let shutdownOnExit = null;
return Promise.resolve().then(() => {
let args = environment.cliArgs.slice();
if (args.length === 0) {
args[0] = 'help';
}
if (args[0] === '--help') {
if (args.length === 1) {
args[0] = 'help';
} else {
args.shift();
args.push('--help');
}
}
let commandName = args.shift();
let commandArgs = args;
let helpOptions;
let commandLookupCreationtoken = heimdall.start('lookup-command');
let CurrentCommand = lookupCommand(environment.commands, commandName, commandArgs, {
project: environment.project,
ui: this.ui,
});
let command = new CurrentCommand({
ui: this.ui,
analytics: this.analytics,
commands: environment.commands,
tasks: environment.tasks,
project: environment.project,
settings: environment.settings,
testing: this.testing,
cli: this,
});
commandLookupCreationtoken.stop();
getOptionArgs('--verbose', commandArgs).forEach(arg => {
process.env[`EMBER_VERBOSE_${arg.toUpperCase()}`] = 'true';
});
logger.info('command: %s', commandName);
if (!this.testing) {
let skipInstallationCheck = commandArgs.indexOf('--skip-installation-check') !== -1;
if (environment.project.isEmberCLIProject() && !skipInstallationCheck) {
const InstallationChecker = require('../models/installation-checker');
new InstallationChecker({ project: environment.project }).checkInstallations();
}
}
let instrumentation = this.instrumentation;
let onCommandInterrupt;
let runPromise = Promise.resolve().then(() => {
instrumentation.stopAndReport('init');
instrumentation.start('command');
loggerTesting.info('cli: command.beforeRun');
onProcessInterrupt.addHandler(onCommandInterrupt);
return command.beforeRun(commandArgs);
}).then(() => {
loggerTesting.info('cli: command.validateAndRun');
return command.validateAndRun(commandArgs);
}).then(result => {
instrumentation.stopAndReport('command', commandName, commandArgs);
onProcessInterrupt.removeHandler(onCommandInterrupt);
return result;
}).then(result => {
// if the help option was passed, call the help command
if (result === 'callHelp') {
helpOptions = {
environment,
commandName,
commandArgs,
};
return this.callHelp(helpOptions);
}
return result;
}).then(exitCode => {
loggerTesting.info(`cli: command run complete. exitCode: ${exitCode}`);
// TODO: fix this
// Possibly this issue: https://github.com/joyent/node/issues/8329
// Wait to resolve promise when running on windows.
// This ensures that stdout is flushed so acceptance tests get full output
return new Promise(resolve => {
if (process.platform === 'win32') {
setTimeout(resolve, 250, exitCode);
} else {
resolve(exitCode);
}
});
});
onCommandInterrupt = () =>
Promise.resolve(command.onInterrupt())
.then(() => runPromise);
return runPromise;
})
.catch(this.logError.bind(this));
}
/**
* @private
* @method callHelp
* @param options
* @return {Promise}
*/
callHelp(options) {
let environment = options.environment;
let commandName = options.commandName;
let commandArgs = options.commandArgs;
let helpIndex = commandArgs.indexOf('--help');
let hIndex = commandArgs.indexOf('-h');
let HelpCommand = lookupCommand(environment.commands, 'help', commandArgs, {
project: environment.project,
ui: this.ui,
});
let help = new HelpCommand({
ui: this.ui,
analytics: this.analytics,
commands: environment.commands,
tasks: environment.tasks,
project: environment.project,
settings: environment.settings,
testing: this.testing,
});
if (helpIndex > -1) {
commandArgs.splice(helpIndex, 1);
}
if (hIndex > -1) {
commandArgs.splice(hIndex, 1);
}
commandArgs.unshift(commandName);
return help.validateAndRun(commandArgs);
}
/**
* @private
* @method logError
* @param error
* @return {number}
*/
logError(error) {
if (this.testing && error) {
console.error(error.message);
if (error.stack) {
console.error(error.stack);
}
throw error;
}
this.ui.errorLog.push(error);
this.ui.writeError(error);
return 1;
}
}
module.exports = CLI;