forked from gabrielflorit/script-8.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
29 lines (23 loc) · 708 Bytes
/
server.js
File metadata and controls
29 lines (23 loc) · 708 Bytes
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
const chokidar = require('chokidar')
const WebSocket = require('ws')
const fs = require('fs')
const wss = new WebSocket.Server({ port: 8989 })
wss.on('connection', ws => {
console.log('client connected')
ws.on('error', error => console.log(error.code))
ws.on('close', () => console.log('client disconnected'))
})
if (!process.argv[2]) {
console.error('Please specify a directory.')
process.exit(1)
}
const dir = `${process.argv[2]}/*.*`
console.log(`Watching ${dir}`)
chokidar.watch(dir).on('change', path => {
console.log(path)
const str = fs.readFileSync(path, 'utf8').replace(/\/\* eslint.*\*\/\n/, '')
console.log(str)
wss.clients.forEach(client => {
client.send(str)
})
})