-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.js
174 lines (174 loc) · 7.32 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
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
const chokidar = require('chokidar')
const p = require('path')
const fs = require('fs')
const { saveError = e => console.log(e) } = global
class watcher {
constructor(paths, FileEvent) {
console.log('Intiating watch folders', paths)
let time = new Date().getTime()
this.folders = []
this.files = []
this.removedFiles = []
this.removedFolders = []
this.FEVENT = []
this.FileTimer
this.init = process.env.INITWATCH === 'True' ? true : false
this.FileEvent = FileEvent
this.watch = chokidar.watch(paths, { ignored: [/[\/\\]\./, /^null$/, /.*\.lock$/, /.*node_modules/], persistent: true, usePolling: true, interval: 1000, binaryInterval: 3000, alwaysStat: true, awaitWriteFinish: { stabilityThreshold: 1000, pollInterval: 1500 }, ignorePermissionErrors: false, atomic: 800 })
this.watch.on('add', (path, stats) => this.addFile(path, stats))
.on('addDir', (path, stats) => this.addDir(path, stats))
.on('change', (path, stats) => this.changeFile(path, stats))
.on('unlink', (path) => this.unlinkFile(path))
.on('unlinkDir', (path) => this.unlinkDir(path))
.on('error', e => this.fileError(e))
.on('ready', () => {
this.init = true
let dif = new Date().getTime() - time
console.log(`Initialized watch folders ${JSON.stringify(paths)} in: ${dif < 1000 ? `${dif}ms` : dif < 1000 * 60 ? `${dif / 1000}s` : dif < 1000 * 60 * 60 ? `${Math.floor(dif / (1000 * 60))} minutes ${dif % (1000 * 60) / 1000} seconds` : `${Math.floor((dif / 1000) * 60 * 60)} hours ${Math.floor(dif % (1000 * 60))} minutes`}. Watching changes.`);
this.getFolders()
this.runFile()
})
}
addPath = path => this.watch.add(path);
getFolders = () => {
this.folders = this.watch.getWatched()
let k = Object.keys(this.folders)
this.files = []
for (let i = 0; i < k.length; i++) {
let w = this.folders[k[i]]
for (let z = 0; z < w.length; z++) {
let path = p.resolve(k[i], w[z])
let file = fs.statSync(path)
this.files.push({ path, stats: file })
}
}
this.folders = k
console.log('Watching', this.files.length, 'files')
}
addNewFile(file, isRename) { this.que('addFile', file, isRename) }
addDirectory(file, isRename) { this.que('addDir', file, isRename) }
onchangeFile(file, stats) { this.que('changeFile', file, stats) }
unlinkFolder(path) { setTimeout(() => this.que('deleteDir', { path }), 2000) }
unlink(path) { setTimeout(() => this.que('deleteFile', { path }), 2000) }
async addFile(path, stats) {
if (this.init) {
this.files.push({ path, stats })
let r = null
let c = () => {
for (let i = 0; i < this.removedFiles.length; i++) {
if (this.removedFiles[i].stats.size === stats.size && this.removedFiles[i].stats.birthtimeMs === stats.birthtimeMs && this.removedFiles[i].stats.dev === stats.dev && p.extname(path) === p.extname(this.removedFiles[i].path)) {
r = this.removedFiles.splice(i, 1)
i = Infinity
}
}
}
await c()
this.addNewFile({ path, stats }, r ? r[0] : false)
}
}
async addDir(path, stats) {
if (this.init) {
this.folders.push(path)
this.files.push({ path: path, stats: fs.statSync(path) })
let r = null
let c = () => {
for (let i = 0; i < this.removedFolders.length; i++) {
if (this.removedFolders[i].stats.size === stats.size && this.removedFolders[i].stats.birthtimeMs === stats.birthtimeMs && this.removedFolders[i].stats.dev === stats.dev) {
r = this.removedFolders.splice(i, 1)
i = Infinity
}
}
}
await c()
this.addDirectory({ path, stats }, r ? r[0] : false)
}
}
changeFile(path, stats) {
if (this.init) this.onchangeFile({ path, stats })
}
unlinkFile(path) {
if (this.init) {
for (let i = 0; i < this.files.length; i++) {
if (absolute(this.files[i].path) === absolute(path)) {
let r = this.files.splice(i, 1)[0]
if (r) this.removedFiles.push(r)
i = Infinity
}
}
this.unlink(path)
}
}
unlinkDir(path) {
if (this.init) {
let a = false
for (let i = 0; i < this.files.length; i++) {
if (this.files[i].path && path && absolute(this.files[i].path) === absolute(path)) {
let r = this.files.splice(i, 1)[0]
if (r) this.removedFolders.push(r)
a = true
i = Infinity
}
}
for (let i = 0; i < this.folders.length; i++) {
if (this.folders[i] && path && absolute(this.folders[i]) === absolute(path)) {
let r = this.folders.splice(i, 1)[0]
if (r && !this.removedFolders.find(u => u.path === r.path)) this.removedFolders.push(r)
i = Infinity
}
}
this.unlinkFolder(path)
}
}
que(type, u, a) {
this.FEVENT.push([type, u, a])
}
fileError(error) {
if (error.code === 'EBUSY') return
if (error.code === 'UNKNOWN') return
if (error.code === 'EBADF') return
console.error('Error happened', error);
if (this.error) this.error(error)
}
runFile() {
clearTimeout(this.FileTimer)
let set = () => this.FileTimer = setTimeout(() => this.runFile(), 100)
if (this.FEVENT.length > 0) {
let file = this.FEVENT.splice(0, 1)[0]
if (file[0] === 'deleteFile' || file[0] === 'deleteDir') {
let a = false
if (file[0] === 'deleteFile') {
for (let i = 0; i < this.removedFiles.length; i++) {
let path = this.removedFiles[i].path
if (file[1].path === path) {
a = true
i = Infinity
}
}
} else {
for (let i = 0; i < this.removedFolders.length; i++) {
let path = this.removedFolders[i].path
if (file[1].path === path) {
a = true
i = Infinity
}
}
}
if (!a) return this.runFile()
}
if (this.FileEvent && typeof this.FileEvent === 'function') {
try {
this.FileEvent(file[0], file[1], file[2])
} catch (e) {
saveError(e)
}
this.runFile()
} else {
saveError(`UNHANDLED FILE EVENT: ${JSON.stringify(file)}`)
this.runFile()
}
} else {
set()
}
}
}
module.exports = watcher