index.js 3.2 KB

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