1 | import fs from 'fs'
|
2 |
|
3 | import commander from 'commander'
|
4 | import { version } from '@babel/core'
|
5 | import uniq from 'lodash/uniq'
|
6 | import glob from 'glob'
|
7 |
|
8 | import pkg from '../package.json'
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | commander.option('--config [path]', 'Path to a elodin.config.js file to use')
|
40 | commander.option('-w, --watch', 'Recompile files on changes')
|
41 | commander.option('-c, --clean', 'Remove old files before compilation')
|
42 | commander.option(
|
43 | '-sib, --skip-initial-build',
|
44 | 'Do not compile files before watching'
|
45 | )
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | commander.version(pkg.version)
|
57 | commander.usage('[options] <files ...>')
|
58 |
|
59 | export 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 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
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 |
|
113 |
|
114 |
|
115 |
|
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 |
|
131 | function 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 |
|
143 | function collect(value, previousValue) {
|
144 |
|
145 | if (typeof value !== 'string') return previousValue
|
146 |
|
147 | const values = value.split(',')
|
148 |
|
149 | return previousValue ? previousValue.concat(values) : values
|
150 | }
|