UNPKG

2.63 kBJavaScriptView Raw
1#!/usr/bin/env node
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 */
9
10"use strict";
11
12// to use V8's code cache to speed up instantiation time
13require("v8-compile-cache");
14
15//------------------------------------------------------------------------------
16// Helpers
17//------------------------------------------------------------------------------
18
19const useStdIn = (process.argv.indexOf("--stdin") > -1),
20 init = (process.argv.indexOf("--init") > -1),
21 debug = (process.argv.indexOf("--debug") > -1);
22
23// must do this initialization *before* other requires in order to work
24if (debug) {
25 require("debug").enable("eslint:*,-eslint:code-path");
26}
27
28//------------------------------------------------------------------------------
29// Requirements
30//------------------------------------------------------------------------------
31
32// now we can safely include the other modules that use debug
33const cli = require("../lib/cli"),
34 path = require("path"),
35 fs = require("fs");
36
37//------------------------------------------------------------------------------
38// Execution
39//------------------------------------------------------------------------------
40
41process.once("uncaughtException", err => {
42
43 // lazy load
44 const lodash = require("lodash");
45
46 if (typeof err.messageTemplate === "string" && err.messageTemplate.length > 0) {
47 const template = lodash.template(fs.readFileSync(path.resolve(__dirname, `../messages/${err.messageTemplate}.txt`), "utf-8"));
48 const pkg = require("../package.json");
49
50 console.error("\nOops! Something went wrong! :(");
51 console.error(`\nESLint: ${pkg.version}.\n\n${template(err.messageData || {})}`);
52 } else {
53
54 console.error(err.stack);
55 }
56
57 process.exitCode = 2;
58});
59
60if (useStdIn) {
61
62 /*
63 * Note: `process.stdin.fd` is not used here due to https://github.com/nodejs/node/issues/7439.
64 * Accessing the `process.stdin` property seems to modify the behavior of file descriptor 0, resulting
65 * in an error when stdin is piped in asynchronously.
66 */
67 const STDIN_FILE_DESCRIPTOR = 0;
68
69 process.exitCode = cli.execute(process.argv, fs.readFileSync(STDIN_FILE_DESCRIPTOR, "utf8"));
70} else if (init) {
71 const configInit = require("../lib/init/config-initializer");
72
73 configInit.initializeConfig().then(() => {
74 process.exitCode = 0;
75 }).catch(err => {
76 process.exitCode = 1;
77 console.error(err.message);
78 console.error(err.stack);
79 });
80} else {
81 process.exitCode = cli.execute(process.argv);
82}