-
Notifications
You must be signed in to change notification settings - Fork 3
/
once.js
67 lines (51 loc) · 1.42 KB
/
once.js
1
2
3
4
5
6
7
8
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const extractDependencies = require('./src/extract-dependencies')
const make = require('./src/make')
const watch = require('./src/watcher')
const withLogger = require('./src/with-logger')
const {
ERROR,
READY,
CREATE_TARGET: FIND,
} = require('./src/events')
module.exports = withLogger(tsconfig)
function tsconfig (options) {
const { log } = options
return new Promise((resolve, reject) => {
const errors = []
const handleError = (file) => error => {
file
? log.error(`Error while processing ${file}`)
: log.error(`Error from file crawler`)
log.errorError(error)
errors.push(error)
}
const watcher = watch(options)
watcher.on(ERROR, handleError())
const all = []
watcher.on(FIND, file => all.push(build(file, options).catch(handleError(file))))
watcher.on(READY, () => {
log.silly('All files collected, finishing processing')
watcher.close()
Promise.all(all).then(() => {
if (errors.length > 0) {
log.info(`Processing complete, with ${errors.length} errors, see above for details`)
reject(errors)
} else {
log.info('Processing complete, without errors')
resolve()
}
})
})
})
}
async function build (filepath, options) {
clearCache(filepath)
return make(filepath, options)
}
function clearCache (filepath) {
const dependencies = extractDependencies(filepath)
if (dependencies) {
dependencies.forEach(clearCache)
}
delete require.cache[filepath]
}