forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
122 lines (104 loc) · 3.41 KB
/
index.ts
File metadata and controls
122 lines (104 loc) · 3.41 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
// tslint:disable:no-global-tslint-disable file-header
import { logging, terminal } from '@angular-devkit/core';
import { filter } from 'rxjs/operators';
import { runCommand } from '../../models/command-runner';
import { getProjectDetails } from '../../utilities/project';
function loadCommands() {
return {
// Schematics commands.
'add': require('../../commands/add').default,
'new': require('../../commands/new').default,
'generate': require('../../commands/generate').default,
'update': require('../../commands/update').default,
// Architect commands.
'build': require('../../commands/build').default,
'serve': require('../../commands/serve').default,
'test': require('../../commands/test').default,
'e2e': require('../../commands/e2e').default,
'lint': require('../../commands/lint').default,
'xi18n': require('../../commands/xi18n').default,
'run': require('../../commands/run').default,
// Disabled commands.
'eject': require('../../commands/eject').default,
// Easter eggs.
'make-this-awesome': require('../../commands/easter-egg').default,
// Other.
'config': require('../../commands/config').default,
'help': require('../../commands/help').default,
'version': require('../../commands/version').default,
'doc': require('../../commands/doc').default,
// deprecated
'get': require('../../commands/getset').default,
'set': require('../../commands/getset').default,
};
}
export default async function(options: { testing?: boolean, cliArgs: string[] }) {
const commands = loadCommands();
const logger = new logging.IndentLogger('cling');
let loggingSubscription;
if (!options.testing) {
loggingSubscription = initializeLogging(logger);
}
let projectDetails = getProjectDetails();
if (projectDetails === null) {
projectDetails = { root: process.cwd() };
}
const context = {
project: projectDetails,
};
try {
const maybeExitCode = await runCommand(commands, options.cliArgs, logger, context);
if (typeof maybeExitCode === 'number') {
console.assert(Number.isInteger(maybeExitCode));
return maybeExitCode;
}
return 0;
} catch (err) {
if (err instanceof Error) {
logger.fatal(err.message);
if (err.stack) {
logger.fatal(err.stack);
}
} else if (typeof err === 'string') {
logger.fatal(err);
} else if (typeof err === 'number') {
// Log nothing.
} else {
logger.fatal('An unexpected error occured: ' + JSON.stringify(err));
}
if (options.testing) {
debugger;
throw err;
}
if (loggingSubscription) {
loggingSubscription.unsubscribe();
}
return 1;
}
}
// Initialize logging.
function initializeLogging(logger: logging.Logger) {
return logger
.pipe(filter(entry => (entry.level != 'debug')))
.subscribe(entry => {
let color = (x: string) => terminal.dim(terminal.white(x));
let output = process.stdout;
switch (entry.level) {
case 'info':
color = terminal.white;
break;
case 'warn':
color = terminal.yellow;
break;
case 'error':
color = terminal.red;
output = process.stderr;
break;
case 'fatal':
color = (x) => terminal.bold(terminal.red(x));
output = process.stderr;
break;
}
output.write(color(entry.message) + '\n');
});
}