UNPKG

4.27 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = parseArgv;
7
8var _fs = _interopRequireDefault(require("fs"));
9
10var _commander = _interopRequireDefault(require("commander"));
11
12var _core = require("@babel/core");
13
14var _uniq = _interopRequireDefault(require("lodash/uniq"));
15
16var _glob = _interopRequireDefault(require("glob"));
17
18var _package = _interopRequireDefault(require("../package.json"));
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
21
22// commander.option(
23// '--plugins [list]',
24// 'comma-separated list of plugin names',
25// collect
26// )
27// commander.option('--config-file [path]', 'Path to a .babelrc file to use')
28// commander.option(
29// '--env-name [name]',
30// "The name of the 'env' to use when loading configs and plugins. " +
31// "Defaults to the value of BABEL_ENV, or else NODE_ENV, or else 'development'."
32// )
33// commander.option(
34// '--root-mode [mode]',
35// 'The project-root resolution mode. ' +
36// "One of 'root' (the default), 'upward', or 'upward-optional'."
37// )
38// commander.option(
39// '--ignore [list]',
40// 'list of glob paths to **not** compile',
41// collect
42// )
43// commander.option(
44// '--only [list]',
45// 'list of glob paths to **only** compile',
46// collect
47// )
48_commander["default"].option('--config [path]', 'Path to a elodin.config.js file to use');
49
50_commander["default"].option('-w, --watch', 'Recompile files on changes');
51
52_commander["default"].option('-c, --clean', 'Remove old files before compilation');
53
54_commander["default"].option('-sib, --skip-initial-build', 'Do not compile files before watching'); // commander.option(
55// '-d, --out-dir [out]',
56// 'Compile an input directory of modules into an output directory'
57// )
58// commander.option(
59// '--relative',
60// 'Compile into an output directory relative to input directory or file. Requires --out-dir [out]'
61// )
62
63
64_commander["default"].version(_package["default"].version);
65
66_commander["default"].usage('[options] <files ...>');
67
68function parseArgv(args) {
69 //
70 _commander["default"].parse(args);
71
72 var errors = [];
73
74 var filenames = _commander["default"].args.reduce(function (globbed, input) {
75 var files = _glob["default"].sync(input);
76
77 if (!files.length) files = [input];
78 return globbed.concat(files);
79 }, []);
80
81 filenames = (0, _uniq["default"])(filenames);
82 filenames.forEach(function (filename) {
83 if (!_fs["default"].existsSync(filename)) {
84 errors.push(filename + ' does not exist');
85 }
86 }); // if (commander.outDir && !filenames.length) {
87 // errors.push('--out-dir requires filenames')
88 // }
89 // if (commander.outFile && commander.outDir) {
90 // errors.push('--out-file and --out-dir cannot be used together')
91 // }
92 // if (commander.relative && !commander.outDir) {
93 // errors.push('--relative requires --out-dir usage')
94 // }
95
96 if (_commander["default"].watch) {
97 if (!filenames.length) {
98 errors.push('--watch requires filenames');
99 }
100 }
101
102 if (_commander["default"].skipInitialBuild && !_commander["default"].watch) {
103 errors.push('--skip-initial-build requires --watch');
104 }
105
106 if (errors.length) {
107 console.error('elodin:');
108 errors.forEach(function (e) {
109 console.error(' ' + e);
110 });
111 process.exit(2);
112 }
113
114 var opts = _commander["default"].opts();
115
116 var elodinOptions = {
117 // presets: opts.presets,
118 // plugins: opts.plugins,
119 // ignore: opts.ignore,
120 // only: opts.only,
121 configFile: opts.config || './elodin.config.js'
122 };
123 return {
124 elodinOptions: elodinOptions,
125 cliOptions: {
126 filename: opts.filename,
127 filenames: filenames,
128 watch: opts.watch,
129 clean: opts.clean,
130 skipInitialBuild: opts.skipInitialBuild
131 }
132 };
133}
134
135function booleanify(val) {
136 if (val === 'true' || val == 1) {
137 return true;
138 }
139
140 if (val === 'false' || val == 0 || !val) {
141 return false;
142 }
143
144 return val;
145}
146
147function collect(value, previousValue) {
148 // If the user passed the option with no value, like "babel file.js --presets", do nothing.
149 if (typeof value !== 'string') return previousValue;
150 var values = value.split(',');
151 return previousValue ? previousValue.concat(values) : values;
152}
\No newline at end of file