UNPKG

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