4 * @fileoverview Main CLI that is run via the eslint command.
5 * @author Nicholas C. Zakas
8 /* eslint no-console:off */
12 // to use V8's code cache to speed up instantiation time
13 require("v8-compile-cache");
15 // must do this initialization *before* other requires in order to work
16 if (process.argv.includes("--debug")) {
17 require("debug").enable("eslint:*,-eslint:code-path,eslintrc:*");
20 //------------------------------------------------------------------------------
22 //------------------------------------------------------------------------------
25 * Read data from stdin til the end.
28 * - https://github.com/nodejs/node/blob/master/doc/api/process.md#processstdin
29 * - https://github.com/nodejs/node/blob/master/doc/api/process.md#a-note-on-process-io
30 * - https://lists.gnu.org/archive/html/bug-gnu-emacs/2016-01/msg00419.html
31 * - https://github.com/nodejs/node/issues/7439 (historical)
33 * On Windows using `fs.readFileSync(STDIN_FILE_DESCRIPTOR, "utf8")` seems
34 * to read 4096 bytes before blocking and never drains to read further data.
36 * The investigation on the Emacs thread indicates:
38 * > Emacs on MS-Windows uses pipes to communicate with subprocesses; a
39 * > pipe on Windows has a 4K buffer. So as soon as Emacs writes more than
40 * > 4096 bytes to the pipe, the pipe becomes full, and Emacs then waits for
41 * > the subprocess to read its end of the pipe, at which time Emacs will
42 * > write the rest of the stuff.
43 * @returns {Promise<string>} The read text.
45 function readStdin() {
46 return new Promise((resolve, reject) => {
52 .on("readable", () => {
53 while ((chunk = process.stdin.read()) !== null) {
57 .on("end", () => resolve(content))
63 * Get the error message of a given value.
64 * @param {any} error The value to get.
65 * @returns {string} The error message.
67 function getErrorMessage(error) {
69 // Lazy loading because this is used only if an error happened.
70 const util = require("util");
72 // Foolproof -- thirdparty module might throw non-object.
73 if (typeof error !== "object" || error === null) {
77 // Use templates if `error.messageTemplate` is present.
78 if (typeof error.messageTemplate === "string") {
80 const template = require(`../messages/${error.messageTemplate}.js`);
82 return template(error.messageData || {});
85 // Ignore template error then fallback to use `error.stack`.
89 // Use the stacktrace if it's an error object.
90 if (typeof error.stack === "string") {
94 // Otherwise, dump the object.
95 return util.format("%o", error);
99 * Catch and report unexpected error.
100 * @param {any} error The thrown error object.
103 function onFatalError(error) {
104 process.exitCode = 2;
106 const { version } = require("../package.json");
107 const message = getErrorMessage(error);
110 Oops! Something went wrong! :(
117 //------------------------------------------------------------------------------
119 //------------------------------------------------------------------------------
121 (async function main() {
122 process.on("uncaughtException", onFatalError);
123 process.on("unhandledRejection", onFatalError);
125 // Call the config initializer if `--init` is present.
126 if (process.argv.includes("--init")) {
127 await require("../lib/init/config-initializer").initializeConfig();
131 // Otherwise, call the CLI.
132 process.exitCode = await require("../lib/cli").execute(
134 process.argv.includes("--stdin") ? await readStdin() : null
136 }()).catch(onFatalError);