-
Notifications
You must be signed in to change notification settings - Fork 3
/
watch.js
160 lines (126 loc) · 4.11 KB
/
watch.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const EventEmitter = require('events')
const { DepGraph } = require('dependency-graph')
const fs = require('fs-extra')
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, CREATE_DEPENDENCY,
UPDATE_TARGET, UPDATE_DEPENDENCY,
DELETE_TARGET, DELETE_DEPENDENCY,
} = require('./src/events')
module.exports = withLogger(tsconfigWatch)
function tsconfigWatch (options) {
const { log } = options
const external = new EventEmitter()
const handleError = (file) => error => {
file
? log.error(`Error while processing ‘${file}’:`)
: log.error(`Error from file watcher:`)
log.errorError(error)
external.emit(ERROR, error)
}
const watcher = watch(options, true)
watcher.on(ERROR, handleError())
watcher.on(READY, () => external.emit(READY))
external.close = () => watcher.close()
const dependenciesMap = new DepGraph()
let _queue = Promise.resolve()
const queue = (action) => { _queue = _queue.then(action) }
watcher.on(CREATE_TARGET, file => queue(() => (
add(file, true)
.then(() => build(file, options))
.catch(handleError(file))
)))
watcher.on(UPDATE_TARGET, file => queue(() => (
build(file, options)
.catch(handleError(file))
)))
watcher.on(DELETE_TARGET, file => queue(() => (
build(file, options)
.then(() => remove(file, true))
.catch(handleError(file))
)))
watcher.on(CREATE_DEPENDENCY, file => queue(() => (
add(file)
.then(() => build(file, options))
.catch(handleError(file))
)))
watcher.on(UPDATE_DEPENDENCY, file => queue(() => (
build(file, options)
.catch(handleError(file))
)))
watcher.on(DELETE_DEPENDENCY, file => queue(() => (
build(file, options)
.then(() => remove(file))
.catch(handleError(file))
)))
return external
// Event Handlers
async function add(filepath, buildable) {
const data = {
buildable,
}
dependenciesMap.addNode(filepath, data)
// in case the node has already been added as a dependency
if (buildable) {
// override data
dependenciesMap.setNodeData(filepath, data)
// don't watch redundantly
watcher.clearDependency(filepath)
}
}
async function remove(filepath, buildable) {
if (buildable) {
await fs.remove(`${filepath}on`)
}
const dependencies = dependenciesMap.dependenciesOf(filepath)
dependenciesMap.removeNode(filepath)
return Promise.all(
dependencies
.filter(dependency => dependenciesMap.dependantsOf(dependency).length === 0)
.filter(dependency => !dependenciesMap.getNodeData(dependency).buildable)
.map(dependency => remove(dependency))
)
}
// Helpers
async function build (filepath, options) {
return Promise.all(
[filepath]
.concat(dependenciesMap.dependantsOf(filepath))
.map(fp => delete require.cache[fp] && fp)
.filter(fp => dependenciesMap.getNodeData(fp).buildable)
.filter(fp => fs.existsSync(fp))
.map(fp => make(fp, options).then(updateDependencies(fp)))
)
}
async function updateDependencies (filepath) {
const directDependencies = extractDependencies(filepath) || []
const transitiveDependencies = dependenciesMap.dependenciesOf(filepath)
const extraDependencies = transitiveDependencies.filter(d => !directDependencies.includes(d))
extraDependencies.forEach(dependency => {
// does not touch deep dependencies, therefore clears obsolete direct dependencies
dependenciesMap.removeDependency(filepath, dependency)
if (dependenciesMap.dependantsOf(dependency).length === 0) {
watcher.clearDependency(dependency)
if (!dependenciesMap.getNodeData(dependency).buildable) {
dependenciesMap.removeNode(dependency)
}
}
})
const newDependecies = directDependencies.filter(d => !transitiveDependencies.includes(d))
return Promise.all(newDependecies.map(async dependency => {
if (dependenciesMap.hasNode(dependency)) {
dependenciesMap.addDependency(filepath, dependency)
return
}
add(dependency)
dependenciesMap.addDependency(filepath, dependency)
watcher.addDependency(dependency)
return updateDependencies(dependency)
}))
}
}