-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevServer.js
41 lines (37 loc) · 1.14 KB
/
devServer.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
const cluster = require('cluster')
const chokidar = require('chokidar')
const fs = require('fs')
const p = require('path')
const watchConfig = {
dir: [ 'src', 'app.js' ],
options: {
ignored: /(^|[/\\])\../,
persistent: true
}
}
if (cluster.isMaster) {
let worker = cluster.fork()
let isReady = false
chokidar.watch(watchConfig.dir, watchConfig.options)
.on('ready', () => (isReady = true))
.on('add', path => {
if (isReady && path.startsWith('src/routes')) {
const filename = `${__dirname}/src/controllers/${p.basename(path, p.extname(path))}Controller.js`
console.log(filename)
if (!fs.existsSync(filename)) {
fs.createReadStream(`${__dirname}/src/controllers/.ExampleController.js`).pipe(fs.createWriteStream(filename))
}
}
})
.on('change', path => {
console.log(`${path} changed`)
if (p.extname(path) !== '.js') return
worker && worker.kill()
worker = cluster.fork().on('listening', (address) => {
console.log(`[master] listening: worker ${worker.id}, pid:${worker.process.pid}`)
})
})
}
if (cluster.isWorker) {
require('./app')
}