forked from capaj/chokidar-socket-emitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
92 lines (86 loc) · 2.4 KB
/
server.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
'use strict'
const chokidar = require('chokidar')
const path = require('path')
const socketsConnected = []
module.exports = (opts, cb) => {
let baseURL
let pjson
let error
try {
pjson = require(path.join(opts.dir || path.dirname(require.main.filename), 'package.json'))
} catch (err) {
error = err
}
if (!error) {
baseURL = pjson.jspm && pjson.jspm.directories && pjson.jspm.directories.baseURL || pjson.directories && pjson.directories.baseURL
}
let app = opts.app
if (!app) {
app = require('http').createServer()
}
var io = require('socket.io')(app)
if (!opts.app) {
let port = opts.port || 9111
app.listen(port, () => {
console.log('chokidar-socket-emitter listening on ' + port)
cb && cb()
})
}
const pathToWatch = opts.path || baseURL || '.'
let ignoredPaths = [
/[\/\\]\./,
// Ignore relative, top-level dotfiles as well (e.g. '.gitignore').
/^\.[^\/\\]/,
'node_modules/**',
(baseURL ? baseURL + '/' : '') + 'jspm_packages/**',
'.git/**'
]
let chokidarOpts = Object.assign({
ignored: ignoredPaths,
ignoreInitial: true
}, opts.chokidar)
console.log('chokidar watching ', path.resolve(pathToWatch))
var watcher = chokidar.watch(path.resolve(pathToWatch), chokidarOpts).on('all', (event, onPath) => {
let absolutePath = path.join(process.cwd(), onPath)
if (opts.relativeTo) {
onPath = path.relative(opts.relativeTo, onPath)
} else if (baseURL) {
onPath = path.relative(baseURL, onPath)
}
if (path.sep === '\\') {
onPath = onPath.replace(/\\/g, '/')
} else {
}
console.log('File ', onPath, ' emitted: ' + event)
socketsConnected.forEach((socket) => {
socket.emit(event, {path: onPath, absolutePath})
})
})
io.on('connection', (socket) => {
let index = socketsConnected.push(socket)
socket.on('disconnect', () => {
socketsConnected.splice(index - 1, 1)
})
socket.on('identification', (name) => {
console.log('connected client: ' + name)
})
if (pjson) {
socket.on('package.json', function (fn) {
fn(pjson)
})
}
})
return {
io: io,
app: app,
watcher: watcher,
close: (callback) => {
watcher.close()
console.log('closing chokidar-socket-emitter')
socketsConnected.forEach((socket) => {
socket.disconnect()
})
io.httpServer.close(callback)
}
}
}