-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch-files.js
246 lines (217 loc) · 6.99 KB
/
watch-files.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
import {
MATCH,
isFontCustomFile,
isViewCustomFile,
isViewFile,
isViewLogicFile,
isViewDataGraphqlFile,
isViewGraphqlFile,
} from './match-files.js'
import { debounce } from 'debounce'
import { promises as fs, existsSync } from 'fs'
import { getFontId } from './fonts.js'
import chalk from 'chalk'
import chokidar from 'chokidar'
import getPointsOfUse from './get-points-of-use.js'
import path from 'path'
export default async function watchFiles({ morpher }) {
let watcher = chokidar.watch(MATCH, {
cwd: morpher.src,
ignored: [
'**/view.js',
'**/flow.js',
'**/view.module.css',
'**/data.js',
'**/index.js',
'**/*.graphql.js',
'DesignSystem/Fonts/*.js',
'DesignSystem/Fonts/*.css',
],
ignoreInitial: true,
// awaitWriteFinish: true,
})
let processing = false
let queue = []
let processEvents = debounce(async () => {
if (queue.length === 0 || processing) {
return
}
let queueClone = [...queue]
queue = []
processing = true
try {
await processQueue({ queue: queueClone, morpher })
} catch (error) {
console.error(error)
process.exit(1)
}
processing = false
if (queue.length > 0) {
processEvents()
}
}, 10)
function onEvent({ file, op }) {
queue.push({ file: path.join(morpher.src, file), op })
processEvents()
}
watcher.on('add', (file) => onEvent({ file, op: 'add' }))
watcher.on('change', (file) => onEvent({ file, op: 'change' }))
watcher.on('unlink', (file) => onEvent({ file, op: 'unlink' }))
return watcher
}
async function processQueue({ queue, morpher }) {
let filesToProcess = {
filesView: new Set(),
filesViewGraphql: new Set(),
filesViewLogic: new Set(),
filesViewCustom: new Set(),
filesFontCustom: new Set(),
}
processAddedOrChanged({
files: queue.filter((item) => item.op !== 'unlink'),
morpher,
filesToProcess,
})
await processUnlinked({
files: queue.filter((item) => item.op === 'unlink'),
morpher,
filesToProcess,
})
return morpher.processFiles(filesToProcess)
}
async function processUnlinked({ files, morpher, filesToProcess }) {
await Promise.all(
files.map(async ({ file }) => {
morpher.verbose &&
console.log(
chalk.magenta('X'),
path.basename(path.dirname(file)),
chalk.dim(file.replace(morpher.src, '').replace(/^\//, ''))
)
if (isFontCustomFile(file)) {
let id = getFontId(file)
let font = morpher.customFonts.get(id)
font.delete(file)
if (font.size === 0) {
morpher.customFonts.delete(id)
}
} else {
let viewFile = path.join(path.dirname(file), 'view.blocks')
let view = morpher.viewsToFiles.get(viewFile)
if (!view) return
processPointsOfUse({ view, morpher, filesToProcess })
filesToProcess.filesView.delete(view.file)
let viewFilesSet = morpher.viewsById.get(view.id)
viewFilesSet.delete(view.file)
if (viewFilesSet.size === 0) {
morpher.viewsById.delete(view.id)
}
morpher.viewsToFiles.delete(view.file)
if (isViewFile(file)) {
filesToProcess.filesView.delete(file)
try {
await fs.unlink(file.replace('view.blocks', 'view.js'))
} catch (error) {}
} else if (isViewLogicFile(file)) {
if (files.every((item) => item.file !== view.file)) {
filesToProcess.filesView.add(view.file)
}
filesToProcess.filesViewLogic.delete(file)
let dataFile = path.join(path.dirname(file), 'data.graphql')
if (existsSync(dataFile)) {
filesToProcess.filesViewGraphql.add(dataFile)
}
} else if (isViewDataGraphqlFile(file)) {
if (files.every((item) => item.file !== view.file)) {
filesToProcess.filesView.add(view.file)
}
filesToProcess.filesViewGraphql.delete(file)
let logicFile = path.join(path.dirname(file), 'logic.js')
if (existsSync(logicFile)) {
filesToProcess.filesViewLogic.add(logicFile)
}
} else if (isViewCustomFile(file)) {
filesToProcess.filesViewCustom.delete(file)
} else if (isViewGraphqlFile(file)) {
filesToProcess.filesViewGraphql.delete(file)
try {
await fs.unlink(`${file}.js`)
} catch (error) {}
}
}
})
)
}
function processAddedOrChanged({ files, morpher, filesToProcess }) {
files.forEach(({ file, op }) => {
morpher.verbose &&
console.log(
chalk.yellow(op.toUpperCase()[0]),
path.basename(path.dirname(file)),
chalk.dim(file.replace(morpher.src, '').replace(/^\//, ''))
)
if (isFontCustomFile(file)) {
filesToProcess.filesFontCustom.add(file)
} else {
if (isViewFile(file)) {
filesToProcess.filesView.add(file)
let logicFile = path.join(path.dirname(file), 'logic.js')
if (existsSync(logicFile)) {
filesToProcess.filesViewLogic.add(logicFile)
}
let dataFile = path.join(path.dirname(file), 'data.graphql')
if (existsSync(dataFile)) {
filesToProcess.filesViewGraphql.add(dataFile)
}
let parentFile = path.join(
path.dirname(path.dirname(file)),
'view.blocks'
)
if (existsSync(parentFile)) {
filesToProcess.filesView.add(parentFile)
}
} else if (isViewLogicFile(file)) {
filesToProcess.filesView.add(
path.join(path.dirname(file), 'view.blocks')
)
filesToProcess.filesViewLogic.add(file)
let dataFile = path.join(path.dirname(file), 'data.graphql')
if (existsSync(dataFile)) {
filesToProcess.filesViewGraphql.add(dataFile)
}
} else if (isViewDataGraphqlFile(file)) {
filesToProcess.filesView.add(
path.join(path.dirname(file), 'view.blocks')
)
filesToProcess.filesViewGraphql.add(file)
let logicFile = path.join(path.dirname(file), 'logic.js')
if (existsSync(logicFile)) {
filesToProcess.filesViewLogic.add(logicFile)
}
} else if (isViewCustomFile(file)) {
filesToProcess.filesViewCustom.add(file)
} else if (isViewGraphqlFile(file)) {
filesToProcess.filesViewGraphql.add(file)
}
let viewFile = path.join(path.dirname(file), 'view.blocks')
let view = morpher.viewsToFiles.get(viewFile)
if (!view) return
processPointsOfUse({ view, morpher, filesToProcess })
}
})
}
function processPointsOfUse({ view, morpher, filesToProcess }) {
let { filesView, filesViewLogic, filesViewGraphql } = getPointsOfUse({
view,
viewsToFiles: morpher.viewsToFiles,
})
for (let file of filesView) {
filesToProcess.filesView.add(file)
}
for (let file of filesViewLogic) {
filesToProcess.filesViewLogic.add(file)
}
for (let file of filesViewGraphql) {
filesToProcess.filesViewGraphql.add(file)
}
}