options.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = parseArgv;
  6. function _fs() {
  7. const data = _interopRequireDefault(require("fs"));
  8. _fs = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _commander() {
  14. const data = _interopRequireDefault(require("commander"));
  15. _commander = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _core() {
  21. const data = require("@babel/core");
  22. _core = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _uniq() {
  28. const data = _interopRequireDefault(require("lodash/uniq"));
  29. _uniq = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _glob() {
  35. const data = _interopRequireDefault(require("glob"));
  36. _glob = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. var _package = _interopRequireDefault(require("../../package.json"));
  42. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  43. _commander().default.option("-f, --filename [filename]", "filename to use when reading from stdin - this will be used in source-maps, errors etc");
  44. _commander().default.option("--presets [list]", "comma-separated list of preset names", collect);
  45. _commander().default.option("--plugins [list]", "comma-separated list of plugin names", collect);
  46. _commander().default.option("--config-file [path]", "Path to a .babelrc file to use");
  47. _commander().default.option("--env-name [name]", "The name of the 'env' to use when loading configs and plugins. " + "Defaults to the value of BABEL_ENV, or else NODE_ENV, or else 'development'.");
  48. _commander().default.option("--root-mode [mode]", "The project-root resolution mode. " + "One of 'root' (the default), 'upward', or 'upward-optional'.");
  49. _commander().default.option("--source-type [script|module]", "");
  50. _commander().default.option("--no-babelrc", "Whether or not to look up .babelrc and .babelignore files");
  51. _commander().default.option("--ignore [list]", "list of glob paths to **not** compile", collect);
  52. _commander().default.option("--only [list]", "list of glob paths to **only** compile", collect);
  53. _commander().default.option("--no-highlight-code", "enable/disable ANSI syntax highlighting of code frames (on by default)");
  54. _commander().default.option("--no-comments", "write comments to generated output (true by default)");
  55. _commander().default.option("--retain-lines", "retain line numbers - will result in really ugly code");
  56. _commander().default.option("--compact [true|false|auto]", "do not include superfluous whitespace characters and line terminators", booleanify);
  57. _commander().default.option("--minified", "save as much bytes when printing [true|false]");
  58. _commander().default.option("--auxiliary-comment-before [string]", "print a comment before any injected non-user code");
  59. _commander().default.option("--auxiliary-comment-after [string]", "print a comment after any injected non-user code");
  60. _commander().default.option("-s, --source-maps [true|false|inline|both]", "", booleanify);
  61. _commander().default.option("--source-map-target [string]", "set `file` on returned source map");
  62. _commander().default.option("--source-file-name [string]", "set `sources[0]` on returned source map");
  63. _commander().default.option("--source-root [filename]", "the root from which all sources are relative");
  64. _commander().default.option("--module-root [filename]", "optional prefix for the AMD module formatter that will be prepend to the filename on module definitions");
  65. _commander().default.option("-M, --module-ids", "insert an explicit id for modules");
  66. _commander().default.option("--module-id [string]", "specify a custom name for module ids");
  67. _commander().default.option("-x, --extensions [extensions]", "List of extensions to compile when a directory has been input [.es6,.js,.es,.jsx,.mjs]", collect);
  68. _commander().default.option("--keep-file-extension", "Preserve the file extensions of the input files");
  69. _commander().default.option("-w, --watch", "Recompile files on changes");
  70. _commander().default.option("--skip-initial-build", "Do not compile files before watching");
  71. _commander().default.option("-o, --out-file [out]", "Compile all input files into a single file");
  72. _commander().default.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory");
  73. _commander().default.option("--relative", "Compile into an output directory relative to input directory or file. Requires --out-dir [out]");
  74. _commander().default.option("-D, --copy-files", "When compiling a directory copy over non-compilable files");
  75. _commander().default.option("--include-dotfiles", "Include dotfiles when compiling and copying non-compilable files");
  76. _commander().default.option("--verbose", "Log everything");
  77. _commander().default.option("--delete-dir-on-start", "Delete the out directory before compilation");
  78. _commander().default.version(_package.default.version + " (@babel/core " + _core().version + ")");
  79. _commander().default.usage("[options] <files ...>");
  80. function parseArgv(args) {
  81. _commander().default.parse(args);
  82. const errors = [];
  83. let filenames = _commander().default.args.reduce(function (globbed, input) {
  84. let files = _glob().default.sync(input);
  85. if (!files.length) files = [input];
  86. return globbed.concat(files);
  87. }, []);
  88. filenames = (0, _uniq().default)(filenames);
  89. filenames.forEach(function (filename) {
  90. if (!_fs().default.existsSync(filename)) {
  91. errors.push(filename + " does not exist");
  92. }
  93. });
  94. if (_commander().default.outDir && !filenames.length) {
  95. errors.push("--out-dir requires filenames");
  96. }
  97. if (_commander().default.outFile && _commander().default.outDir) {
  98. errors.push("--out-file and --out-dir cannot be used together");
  99. }
  100. if (_commander().default.relative && !_commander().default.outDir) {
  101. errors.push("--relative requires --out-dir usage");
  102. }
  103. if (_commander().default.watch) {
  104. if (!_commander().default.outFile && !_commander().default.outDir) {
  105. errors.push("--watch requires --out-file or --out-dir");
  106. }
  107. if (!filenames.length) {
  108. errors.push("--watch requires filenames");
  109. }
  110. }
  111. if (_commander().default.skipInitialBuild && !_commander().default.watch) {
  112. errors.push("--skip-initial-build requires --watch");
  113. }
  114. if (_commander().default.deleteDirOnStart && !_commander().default.outDir) {
  115. errors.push("--delete-dir-on-start requires --out-dir");
  116. }
  117. if (!_commander().default.outDir && filenames.length === 0 && typeof _commander().default.filename !== "string" && _commander().default.babelrc !== false) {
  118. errors.push("stdin compilation requires either -f/--filename [filename] or --no-babelrc");
  119. }
  120. if (errors.length) {
  121. console.error("babel:");
  122. errors.forEach(function (e) {
  123. console.error(" " + e);
  124. });
  125. process.exit(2);
  126. }
  127. const opts = _commander().default.opts();
  128. const babelOptions = {
  129. presets: opts.presets,
  130. plugins: opts.plugins,
  131. rootMode: opts.rootMode,
  132. configFile: opts.configFile,
  133. envName: opts.envName,
  134. sourceType: opts.sourceType,
  135. ignore: opts.ignore,
  136. only: opts.only,
  137. retainLines: opts.retainLines,
  138. compact: opts.compact,
  139. minified: opts.minified,
  140. auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
  141. auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
  142. sourceMaps: opts.sourceMaps,
  143. sourceFileName: opts.sourceFileName,
  144. sourceRoot: opts.sourceRoot,
  145. moduleRoot: opts.moduleRoot,
  146. moduleIds: opts.moduleIds,
  147. moduleId: opts.moduleId,
  148. babelrc: opts.babelrc === true ? undefined : opts.babelrc,
  149. highlightCode: opts.highlightCode === true ? undefined : opts.highlightCode,
  150. comments: opts.comments === true ? undefined : opts.comments
  151. };
  152. for (const key of Object.keys(babelOptions)) {
  153. if (babelOptions[key] === undefined) {
  154. delete babelOptions[key];
  155. }
  156. }
  157. return {
  158. babelOptions,
  159. cliOptions: {
  160. filename: opts.filename,
  161. filenames,
  162. extensions: opts.extensions,
  163. keepFileExtension: opts.keepFileExtension,
  164. watch: opts.watch,
  165. skipInitialBuild: opts.skipInitialBuild,
  166. outFile: opts.outFile,
  167. outDir: opts.outDir,
  168. relative: opts.relative,
  169. copyFiles: opts.copyFiles,
  170. includeDotfiles: opts.includeDotfiles,
  171. verbose: opts.verbose,
  172. deleteDirOnStart: opts.deleteDirOnStart,
  173. sourceMapTarget: opts.sourceMapTarget
  174. }
  175. };
  176. }
  177. function booleanify(val) {
  178. if (val === "true" || val == 1) {
  179. return true;
  180. }
  181. if (val === "false" || val == 0 || !val) {
  182. return false;
  183. }
  184. return val;
  185. }
  186. function collect(value, previousValue) {
  187. if (typeof value !== "string") return previousValue;
  188. const values = value.split(",");
  189. return previousValue ? previousValue.concat(values) : values;
  190. }