1 | import '@babel/polyfill'
|
2 | import defaults from 'lodash/defaults'
|
3 | import outputFileSync from 'output-file-sync'
|
4 | import { sync as mkdirpSync } from 'mkdirp'
|
5 | import { performance } from 'perf_hooks'
|
6 | import rimraf from 'rimraf'
|
7 | import slash from 'slash'
|
8 | import path from 'path'
|
9 | import chalk from 'chalk'
|
10 | import glob from 'glob'
|
11 | import fs from 'fs'
|
12 |
|
13 | import * as util from './util'
|
14 |
|
15 | export default async function({ cliOptions, elodinOptions }) {
|
16 | let config
|
17 |
|
18 | const configPath = path.join(process.cwd(), elodinOptions.configFile)
|
19 | try {
|
20 | config = require(configPath)
|
21 | } catch (e) {
|
22 | console.error(
|
23 | chalk`{bold.red Unable to locate the elodin config file at ${configPath}.}
|
24 |
|
25 | {red A valid elodin configuration must be passed.
|
26 | Start by creating a {bold elodin.config.js} file.
|
27 | For further information check out {underline https://elodin.dev/docs/setup/getting-started}.}`
|
28 | )
|
29 | console.error(e)
|
30 |
|
31 | return false
|
32 | }
|
33 |
|
34 | const filenames = cliOptions.filenames
|
35 |
|
36 | if (!config.generator) {
|
37 | console.error(
|
38 | chalk`{bold.red Unable to find a generator in your elodin configuration.}
|
39 |
|
40 | {red A generator is required to be able to compile elodin files.
|
41 | For further information check out {underline https://elodin.dev/docs/setup/configuration}.}`
|
42 | )
|
43 | return false
|
44 | }
|
45 |
|
46 | if (cliOptions.clean) {
|
47 | if (config.generator.filePattern) {
|
48 | let didClean = false
|
49 | const ignorePattern = config.generator.ignorePattern || []
|
50 | config.generator.filePattern.map(v => {
|
51 | try {
|
52 | const files = glob.sync(process.cwd() + '/**/' + v)
|
53 | const actualFiles = files.filter(
|
54 | file =>
|
55 | ignorePattern.find(pattern => file.indexOf(pattern) !== -1) ===
|
56 | undefined
|
57 | )
|
58 |
|
59 | if (actualFiles.length > 0) {
|
60 | console.log('Cleaning ' + actualFiles.length + ' files.')
|
61 | actualFiles.forEach(fs.unlinkSync)
|
62 | didClean = true
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | } catch (e) {
|
71 |
|
72 |
|
73 | }
|
74 | })
|
75 |
|
76 | if (didClean) {
|
77 | console.log('')
|
78 | }
|
79 | }
|
80 | }
|
81 |
|
82 | async function walk(filenames) {
|
83 | console.log(chalk`{cyan >>>> Start compiling}`)
|
84 |
|
85 | const _filenames = []
|
86 | let start = performance.now()
|
87 |
|
88 | filenames.forEach(function(filename) {
|
89 | if (!fs.existsSync(filename)) return
|
90 |
|
91 | const stat = fs.statSync(filename)
|
92 | if (stat.isDirectory()) {
|
93 | const dirname = filename
|
94 |
|
95 | util.readdirForCompilable(filename).forEach(function(filename) {
|
96 | _filenames.push(path.join(dirname, filename))
|
97 | })
|
98 | } else {
|
99 | _filenames.push(filename)
|
100 | }
|
101 | })
|
102 |
|
103 | let files = 0
|
104 |
|
105 | const compileConfig = {
|
106 | ...config,
|
107 | errors: cliOptions.watch ? 'log' : 'throw',
|
108 | errorCount: 0,
|
109 | }
|
110 |
|
111 | const results = await Promise.all(
|
112 | _filenames.map(async function(filename) {
|
113 | let sourceFilename = filename
|
114 |
|
115 | sourceFilename = slash(sourceFilename)
|
116 |
|
117 | const didCompile = await util.compile(filename, compileConfig)
|
118 |
|
119 | files++
|
120 | })
|
121 | )
|
122 |
|
123 | let end = performance.now()
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | let fail = compileConfig.errorCount
|
130 |
|
131 | if (files > 0) {
|
132 | console.log(
|
133 | chalk`{cyan >>>> Finish compiling (${files} file${
|
134 | files > 1 ? 's' : ''
|
135 | }${
|
136 | fail > 0 ? chalk`, {red ${fail} error${fail > 1 ? 's' : ''}}` : ''
|
137 | })} ${Math.round(end - start)} mseconds`
|
138 | )
|
139 | }
|
140 | }
|
141 |
|
142 | async function files(filenames) {
|
143 | if (!cliOptions.skipInitialBuild) {
|
144 | await walk(filenames)
|
145 | }
|
146 |
|
147 | if (cliOptions.watch) {
|
148 | const chokidar = util.requireChokidar()
|
149 | chokidar
|
150 | .watch(filenames, {
|
151 | persistent: true,
|
152 | ignoreInitial: true,
|
153 | awaitWriteFinish: {
|
154 | stabilityThreshold: 50,
|
155 | pollInterval: 10,
|
156 | },
|
157 | })
|
158 | .on('all', function(type, filename) {
|
159 | if (!util.isCompilableExtension(filename)) {
|
160 | return
|
161 | }
|
162 |
|
163 | if (type === 'add' || type === 'change') {
|
164 | walk(filenames).catch(err => {
|
165 | console.error(err)
|
166 | })
|
167 | }
|
168 | })
|
169 | }
|
170 | }
|
171 |
|
172 | if (cliOptions.filenames.length) {
|
173 | await files(cliOptions.filenames)
|
174 | } else {
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | }
|