-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.js
62 lines (53 loc) · 1.85 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
let fs = require('fs')
let chokidar = require('chokidar')
let colors = require('colors/safe')
let isRunning = require('is-running')
// Define our watching parameters
let basePath = process.cwd()
let pidPath = basePath + '/storage/logs/swoole_http.pid'
let pid = fs.readFileSync(pidPath, 'utf8').split(',')[0];
let ready = false
let logger = (color, message, level = 'log', skipSignal = false) => {
console[level](colors[color](message))
if (ready && !skipSignal) {
sendSignal()
}
}
let sendSignal = () => {
// if (!isRunning(pid)) {
// ready = false
// logger('red', `PID ${pid} is not alive. Close watcher process.`, 'error')
// process.exit()
// }
process.kill(Number(pid), 'SIGUSR1')
logger('green', `Reloading process PID ${pid}...`, 'log', true)
}
// if (!isRunning(Number(pid))) {
// logger('red', `PID ${pid} is not alive.`, 'error')
// return
// } else {
// logger('green', `PID ${pid} is alive, start watching process...`)
// }
// Initialize watcher.
// Define your paths here.
let watcher = chokidar.watch([
basePath + '/app',
basePath + '/resources',
basePath + '/routes',
basePath + '/graphql',
], {
ignored: /(^|[\/\\])\../,
persistent: true
})
// Add event listeners.
watcher
.on('add', path => logger('yellow', `File ${path} has been added.`))
.on('change', path => logger('yellow', `File ${path} has been changed.`))
.on('unlink', path => logger('yellow', `File ${path} has been removed.`))
.on('addDir', path => logger('yellow', `Directory ${path} has been added.`))
.on('unlinkDir', path => logger('yellow', `Directory ${path} has been removed.`))
.on('error', error => logger('red', `Watcher error: ${error}`))
.on('ready', () => {
logger('green', 'Initial scan is finished. Ready for watching changes...')
ready = true
})