]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/bin/eslint.js
4 * @fileoverview Main CLI that is run via the eslint command.
5 * @author Nicholas C. Zakas
8 /* eslint no-console:off -- CLI */
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:*");
17 //------------------------------------------------------------------------------
19 //------------------------------------------------------------------------------
22 * Read data from stdin til the end.
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)
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.
33 * The investigation on the Emacs thread indicates:
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.
42 function readStdin() {
43 return new Promise((resolve
, reject
) => {
49 .on("readable", () => {
50 while ((chunk
= process
.stdin
.read()) !== null) {
54 .on("end", () => resolve(content
))
60 * Get the error message of a given value.
61 * @param {any} error The value to get.
62 * @returns {string} The error message.
64 function getErrorMessage(error
) {
66 // Lazy loading because this is used only if an error happened.
67 const util
= require("util");
69 // Foolproof -- third-party module might throw non-object.
70 if (typeof error
!== "object" || error
=== null) {
74 // Use templates if `error.messageTemplate` is present.
75 if (typeof error
.messageTemplate
=== "string") {
77 const template
= require(`../messages/${error.messageTemplate}.js`);
79 return template(error
.messageData
|| {});
82 // Ignore template error then fallback to use `error.stack`.
86 // Use the stacktrace if it's an error object.
87 if (typeof error
.stack
=== "string") {
91 // Otherwise, dump the object.
92 return util
.format("%o", error
);
96 * Catch and report unexpected error.
97 * @param {any} error The thrown error object.
100 function onFatalError(error
) {
101 process
.exitCode
= 2;
103 const { version
} = require("../package.json");
104 const message
= getErrorMessage(error
);
107 Oops! Something went wrong! :(
114 //------------------------------------------------------------------------------
116 //------------------------------------------------------------------------------
118 (async
function main() {
119 process
.on("uncaughtException", onFatalError
);
120 process
.on("unhandledRejection", onFatalError
);
122 // Call the config initializer if `--init` is present.
123 if (process
.argv
.includes("--init")) {
125 // `eslint --init` has been moved to `@eslint/create-config`
126 console
.warn("You can also run this command directly using 'npm init @eslint/config'.");
128 const spawn
= require("cross-spawn");
130 spawn
.sync("npm", ["init", "@eslint/config"], { encoding
: "utf8", stdio
: "inherit" });
134 // Otherwise, call the CLI.
135 process
.exitCode
= await
require("../lib/cli").execute(
137 process
.argv
.includes("--stdin") ? await
readStdin() : null,
140 }()).catch(onFatalError
);