-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathindex.ts
117 lines (104 loc) · 3.54 KB
/
index.ts
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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { logging } from '@angular-devkit/core';
import { format, stripVTControlCharacters } from 'node:util';
import { CommandModuleError } from '../../src/command-builder/command-module';
import { runCommand } from '../../src/command-builder/command-runner';
import { colors, supportColor } from '../../src/utilities/color';
import { ngDebug } from '../../src/utilities/environment-options';
import { writeErrorToLogFile } from '../../src/utilities/log-file';
export { VERSION } from '../../src/utilities/version';
const MIN_NODEJS_VERSION = [20, 11] as const;
/* eslint-disable no-console */
export default async function (options: { cliArgs: string[] }) {
// This node version check ensures that the requirements of the project instance of the CLI are met
const [major, minor] = process.versions.node.split('.').map((part) => Number(part));
if (
major < MIN_NODEJS_VERSION[0] ||
(major === MIN_NODEJS_VERSION[0] && minor < MIN_NODEJS_VERSION[1])
) {
process.stderr.write(
`Node.js version ${process.version} detected.\n` +
`The Angular CLI requires a minimum of v${MIN_NODEJS_VERSION[0]}.${MIN_NODEJS_VERSION[1]}.\n\n` +
'Please update your Node.js version or visit https://nodejs.org/ for additional instructions.\n',
);
return 3;
}
const colorLevels: Record<string, (message: string) => string> = {
info: (s) => s,
debug: (s) => s,
warn: (s) => colors.bold(colors.yellow(s)),
error: (s) => colors.bold(colors.red(s)),
fatal: (s) => colors.bold(colors.red(s)),
};
const logger = new logging.IndentLogger('cli-main-logger');
const logInfo = console.log;
const logError = console.error;
const useColor = supportColor();
const loggerFinished = logger.forEach((entry) => {
if (!ngDebug && entry.level === 'debug') {
return;
}
const color = useColor ? colorLevels[entry.level] : stripVTControlCharacters;
const message = color(entry.message);
switch (entry.level) {
case 'warn':
case 'fatal':
case 'error':
logError(message);
break;
default:
logInfo(message);
break;
}
});
// Redirect console to logger
console.info = console.log = function (...args) {
logger.info(format(...args));
};
console.warn = function (...args) {
logger.warn(format(...args));
};
console.error = function (...args) {
logger.error(format(...args));
};
try {
return await runCommand(options.cliArgs, logger);
} catch (err) {
if (err instanceof CommandModuleError) {
logger.fatal(`Error: ${err.message}`);
} else if (err instanceof Error) {
try {
const logPath = writeErrorToLogFile(err);
logger.fatal(
`An unhandled exception occurred: ${err.message}\n` +
`See "${logPath}" for further details.`,
);
} catch (e) {
logger.fatal(
`An unhandled exception occurred: ${err.message}\n` +
`Fatal error writing debug log file: ${e}`,
);
if (err.stack) {
logger.fatal(err.stack);
}
}
return 127;
} else if (typeof err === 'string') {
logger.fatal(err);
} else if (typeof err === 'number') {
// Log nothing.
} else {
logger.fatal(`An unexpected error occurred: ${err}`);
}
return 1;
} finally {
logger.complete();
await loggerFinished;
}
}