_babel-node.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. "use strict";
  2. var _pathIsAbsolute = require("path-is-absolute");
  3. var _pathIsAbsolute2 = _interopRequireDefault(_pathIsAbsolute);
  4. var _commander = require("commander");
  5. var _commander2 = _interopRequireDefault(_commander);
  6. var _module2 = require("module");
  7. var _module3 = _interopRequireDefault(_module2);
  8. var _util = require("util");
  9. var _path = require("path");
  10. var _path2 = _interopRequireDefault(_path);
  11. var _repl = require("repl");
  12. var _repl2 = _interopRequireDefault(_repl);
  13. var _babelCore = require("babel-core");
  14. var babel = _interopRequireWildcard(_babelCore);
  15. var _vm = require("vm");
  16. var _vm2 = _interopRequireDefault(_vm);
  17. require("babel-polyfill");
  18. var _babelRegister = require("babel-register");
  19. var _babelRegister2 = _interopRequireDefault(_babelRegister);
  20. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. var program = new _commander2.default.Command("babel-node");
  23. program.option("-e, --eval [script]", "Evaluate script");
  24. program.option("-p, --print [code]", "Evaluate script and print result");
  25. program.option("-o, --only [globs]", "");
  26. program.option("-i, --ignore [globs]", "");
  27. program.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js,.es,.jsx]");
  28. program.option("-w, --plugins [string]", "", _babelCore.util.list);
  29. program.option("-b, --presets [string]", "", _babelCore.util.list);
  30. var pkg = require("../package.json");
  31. program.version(pkg.version);
  32. program.usage("[options] [ -e script | script.js ] [arguments]");
  33. program.parse(process.argv);
  34. (0, _babelRegister2.default)({
  35. extensions: program.extensions,
  36. ignore: program.ignore,
  37. only: program.only,
  38. plugins: program.plugins,
  39. presets: program.presets
  40. });
  41. var replPlugin = function replPlugin(_ref) {
  42. var t = _ref.types;
  43. return {
  44. visitor: {
  45. ModuleDeclaration: function ModuleDeclaration(path) {
  46. throw path.buildCodeFrameError("Modules aren't supported in the REPL");
  47. },
  48. VariableDeclaration: function VariableDeclaration(path) {
  49. if (path.node.kind !== "var") {
  50. throw path.buildCodeFrameError("Only `var` variables are supported in the REPL");
  51. }
  52. },
  53. Program: function Program(path) {
  54. if (path.get("body").some(function (child) {
  55. return child.isExpressionStatement();
  56. })) return;
  57. path.pushContainer("body", t.expressionStatement(t.identifier("undefined")));
  58. }
  59. }
  60. };
  61. };
  62. var _eval = function _eval(code, filename) {
  63. code = code.trim();
  64. if (!code) return undefined;
  65. code = babel.transform(code, {
  66. filename: filename,
  67. presets: program.presets,
  68. plugins: (program.plugins || []).concat([replPlugin])
  69. }).code;
  70. return _vm2.default.runInThisContext(code, {
  71. filename: filename
  72. });
  73. };
  74. if (program.eval || program.print) {
  75. var code = program.eval;
  76. if (!code || code === true) code = program.print;
  77. global.__filename = "[eval]";
  78. global.__dirname = process.cwd();
  79. var _module = new _module3.default(global.__filename);
  80. _module.filename = global.__filename;
  81. _module.paths = _module3.default._nodeModulePaths(global.__dirname);
  82. global.exports = _module.exports;
  83. global.module = _module;
  84. global.require = _module.require.bind(_module);
  85. var result = _eval(code, global.__filename);
  86. if (program.print) {
  87. var output = typeof result === "string" ? result : (0, _util.inspect)(result);
  88. process.stdout.write(output + "\n");
  89. }
  90. } else {
  91. if (program.args.length) {
  92. var args = process.argv.slice(2);
  93. var i = 0;
  94. var ignoreNext = false;
  95. args.some(function (arg, i2) {
  96. if (ignoreNext) {
  97. ignoreNext = false;
  98. return;
  99. }
  100. if (arg[0] === "-") {
  101. var parsedArg = program[arg.slice(2)];
  102. if (parsedArg && parsedArg !== true) {
  103. ignoreNext = true;
  104. }
  105. } else {
  106. i = i2;
  107. return true;
  108. }
  109. });
  110. args = args.slice(i);
  111. var filename = args[0];
  112. if (!(0, _pathIsAbsolute2.default)(filename)) args[0] = _path2.default.join(process.cwd(), filename);
  113. process.argv = ["node"].concat(args);
  114. process.execArgv.unshift(__filename);
  115. _module3.default.runMain();
  116. } else {
  117. replStart();
  118. }
  119. }
  120. function replStart() {
  121. _repl2.default.start({
  122. prompt: "> ",
  123. input: process.stdin,
  124. output: process.stdout,
  125. eval: replEval,
  126. useGlobal: true
  127. });
  128. }
  129. function replEval(code, context, filename, callback) {
  130. var err = void 0;
  131. var result = void 0;
  132. try {
  133. if (code[0] === "(" && code[code.length - 1] === ")") {
  134. code = code.slice(1, -1);
  135. }
  136. result = _eval(code, filename);
  137. } catch (e) {
  138. err = e;
  139. }
  140. callback(err, result);
  141. }