index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env node
  2. "use strict";
  3. var _keys = require("babel-runtime/core-js/object/keys");
  4. var _keys2 = _interopRequireDefault(_keys);
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6. var fs = require("fs");
  7. var commander = require("commander");
  8. var kebabCase = require("lodash/kebabCase");
  9. var options = require("babel-core").options;
  10. var util = require("babel-core").util;
  11. var uniq = require("lodash/uniq");
  12. var glob = require("glob");
  13. (0, _keys2.default)(options).forEach(function (key) {
  14. var option = options[key];
  15. if (option.hidden) return;
  16. var arg = kebabCase(key);
  17. if (option.type !== "boolean") {
  18. arg += " [" + (option.type || "string") + "]";
  19. }
  20. if (option.type === "boolean" && option.default === true) {
  21. arg = "no-" + arg;
  22. }
  23. arg = "--" + arg;
  24. if (option.shorthand) {
  25. arg = "-" + option.shorthand + ", " + arg;
  26. }
  27. var desc = [];
  28. if (option.deprecated) desc.push("[DEPRECATED] " + option.deprecated);
  29. if (option.description) desc.push(option.description);
  30. commander.option(arg, desc.join(" "));
  31. });
  32. commander.option("-x, --extensions [extensions]", "List of extensions to compile when a directory has been input [.es6,.js,.es,.jsx]");
  33. commander.option("-w, --watch", "Recompile files on changes");
  34. commander.option("--skip-initial-build", "Do not compile files before watching");
  35. commander.option("-o, --out-file [out]", "Compile all input files into a single file");
  36. commander.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory");
  37. commander.option("-D, --copy-files", "When compiling a directory copy over non-compilable files");
  38. commander.option("-q, --quiet", "Don't log anything");
  39. var pkg = require("../../package.json");
  40. commander.version(pkg.version + " (babel-core " + require("babel-core").version + ")");
  41. commander.usage("[options] <files ...>");
  42. commander.parse(process.argv);
  43. if (commander.extensions) {
  44. commander.extensions = util.arrayify(commander.extensions);
  45. }
  46. var errors = [];
  47. var filenames = commander.args.reduce(function (globbed, input) {
  48. var files = glob.sync(input);
  49. if (!files.length) files = [input];
  50. return globbed.concat(files);
  51. }, []);
  52. filenames = uniq(filenames);
  53. filenames.forEach(function (filename) {
  54. if (!fs.existsSync(filename)) {
  55. errors.push(filename + " doesn't exist");
  56. }
  57. });
  58. if (commander.outDir && !filenames.length) {
  59. errors.push("filenames required for --out-dir");
  60. }
  61. if (commander.outFile && commander.outDir) {
  62. errors.push("cannot have --out-file and --out-dir");
  63. }
  64. if (commander.watch) {
  65. if (!commander.outFile && !commander.outDir) {
  66. errors.push("--watch requires --out-file or --out-dir");
  67. }
  68. if (!filenames.length) {
  69. errors.push("--watch requires filenames");
  70. }
  71. }
  72. if (commander.skipInitialBuild && !commander.watch) {
  73. errors.push("--skip-initial-build requires --watch");
  74. }
  75. if (errors.length) {
  76. console.error(errors.join(". "));
  77. process.exit(2);
  78. }
  79. var opts = exports.opts = {};
  80. (0, _keys2.default)(options).forEach(function (key) {
  81. var opt = options[key];
  82. if (commander[key] !== undefined && commander[key] !== opt.default) {
  83. opts[key] = commander[key];
  84. }
  85. });
  86. opts.ignore = util.arrayify(opts.ignore, util.regexify);
  87. if (opts.only) {
  88. opts.only = util.arrayify(opts.only, util.regexify);
  89. }
  90. var fn = void 0;
  91. if (commander.outDir) {
  92. fn = require("./dir");
  93. } else {
  94. fn = require("./file");
  95. }
  96. fn(commander, filenames, exports.opts);