1 |
|
2 |
|
3 | /**
|
4 | * @fileoverview Main CLI that is run via the eslint command.
|
5 | * @author Nicholas C. Zakas
|
6 | */
|
7 |
|
8 | /* eslint no-console:off -- CLI */
|
9 |
|
10 | ;
|
11 |
|
12 | // must do this initialization *before* other requires in order to work
|
13 | if (process.argv.includes("--debug")) {
|
14 | require("debug").enable("eslint:*,-eslint:code-path,eslintrc:*");
|
15 | }
|
16 |
|
17 | //------------------------------------------------------------------------------
|
18 | // Helpers
|
19 | //------------------------------------------------------------------------------
|
20 |
|
21 | /**
|
22 | * Read data from stdin til the end.
|
23 | *
|
24 | * Note: See
|
25 | * - https://github.com/nodejs/node/blob/master/doc/api/process.md#processstdin
|
26 | * - https://github.com/nodejs/node/blob/master/doc/api/process.md#a-note-on-process-io
|
27 | * - https://lists.gnu.org/archive/html/bug-gnu-emacs/2016-01/msg00419.html
|
28 | * - https://github.com/nodejs/node/issues/7439 (historical)
|
29 | *
|
30 | * On Windows using `fs.readFileSync(STDIN_FILE_DESCRIPTOR, "utf8")` seems
|
31 | * to read 4096 bytes before blocking and never drains to read further data.
|
32 | *
|
33 | * The investigation on the Emacs thread indicates:
|
34 | *
|
35 | * > Emacs on MS-Windows uses pipes to communicate with subprocesses; a
|
36 | * > pipe on Windows has a 4K buffer. So as soon as Emacs writes more than
|
37 | * > 4096 bytes to the pipe, the pipe becomes full, and Emacs then waits for
|
38 | * > the subprocess to read its end of the pipe, at which time Emacs will
|
39 | * > write the rest of the stuff.
|
40 | * @returns {Promise<string>} The read text.
|
41 | */
|
42 | function readStdin() {
|
43 | return new Promise((resolve, reject) => {
|
44 | let content = "";
|
45 | let chunk = "";
|
46 |
|
47 | process.stdin
|
48 | .setEncoding("utf8")
|
49 | .on("readable", () => {
|
50 | while ((chunk = process.stdin.read()) !== null) {
|
51 | content += chunk;
|
52 | }
|
53 | })
|
54 | .on("end", () => resolve(content))
|
55 | .on("error", reject);
|
56 | });
|
57 | }
|
58 |
|
59 | /**
|
60 | * Get the error message of a given value.
|
61 | * @param {any} error The value to get.
|
62 | * @returns {string} The error message.
|
63 | */
|
64 | function getErrorMessage(error) {
|
65 |
|
66 | // Lazy loading because this is used only if an error happened.
|
67 | const util = require("util");
|
68 |
|
69 | // Foolproof -- third-party module might throw non-object.
|
70 | if (typeof error !== "object" || error === null) {
|
71 | return String(error);
|
72 | }
|
73 |
|
74 | // Use templates if `error.messageTemplate` is present.
|
75 | if (typeof error.messageTemplate === "string") {
|
76 | try {
|
77 | const template = require(`../messages/${error.messageTemplate}.js`);
|
78 |
|
79 | return template(error.messageData || {});
|
80 | } catch {
|
81 |
|
82 | // Ignore template error then fallback to use `error.stack`.
|
83 | }
|
84 | }
|
85 |
|
86 | // Use the stacktrace if it's an error object.
|
87 | if (typeof error.stack === "string") {
|
88 | return error.stack;
|
89 | }
|
90 |
|
91 | // Otherwise, dump the object.
|
92 | return util.format("%o", error);
|
93 | }
|
94 |
|
95 | /**
|
96 | * Tracks error messages that are shown to the user so we only ever show the
|
97 | * same message once.
|
98 | * @type {Set<string>}
|
99 | */
|
100 | const displayedErrors = new Set();
|
101 |
|
102 | /**
|
103 | * Tracks whether an unexpected error was caught
|
104 | * @type {boolean}
|
105 | */
|
106 | let hadFatalError = false;
|
107 |
|
108 | /**
|
109 | * Catch and report unexpected error.
|
110 | * @param {any} error The thrown error object.
|
111 | * @returns {void}
|
112 | */
|
113 | function onFatalError(error) {
|
114 | process.exitCode = 2;
|
115 | hadFatalError = true;
|
116 |
|
117 | const { version } = require("../package.json");
|
118 | const message = `
|
119 | Oops! Something went wrong! :(
|
120 |
|
121 | ESLint: ${version}
|
122 |
|
123 | ${getErrorMessage(error)}`;
|
124 |
|
125 | if (!displayedErrors.has(message)) {
|
126 | console.error(message);
|
127 | displayedErrors.add(message);
|
128 | }
|
129 | }
|
130 |
|
131 | //------------------------------------------------------------------------------
|
132 | // Execution
|
133 | //------------------------------------------------------------------------------
|
134 |
|
135 | (async function main() {
|
136 | process.on("uncaughtException", onFatalError);
|
137 | process.on("unhandledRejection", onFatalError);
|
138 |
|
139 | // Call the config initializer if `--init` is present.
|
140 | if (process.argv.includes("--init")) {
|
141 |
|
142 | // `eslint --init` has been moved to `@eslint/create-config`
|
143 | console.warn("You can also run this command directly using 'npm init @eslint/config'.");
|
144 |
|
145 | const spawn = require("cross-spawn");
|
146 |
|
147 | spawn.sync("npm", ["init", "@eslint/config"], { encoding: "utf8", stdio: "inherit" });
|
148 | return;
|
149 | }
|
150 |
|
151 | // Otherwise, call the CLI.
|
152 | const exitCode = await require("../lib/cli").execute(
|
153 | process.argv,
|
154 | process.argv.includes("--stdin") ? await readStdin() : null,
|
155 | true
|
156 | );
|
157 |
|
158 | /*
|
159 | * If an uncaught exception or unhandled rejection was detected in the meantime,
|
160 | * keep the fatal exit code 2 that is already assigned to `process.exitCode`.
|
161 | * Without this condition, exit code 2 (unsuccessful execution) could be overwritten with
|
162 | * 1 (successful execution, lint problems found) or even 0 (successful execution, no lint problems found).
|
163 | * This ensures that unexpected errors that seemingly don't affect the success
|
164 | * of the execution will still cause a non-zero exit code, as it's a common
|
165 | * practice and the default behavior of Node.js to exit with non-zero
|
166 | * in case of an uncaught exception or unhandled rejection.
|
167 | *
|
168 | * Otherwise, assign the exit code returned from CLI.
|
169 | */
|
170 | if (!hadFatalError) {
|
171 | process.exitCode = exitCode;
|
172 | }
|
173 | }()).catch(onFatalError);
|