Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/env node
  2. "use strict";
  3. const validators = require("./validators");
  4. const config = require("..");
  5. const prettier = require("../prettier");
  6. // Require locally installed eslint, for `npx eslint-config-prettier` support
  7. // with no local eslint-config-prettier installation.
  8. const { ESLint } = require(require.resolve("eslint", {
  9. paths: [process.cwd(), ...require.resolve.paths("eslint")],
  10. }));
  11. const SPECIAL_RULES_URL =
  12. "https://github.com/prettier/eslint-config-prettier#special-rules";
  13. const PRETTIER_RULES_URL =
  14. "https://github.com/prettier/eslint-config-prettier#arrow-body-style-and-prefer-arrow-callback";
  15. if (module === require.main) {
  16. const args = process.argv.slice(2);
  17. if (args.length === 0) {
  18. console.error(help());
  19. process.exit(1);
  20. }
  21. const eslint = new ESLint();
  22. Promise.all(args.map((file) => eslint.calculateConfigForFile(file)))
  23. .then((configs) => {
  24. const rules = [].concat(
  25. ...configs.map(({ rules }, index) =>
  26. Object.entries(rules).map((entry) => [...entry, args[index]])
  27. )
  28. );
  29. const result = processRules(rules);
  30. if (result.stderr) {
  31. console.error(result.stderr);
  32. }
  33. if (result.stdout) {
  34. console.error(result.stdout);
  35. }
  36. process.exit(result.code);
  37. })
  38. .catch((error) => {
  39. console.error(error.message);
  40. process.exit(1);
  41. });
  42. }
  43. function help() {
  44. return `
  45. Usage: npx eslint-config-prettier FILE...
  46. Resolves an ESLint configuration for every given FILE and checks if they
  47. contain rules that are unnecessary or conflict with Prettier. Example:
  48. npx eslint-config-prettier index.js test/index.js other/file/to/check.js
  49. Exit codes:
  50. 0: No automatically detectable problems found.
  51. 1: General error.
  52. 2: Conflicting rules found.
  53. For more information, see:
  54. https://github.com/prettier/eslint-config-prettier#cli-helper-tool
  55. `.trim();
  56. }
  57. function processRules(configRules) {
  58. const regularRules = filterRules(config.rules, (_, value) => value === "off");
  59. const optionsRules = filterRules(
  60. config.rules,
  61. (ruleName, value) => value === 0 && ruleName in validators
  62. );
  63. const specialRules = filterRules(
  64. config.rules,
  65. (ruleName, value) => value === 0 && !(ruleName in validators)
  66. );
  67. const enabledRules = configRules
  68. .map(([ruleName, value, source]) => {
  69. const arrayValue = Array.isArray(value) ? value : [value];
  70. const [level, ...options] = arrayValue;
  71. const isOff = level === "off" || level === 0;
  72. return isOff ? null : { ruleName, options, source };
  73. })
  74. .filter(Boolean);
  75. const flaggedRules = enabledRules.filter(
  76. ({ ruleName }) => ruleName in config.rules
  77. );
  78. const regularFlaggedRuleNames = filterRuleNames(
  79. flaggedRules,
  80. ({ ruleName }) => ruleName in regularRules
  81. );
  82. const optionsFlaggedRuleNames = filterRuleNames(
  83. flaggedRules,
  84. ({ ruleName, ...rule }) =>
  85. ruleName in optionsRules && !validators[ruleName](rule)
  86. );
  87. const specialFlaggedRuleNames = filterRuleNames(
  88. flaggedRules,
  89. ({ ruleName }) => ruleName in specialRules
  90. );
  91. const prettierFlaggedRuleNames = filterRuleNames(
  92. enabledRules,
  93. ({ ruleName, source }) =>
  94. ruleName in prettier.rules &&
  95. enabledRules.some(
  96. (rule) =>
  97. rule.ruleName === "prettier/prettier" && rule.source === source
  98. )
  99. );
  100. const regularMessage = [
  101. "The following rules are unnecessary or might conflict with Prettier:",
  102. "",
  103. printRuleNames(regularFlaggedRuleNames),
  104. ].join("\n");
  105. const optionsMessage = [
  106. "The following rules are enabled with config that might conflict with Prettier. See:",
  107. SPECIAL_RULES_URL,
  108. "",
  109. printRuleNames(optionsFlaggedRuleNames),
  110. ].join("\n");
  111. const specialMessage = [
  112. "The following rules are enabled but cannot be automatically checked. See:",
  113. SPECIAL_RULES_URL,
  114. "",
  115. printRuleNames(specialFlaggedRuleNames),
  116. ].join("\n");
  117. const prettierMessage = [
  118. "The following rules can cause issues when using eslint-plugin-prettier at the same time.",
  119. "Only enable them if you know what you are doing! See:",
  120. PRETTIER_RULES_URL,
  121. "",
  122. printRuleNames(prettierFlaggedRuleNames),
  123. ].join("\n");
  124. if (
  125. regularFlaggedRuleNames.length === 0 &&
  126. optionsFlaggedRuleNames.length === 0
  127. ) {
  128. const message =
  129. specialFlaggedRuleNames.length === 0 &&
  130. prettierFlaggedRuleNames.length === 0
  131. ? "No rules that are unnecessary or conflict with Prettier were found."
  132. : [
  133. specialFlaggedRuleNames.length === 0 ? null : specialMessage,
  134. prettierFlaggedRuleNames.length === 0 ? null : prettierMessage,
  135. "Other than that, no rules that are unnecessary or conflict with Prettier were found.",
  136. ]
  137. .filter(Boolean)
  138. .join("\n\n");
  139. return {
  140. stdout: message,
  141. code: 0,
  142. };
  143. }
  144. const message = [
  145. regularFlaggedRuleNames.length === 0 ? null : regularMessage,
  146. optionsFlaggedRuleNames.length === 0 ? null : optionsMessage,
  147. specialFlaggedRuleNames.length === 0 ? null : specialMessage,
  148. prettierFlaggedRuleNames.length === 0 ? null : prettierMessage,
  149. ]
  150. .filter(Boolean)
  151. .join("\n\n");
  152. return {
  153. stdout: message,
  154. code: 2,
  155. };
  156. }
  157. function filterRules(rules, fn) {
  158. return Object.entries(rules)
  159. .filter(([ruleName, value]) => fn(ruleName, value))
  160. .reduce((obj, [ruleName]) => {
  161. obj[ruleName] = true;
  162. return obj;
  163. }, Object.create(null));
  164. }
  165. function filterRuleNames(rules, fn) {
  166. return [
  167. ...new Set(rules.filter((rule) => fn(rule)).map((rule) => rule.ruleName)),
  168. ];
  169. }
  170. function printRuleNames(ruleNames) {
  171. return ruleNames
  172. .slice()
  173. .sort()
  174. .map((ruleName) => `- ${ruleName}`)
  175. .join("\n");
  176. }
  177. exports.processRules = processRules;