Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cli.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @fileoverview Main CLI object.
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. /*
  7. * The CLI object should *not* call process.exit() directly. It should only return
  8. * exit codes. This allows other programs to use the CLI object and still control
  9. * when the program exits.
  10. */
  11. //------------------------------------------------------------------------------
  12. // Requirements
  13. //------------------------------------------------------------------------------
  14. const fs = require("fs"),
  15. path = require("path"),
  16. options = require("./options"),
  17. CLIEngine = require("./cli-engine"),
  18. mkdirp = require("mkdirp"),
  19. log = require("./util/logging");
  20. const debug = require("debug")("eslint:cli");
  21. //------------------------------------------------------------------------------
  22. // Helpers
  23. //------------------------------------------------------------------------------
  24. /**
  25. * Predicate function for whether or not to apply fixes in quiet mode.
  26. * If a message is a warning, do not apply a fix.
  27. * @param {LintResult} lintResult The lint result.
  28. * @returns {boolean} True if the lint message is an error (and thus should be
  29. * autofixed), false otherwise.
  30. */
  31. function quietFixPredicate(lintResult) {
  32. return lintResult.severity === 2;
  33. }
  34. /**
  35. * Translates the CLI options into the options expected by the CLIEngine.
  36. * @param {Object} cliOptions The CLI options to translate.
  37. * @returns {CLIEngineOptions} The options object for the CLIEngine.
  38. * @private
  39. */
  40. function translateOptions(cliOptions) {
  41. return {
  42. envs: cliOptions.env,
  43. extensions: cliOptions.ext,
  44. rules: cliOptions.rule,
  45. plugins: cliOptions.plugin,
  46. globals: cliOptions.global,
  47. ignore: cliOptions.ignore,
  48. ignorePath: cliOptions.ignorePath,
  49. ignorePattern: cliOptions.ignorePattern,
  50. configFile: cliOptions.config,
  51. rulePaths: cliOptions.rulesdir,
  52. useEslintrc: cliOptions.eslintrc,
  53. parser: cliOptions.parser,
  54. parserOptions: cliOptions.parserOptions,
  55. cache: cliOptions.cache,
  56. cacheFile: cliOptions.cacheFile,
  57. cacheLocation: cliOptions.cacheLocation,
  58. fix: (cliOptions.fix || cliOptions.fixDryRun) && (cliOptions.quiet ? quietFixPredicate : true),
  59. fixTypes: cliOptions.fixType,
  60. allowInlineConfig: cliOptions.inlineConfig,
  61. reportUnusedDisableDirectives: cliOptions.reportUnusedDisableDirectives
  62. };
  63. }
  64. /**
  65. * Outputs the results of the linting.
  66. * @param {CLIEngine} engine The CLIEngine to use.
  67. * @param {LintResult[]} results The results to print.
  68. * @param {string} format The name of the formatter to use or the path to the formatter.
  69. * @param {string} outputFile The path for the output file.
  70. * @returns {boolean} True if the printing succeeds, false if not.
  71. * @private
  72. */
  73. function printResults(engine, results, format, outputFile) {
  74. let formatter;
  75. try {
  76. formatter = engine.getFormatter(format);
  77. } catch (e) {
  78. log.error(e.message);
  79. return false;
  80. }
  81. const output = formatter(results);
  82. if (output) {
  83. if (outputFile) {
  84. const filePath = path.resolve(process.cwd(), outputFile);
  85. if (fs.existsSync(filePath) && fs.statSync(filePath).isDirectory()) {
  86. log.error("Cannot write to output file path, it is a directory: %s", outputFile);
  87. return false;
  88. }
  89. try {
  90. mkdirp.sync(path.dirname(filePath));
  91. fs.writeFileSync(filePath, output);
  92. } catch (ex) {
  93. log.error("There was a problem writing the output file:\n%s", ex);
  94. return false;
  95. }
  96. } else {
  97. log.info(output);
  98. }
  99. }
  100. return true;
  101. }
  102. //------------------------------------------------------------------------------
  103. // Public Interface
  104. //------------------------------------------------------------------------------
  105. /**
  106. * Encapsulates all CLI behavior for eslint. Makes it easier to test as well as
  107. * for other Node.js programs to effectively run the CLI.
  108. */
  109. const cli = {
  110. /**
  111. * Executes the CLI based on an array of arguments that is passed in.
  112. * @param {string|Array|Object} args The arguments to process.
  113. * @param {string} [text] The text to lint (used for TTY).
  114. * @returns {int} The exit code for the operation.
  115. */
  116. execute(args, text) {
  117. if (Array.isArray(args)) {
  118. debug("CLI args: %o", args.slice(2));
  119. }
  120. let currentOptions;
  121. try {
  122. currentOptions = options.parse(args);
  123. } catch (error) {
  124. log.error(error.message);
  125. return 2;
  126. }
  127. const files = currentOptions._;
  128. const useStdin = typeof text === "string";
  129. if (currentOptions.version) { // version from package.json
  130. log.info(`v${require("../package.json").version}`);
  131. } else if (currentOptions.printConfig) {
  132. if (files.length) {
  133. log.error("The --print-config option must be used with exactly one file name.");
  134. return 2;
  135. }
  136. if (useStdin) {
  137. log.error("The --print-config option is not available for piped-in code.");
  138. return 2;
  139. }
  140. const engine = new CLIEngine(translateOptions(currentOptions));
  141. const fileConfig = engine.getConfigForFile(currentOptions.printConfig);
  142. log.info(JSON.stringify(fileConfig, null, " "));
  143. return 0;
  144. } else if (currentOptions.help || (!files.length && !useStdin)) {
  145. log.info(options.generateHelp());
  146. } else {
  147. debug(`Running on ${useStdin ? "text" : "files"}`);
  148. if (currentOptions.fix && currentOptions.fixDryRun) {
  149. log.error("The --fix option and the --fix-dry-run option cannot be used together.");
  150. return 2;
  151. }
  152. if (useStdin && currentOptions.fix) {
  153. log.error("The --fix option is not available for piped-in code; use --fix-dry-run instead.");
  154. return 2;
  155. }
  156. if (currentOptions.fixType && !currentOptions.fix && !currentOptions.fixDryRun) {
  157. log.error("The --fix-type option requires either --fix or --fix-dry-run.");
  158. return 2;
  159. }
  160. const engine = new CLIEngine(translateOptions(currentOptions));
  161. const report = useStdin ? engine.executeOnText(text, currentOptions.stdinFilename, true) : engine.executeOnFiles(files);
  162. if (currentOptions.fix) {
  163. debug("Fix mode enabled - applying fixes");
  164. CLIEngine.outputFixes(report);
  165. }
  166. if (currentOptions.quiet) {
  167. debug("Quiet mode enabled - filtering out warnings");
  168. report.results = CLIEngine.getErrorResults(report.results);
  169. }
  170. if (printResults(engine, report.results, currentOptions.format, currentOptions.outputFile)) {
  171. const tooManyWarnings = currentOptions.maxWarnings >= 0 && report.warningCount > currentOptions.maxWarnings;
  172. if (!report.errorCount && tooManyWarnings) {
  173. log.error("ESLint found too many warnings (maximum: %s).", currentOptions.maxWarnings);
  174. }
  175. return (report.errorCount || tooManyWarnings) ? 1 : 0;
  176. }
  177. return 2;
  178. }
  179. return 0;
  180. }
  181. };
  182. module.exports = cli;