UNPKG

7.37 kBJavaScriptView Raw
1import '@babel/polyfill'
2import defaults from 'lodash/defaults'
3import outputFileSync from 'output-file-sync'
4import { sync as mkdirpSync } from 'mkdirp'
5import { performance } from 'perf_hooks'
6import rimraf from 'rimraf'
7import slash from 'slash'
8import path from 'path'
9import chalk from 'chalk'
10import glob from 'glob'
11import fs from 'fs'
12
13import * as util from './util'
14
15export 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.
26Start by creating a {bold elodin.config.js} file.
27For 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.
41For 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 // config.generator.filePattern
66 // .map(v => process.cwd() + '/**/' + v)
67 // .map(path => rimraf.sync(path))
68
69 // console.log('Successfully cleaned all files.')
70 } catch (e) {
71 // TODO: throw sth
72 // console.log(e)
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 // if (successLog.length + failLog.length > 0) {
126 // console.log(successLog + failLog)
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 // async function write(src, base) {
177 // let relative = path.relative(base, src)
178
179 // if (!util.isCompilableExtension(relative)) {
180 // return false
181 // }
182
183 // const dest = getDest(relative, base)
184
185 // try {
186 // const res = await util.compile(
187 // src,
188 // defaults(
189 // {
190 // sourceFileName: slash(path.relative(dest + '/..', src)),
191 // },
192 // elodinOptions
193 // )
194 // )
195
196 // if (!res) return false
197
198 // util.chmod(src, dest)
199
200 // return true
201 // } catch (err) {
202 // if (cliOptions.watch) {
203 // console.error(err)
204 // return false
205 // }
206
207 // throw err
208 // }
209 // }
210
211 // function getDest(filename, base) {
212 // if (cliOptions.relative) {
213 // return path.join(base, cliOptions.outDir, filename)
214 // }
215 // return path.join(cliOptions.outDir, filename)
216 // }
217
218 // async function handleFile(src, base) {
219 // return await write(src, base)
220 // }
221
222 // async function handle(filenameOrDir) {
223 // if (!fs.existsSync(filenameOrDir)) return 0
224
225 // const stat = fs.statSync(filenameOrDir)
226
227 // if (stat.isDirectory()) {
228 // const dirname = filenameOrDir
229
230 // let count = 0
231
232 // const files = util.readdir(dirname, false)
233
234 // for (const filename of files) {
235 // const src = path.join(dirname, filename)
236
237 // const written = await handleFile(src, dirname)
238 // if (written) count += 1
239 // }
240
241 // return count
242 // } else {
243 // const filename = filenameOrDir
244 // const written = await handleFile(filename, path.dirname(filename))
245
246 // return written ? 1 : 0
247 // }
248 // }
249
250 // if (!cliOptions.skipInitialBuild) {
251 // let compiledFiles = 0
252 // for (const filename of cliOptions.filenames) {
253 // compiledFiles += await handle(filename)
254 // }
255
256 // console.log(
257 // `Successfully compiled ${compiledFiles} ${
258 // compiledFiles !== 1 ? 'files' : 'file'
259 // } with Babel.`
260 // )
261 // }
262
263 // if (cliOptions.watch) {
264 // const chokidar = util.requireChokidar()
265
266 // filenames.forEach(function(filenameOrDir) {
267 // const watcher = chokidar.watch(filenameOrDir, {
268 // persistent: true,
269 // ignoreInitial: true,
270 // awaitWriteFinish: {
271 // stabilityThreshold: 50,
272 // pollInterval: 10,
273 // },
274 // })
275
276 // ;['add', 'change'].forEach(function(type) {
277 // watcher.on(type, function(filename) {
278 // handleFile(
279 // filename,
280 // filename === filenameOrDir
281 // ? path.dirname(filenameOrDir)
282 // : filenameOrDir
283 // ).catch(err => {
284 // console.error(err)
285 // })
286 // })
287 // })
288 // })
289 // }
290}