forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
166 lines (138 loc) · 4.87 KB
/
cli.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
'use strict';
var lookupCommand = require('./lookup-command');
var Promise = require('../ext/promise');
var getOptionArgs = require('../utilities/get-option-args');
var debug = require('debug')('ember-cli:cli');
var debugTesting = require('debug')('ember-cli:testing');
var PlatformChecker = require('../utilities/platform-checker');
var InstallationChecker = require('../models/installation-checker');
function CLI(options) {
this.name = options.name;
this.ui = options.ui;
this.testing = options.testing;
this.disableDependencyChecker = options.disableDependencyChecker;
this.root = options.root;
this.npmPackage = options.npmPackage;
debug('testing %o', !!this.testing);
}
module.exports = CLI;
CLI.prototype.run = function(environment) {
return Promise.hash(environment).then(function(environment) {
var args = environment.cliArgs.slice();
if (args[0] === '--help') {
if (args.length === 1) {
args[0] = 'help';
} else {
args.shift();
args.push('--help');
}
}
var commandName = args.shift();
var commandArgs = args;
var helpOptions;
var update;
var CurrentCommand = lookupCommand(environment.commands, commandName, commandArgs, {
project: environment.project,
ui: this.ui
});
var command = new CurrentCommand({
ui: this.ui,
commands: environment.commands,
tasks: environment.tasks,
project: environment.project,
settings: environment.settings,
testing: this.testing,
cli: this
});
getOptionArgs('--verbose', commandArgs).forEach(function(arg) {
process.env['EMBER_VERBOSE_' + arg.toUpperCase()] = 'true';
});
var platform = new PlatformChecker(process.version);
if (!platform.isValid && !this.testing) {
if (platform.isDeprecated) {
this.ui.writeDeprecateLine('Node ' + process.version +
' is no longer supported by Angular CLI. Please update to a more recent version of Node');
}
if (platform.isUntested) {
this.ui.writeWarnLine('WARNING: Node ' + process.version +
' has currently not been tested against Angular CLI and may result in unexpected behaviour.');
}
}
debug('command: %s', commandName);
if (!this.testing) {
process.chdir(environment.project.root);
var skipInstallationCheck = commandArgs.indexOf('--skip-installation-check') !== -1;
if (environment.project.isEmberCLIProject() && !skipInstallationCheck) {
new InstallationChecker({ project: environment.project }).checkInstallations();
}
}
command.beforeRun(commandArgs);
return Promise.resolve(update).then(function() {
debugTesting('cli: command.validateAndRun');
return command.validateAndRun(commandArgs);
}).then(function(result) {
// if the help option was passed, call the help command
if (result === 'callHelp') {
helpOptions = {
environment: environment,
commandName: commandName,
commandArgs: commandArgs
};
return this.callHelp(helpOptions);
}
return result;
}.bind(this)).then(function(exitCode) {
debugTesting('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(function(resolve) {
if (process.platform === 'win32') {
setTimeout(resolve, 250, exitCode);
} else {
resolve(exitCode);
}
});
}.bind(this));
}.bind(this)).catch(this.logError.bind(this));
};
CLI.prototype.callHelp = function(options) {
var environment = options.environment;
var commandName = options.commandName;
var commandArgs = options.commandArgs;
var helpIndex = commandArgs.indexOf('--help');
var hIndex = commandArgs.indexOf('-h');
var HelpCommand = lookupCommand(environment.commands, 'help', commandArgs, {
project: environment.project,
ui: this.ui
});
var help = new HelpCommand({
ui: this.ui,
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);
};
CLI.prototype.logError = function(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;
};