-
Notifications
You must be signed in to change notification settings - Fork 13
/
watcher.js
126 lines (104 loc) · 3.92 KB
/
watcher.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
const chokidar = require('chokidar')
const path = require('path')
const fs = require('fs-extra')
const {scanEntry, compileSingleFile, getSeperatedOutputFilePathWithRightExtension} = require('./scanner')
const {compile, getClassNameWithNameSpace, writeOutput} = require('./generator')
const getHandlers = ({dir, debounce, outPath, seperate, ignores, extensionName, extraTypeDef}, {verbose, dryrun}) => {
let debounceTimerID = 0
const changedFiles = new Set()
const removedFiles = new Set()
let regenerate = null
if (seperate) {
if (outPath === 'ef.hpp') outPath = outPath = '.efgenerated/ef'
ignores.push(outPath)
regenerate = () => {
for (let file of changedFiles) {
const outFilePath = getSeperatedOutputFilePathWithRightExtension(file, extensionName)
compileSingleFile({input: path.join(dir, file), output: path.join(outPath, outFilePath), base: dir, extraTypeDef}, {verbose, dryrun})
}
for (let file of removedFiles) {
const outFilePath = getSeperatedOutputFilePathWithRightExtension(file, extensionName)
const realOutFilePath = path.join(outPath, outFilePath)
fs.remove(realOutFilePath, (err) => {
if (err) return console.error(err)
})
}
changedFiles.clear()
removedFiles.clear()
}
} else {
let firstRun = true
let $results = null
let dest = ''
let currentVersion = ''
regenerate = () => {
if (firstRun) {
scanEntry({dir, outPath, seperate, ignores, extensionName, extraTypeDef}, {verbose, dryrun, watch: true}, (e, {$results: _results, dest: _dest, currentVersion: _currentVersion}) => {
if (e) return console.error(e)
$results = _results
dest = _dest
currentVersion = _currentVersion
firstRun = false
console.log('Compile cache generated.')
})
changedFiles.clear()
removedFiles.clear()
return
}
let reCompiledFileCount = 0
const handleFileRead = file => (e, source) => {
if (e) return console.error(e)
const filePath = path.relative(dir, file)
const fileName = path.basename(file, path.extname(file))
const dirName = path.dirname(filePath)
const [className, nameSpace] = getClassNameWithNameSpace(fileName, dirName)
if (verbose || dryrun) {
console.log('[V] Input file:', fileName)
console.log('[V] Relative dir:', dirName)
console.log('[V] Relative input path:', filePath)
console.log('[V] Generated class name:', className)
console.log('[V] Generated namesace:', nameSpace)
}
const [, result] = compile([filePath, {className, nameSpace, source}])
$results.set(filePath, result)
reCompiledFileCount += 1
if (reCompiledFileCount >= changedFiles.size) writeOutput({$results, dest, currentVersion}, {verbose, dryrun})
}
for (let file of removedFiles) {
const filePath = path.relative(dir, file)
$results.delete(filePath)
}
for (let file of changedFiles) fs.readFile(file, 'utf8', handleFileRead(file))
changedFiles.clear()
removedFiles.clear()
}
}
const fileUpdated = (file) => {
clearTimeout(debounceTimerID)
console.log(`File detected: ${file}`)
changedFiles.add(file)
removedFiles.delete(file)
debounceTimerID = setTimeout(regenerate, debounce * 1000)
}
const fileUnlinked = (file) => {
clearTimeout(debounceTimerID)
console.log(`File removed: ${file}`)
removedFiles.add(file)
changedFiles.delete(file)
debounceTimerID = setTimeout(regenerate, debounce * 1000)
}
return {fileUpdated, fileUnlinked}
}
const fileWatcher = ({dir, debounce, outPath, seperate, ignores, extensionName, extraTypeDef}, {verbose, dryrun}) => {
const {fileUpdated, fileUnlinked} = getHandlers({dir, debounce, outPath, seperate, ignores, extensionName, extraTypeDef}, {verbose, dryrun})
const watcher = chokidar.watch(['**/*.ef', '**/*.eft', '**/*.efml'], {
cwd: dir,
ignored: ignores
})
watcher
.on('add', fileUpdated)
.on('change', fileUpdated)
.on('unlink', fileUnlinked)
console.log('Change watcher has started.')
}
module.exports = fileWatcher