eslint.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env node
  2. /**
  3. * @fileoverview Main CLI that is run via the eslint command.
  4. * @author Nicholas C. Zakas
  5. */
  6. /* eslint no-console:off */
  7. "use strict";
  8. //------------------------------------------------------------------------------
  9. // Helpers
  10. //------------------------------------------------------------------------------
  11. const useStdIn = (process.argv.indexOf("--stdin") > -1),
  12. init = (process.argv.indexOf("--init") > -1),
  13. debug = (process.argv.indexOf("--debug") > -1);
  14. // must do this initialization *before* other requires in order to work
  15. if (debug) {
  16. require("debug").enable("eslint:*,-eslint:code-path");
  17. }
  18. //------------------------------------------------------------------------------
  19. // Requirements
  20. //------------------------------------------------------------------------------
  21. // now we can safely include the other modules that use debug
  22. const concat = require("concat-stream"),
  23. cli = require("../lib/cli"),
  24. path = require("path"),
  25. fs = require("fs");
  26. //------------------------------------------------------------------------------
  27. // Execution
  28. //------------------------------------------------------------------------------
  29. process.once("uncaughtException", err => {
  30. // lazy load
  31. const lodash = require("lodash");
  32. if (typeof err.messageTemplate === "string" && err.messageTemplate.length > 0) {
  33. const template = lodash.template(fs.readFileSync(path.resolve(__dirname, `../messages/${err.messageTemplate}.txt`), "utf-8"));
  34. const pkg = require("../package.json");
  35. console.error("\nOops! Something went wrong! :(");
  36. console.error(`\nESLint: ${pkg.version}.\n${template(err.messageData || {})}`);
  37. } else {
  38. console.error(err.message);
  39. console.error(err.stack);
  40. }
  41. process.exitCode = 1;
  42. });
  43. if (useStdIn) {
  44. process.stdin.pipe(concat({ encoding: "string" }, text => {
  45. process.exitCode = cli.execute(process.argv, text);
  46. }));
  47. } else if (init) {
  48. const configInit = require("../lib/config/config-initializer");
  49. configInit.initializeConfig().then(() => {
  50. process.exitCode = 0;
  51. }).catch(err => {
  52. process.exitCode = 1;
  53. console.error(err.message);
  54. console.error(err.stack);
  55. });
  56. } else {
  57. process.exitCode = cli.execute(process.argv);
  58. }