suppress.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
  3. // See LICENSE in the project root for license information.
  4. Object.defineProperty(exports, "__esModule", { value: true });
  5. exports.suppressAsync = suppressAsync;
  6. const print_help_1 = require("./utils/print-help");
  7. const runEslint_1 = require("./runEslint");
  8. const constants_1 = require("../constants");
  9. async function suppressAsync() {
  10. const args = process.argv.slice(3);
  11. if (args.includes('--help') || args.includes('-h')) {
  12. (0, print_help_1.printSuppressHelp)();
  13. process.exit(0);
  14. }
  15. // Use reduce to create an object with all the parsed arguments
  16. const parsedArgs = args.reduce((acc, arg, index, arr) => {
  17. if (arg === '--rule') {
  18. // continue because next arg should be the rule
  19. }
  20. else if (index > 0 && arr[index - 1] === '--rule' && arr[index + 1]) {
  21. acc.rules.push(arg);
  22. }
  23. else if (arg === '--all') {
  24. acc.all = true;
  25. }
  26. else if (arg.startsWith('--')) {
  27. throw new Error(`@rushstack/eslint-bulk: Unknown option: ${arg}`);
  28. }
  29. else {
  30. acc.files.push(arg);
  31. }
  32. return acc;
  33. }, { rules: [], all: false, files: [] });
  34. if (parsedArgs.files.length === 0) {
  35. throw new Error('@rushstack/eslint-bulk: Files argument is required. Use glob patterns to specify files or use ' +
  36. '`.` to suppress all files for the specified rules.');
  37. }
  38. if (parsedArgs.rules.length === 0 && !parsedArgs.all) {
  39. throw new Error('@rushstack/eslint-bulk: Please specify at least one rule to suppress. Use --all to suppress all rules.');
  40. }
  41. // Find the index of the last argument that starts with '--'
  42. const lastOptionIndex = args
  43. .map((arg, i) => (arg.startsWith('--') ? i : -1))
  44. .reduce((lastIndex, currentIndex) => Math.max(lastIndex, currentIndex), -1);
  45. // Check if options come before files
  46. if (parsedArgs.files.some((file) => args.indexOf(file) < lastOptionIndex)) {
  47. throw new Error('@rushstack/eslint-bulk: Unable to parse command line arguments. All options should come before files argument.');
  48. }
  49. if (parsedArgs.all) {
  50. process.env[constants_1.ESLINT_BULK_SUPPRESS_ENV_VAR_NAME] = '*';
  51. }
  52. else if (parsedArgs.rules.length > 0) {
  53. process.env[constants_1.ESLINT_BULK_SUPPRESS_ENV_VAR_NAME] = parsedArgs.rules.join(',');
  54. }
  55. await (0, runEslint_1.runEslintAsync)(parsedArgs.files, 'suppress');
  56. if (parsedArgs.all) {
  57. console.log(`@rushstack/eslint-bulk: Successfully suppressed all rules for file(s) ${parsedArgs.files}`);
  58. }
  59. else if (parsedArgs.rules.length > 0) {
  60. console.log(`@rushstack/eslint-bulk: Successfully suppressed rules ${parsedArgs.rules} for file(s) ${parsedArgs.files}`);
  61. }
  62. }
  63. //# sourceMappingURL=suppress.js.map