cli.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. const commander_1 = require("commander");
  30. const editorconfig = __importStar(require("./"));
  31. const package_json_1 = __importDefault(require("../package.json"));
  32. /**
  33. * Default output routine, goes to stdout.
  34. *
  35. * @param s String to output
  36. */
  37. function writeStdOut(s) {
  38. process.stdout.write(s);
  39. }
  40. /**
  41. * Command line interface for editorconfig. Pulled out into a separate module
  42. * to make it easier to test.
  43. *
  44. * @param args Usually process.argv. Note that the first two parameters are
  45. * usually 'node' and 'editorconfig'
  46. * @param testing If testing, you may pass in a Commander OutputConfiguration
  47. * so that you can capture stdout and stderror. If `testing` is provided,
  48. * this routine will throw an error instead of calling `process.exit`.
  49. * @returns An array of combined properties, one for each file argument.
  50. */
  51. async function cli(args, testing) {
  52. const program = new commander_1.Command();
  53. let writeOut = writeStdOut;
  54. if (testing) {
  55. if (testing.writeOut) {
  56. // eslint-disable-next-line @typescript-eslint/unbound-method
  57. writeOut = testing.writeOut;
  58. }
  59. program.configureOutput(testing);
  60. program.exitOverride();
  61. }
  62. program.version('EditorConfig Node.js Core Version ' + package_json_1.default.version, '-v, --version', 'Display version information')
  63. .showHelpAfterError()
  64. .argument('<FILEPATH...>', 'Files to find configuration for. Can be a hyphen (-) if you want path(s) to be read from stdin.')
  65. .option('-f <path>', 'Specify conf filename other than \'.editorconfig\'')
  66. .option('-b <version>', 'Specify version (used by devs to test compatibility)')
  67. .option('--files', 'Output file names that contributed to the configuration, rather than the configuation itself')
  68. .parse(args);
  69. const files = program.args;
  70. const opts = program.opts();
  71. const cache = new Map();
  72. const visited = opts.files ?
  73. files.map(() => []) :
  74. undefined;
  75. // Process sequentially so caching works
  76. async function processAll() {
  77. const p = [];
  78. let i = 0;
  79. for (const filePath of files) {
  80. p.push(await editorconfig.parse(filePath, {
  81. config: opts.f,
  82. version: opts.b,
  83. files: visited ? visited[i++] : undefined,
  84. cache,
  85. }));
  86. }
  87. return p;
  88. }
  89. return await processAll().then((parsed) => {
  90. const header = parsed.length > 1;
  91. parsed.forEach((props, i) => {
  92. if (header) {
  93. writeOut(`[${files[i]}]\n`);
  94. }
  95. if (visited) {
  96. for (const v of visited[i]) {
  97. writeOut(`${v.fileName} [${v.glob}]\n`);
  98. }
  99. }
  100. else {
  101. for (const [key, value] of Object.entries(props)) {
  102. writeOut(`${key}=${String(value)}\n`);
  103. }
  104. }
  105. });
  106. return parsed;
  107. });
  108. }
  109. exports.default = cli;