forked from FGRibreau/filesync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay.js
69 lines (55 loc) · 1.52 KB
/
relay.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
'use strict';
var io = require('socket.io-client');
var gaze = require('gaze');
var fs = require('fs');
var path = require('path');
var logger = require('winston');
var config = require('./config')(logger);
var directory = path.resolve(__dirname, process.argv[2]);
if (!directory) {
logger.error("Usage: node server.js /path/to/directory");
process.exit(1);
}
logger.info('listening on %s', directory);
var SOCKET_IO_URL = config.server.exposed_endpoint + '/?access_token=' + config.auth.token;
logger.info('connecting...');
var sio = io(SOCKET_IO_URL, {
transports: ['websocket', 'polling'],
multiplex: false
});
sio.on('connect', function() {
logger.info('connected!');
});
gaze(directory, function(err, watcher) {
if (err) {
throw err;
}
// Get all watched files
this.watched(function(err, watched) {
console.log(watched);
});
// On file changed
this.on('changed', function(filepath) {
sio.emit('file:changed',
path.basename(filepath),
Date.now(),
fs.readFileSync(filepath, 'utf-8') // @todo use async mode
);
});
// On file added
this.on('added', function(filepath) {
console.log(filepath + ' was added');
});
// On file deleted
this.on('deleted', function(filepath) {
console.log(filepath + ' was deleted');
});
// On changed/added/deleted
this.on('all', function(event, filepath) {
console.log(filepath + ' was ' + event);
});
// Get watched files with relative paths
this.relative(function(err, files) {
console.log(files);
});
});