bin.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. var path = require('path');
  3. var fs = require('fs');
  4. var acorn = require('./acorn.js');
  5. function _interopNamespace(e) {
  6. if (e && e.__esModule) return e;
  7. var n = Object.create(null);
  8. if (e) {
  9. Object.keys(e).forEach(function (k) {
  10. if (k !== 'default') {
  11. var d = Object.getOwnPropertyDescriptor(e, k);
  12. Object.defineProperty(n, k, d.get ? d : {
  13. enumerable: true,
  14. get: function () { return e[k]; }
  15. });
  16. }
  17. });
  18. }
  19. n["default"] = e;
  20. return Object.freeze(n);
  21. }
  22. var acorn__namespace = /*#__PURE__*/_interopNamespace(acorn);
  23. var inputFilePaths = [], forceFileName = false, fileMode = false, silent = false, compact = false, tokenize = false;
  24. var options = {};
  25. function help(status) {
  26. var print = (status === 0) ? console.log : console.error;
  27. print("usage: " + path.basename(process.argv[1]) + " [--ecma3|--ecma5|--ecma6|--ecma7|--ecma8|--ecma9|...|--ecma2015|--ecma2016|--ecma2017|--ecma2018|...]");
  28. print(" [--tokenize] [--locations] [--allow-hash-bang] [--allow-await-outside-function] [--compact] [--silent] [--module] [--help] [--] [<infile>...]");
  29. process.exit(status);
  30. }
  31. for (var i = 2; i < process.argv.length; ++i) {
  32. var arg = process.argv[i];
  33. if (arg[0] !== "-" || arg === "-") { inputFilePaths.push(arg); }
  34. else if (arg === "--") {
  35. inputFilePaths.push.apply(inputFilePaths, process.argv.slice(i + 1));
  36. forceFileName = true;
  37. break
  38. } else if (arg === "--locations") { options.locations = true; }
  39. else if (arg === "--allow-hash-bang") { options.allowHashBang = true; }
  40. else if (arg === "--allow-await-outside-function") { options.allowAwaitOutsideFunction = true; }
  41. else if (arg === "--silent") { silent = true; }
  42. else if (arg === "--compact") { compact = true; }
  43. else if (arg === "--help") { help(0); }
  44. else if (arg === "--tokenize") { tokenize = true; }
  45. else if (arg === "--module") { options.sourceType = "module"; }
  46. else {
  47. var match = arg.match(/^--ecma(\d+)$/);
  48. if (match)
  49. { options.ecmaVersion = +match[1]; }
  50. else
  51. { help(1); }
  52. }
  53. }
  54. function run(codeList) {
  55. var result = [], fileIdx = 0;
  56. try {
  57. codeList.forEach(function (code, idx) {
  58. fileIdx = idx;
  59. if (!tokenize) {
  60. result = acorn__namespace.parse(code, options);
  61. options.program = result;
  62. } else {
  63. var tokenizer = acorn__namespace.tokenizer(code, options), token;
  64. do {
  65. token = tokenizer.getToken();
  66. result.push(token);
  67. } while (token.type !== acorn__namespace.tokTypes.eof)
  68. }
  69. });
  70. } catch (e) {
  71. console.error(fileMode ? e.message.replace(/\(\d+:\d+\)$/, function (m) { return m.slice(0, 1) + inputFilePaths[fileIdx] + " " + m.slice(1); }) : e.message);
  72. process.exit(1);
  73. }
  74. if (!silent) { console.log(JSON.stringify(result, null, compact ? null : 2)); }
  75. }
  76. if (fileMode = inputFilePaths.length && (forceFileName || !inputFilePaths.includes("-") || inputFilePaths.length !== 1)) {
  77. run(inputFilePaths.map(function (path) { return fs.readFileSync(path, "utf8"); }));
  78. } else {
  79. var code = "";
  80. process.stdin.resume();
  81. process.stdin.on("data", function (chunk) { return code += chunk; });
  82. process.stdin.on("end", function () { return run([code]); });
  83. }