-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (78 loc) · 2.18 KB
/
index.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
var io = require('socket.io-client');
var chokidar = require('chokidar');
var yaml = require('js-yaml');
var fs = require('fs');
var netrc = require('netrc');
var userHome = '';
process.argv.forEach((val, index) => {
if (val.indexOf('homeDir=') !== -1) {
userHome = val.replace('homeDir=', '');
}
});
gaConfigPath = userHome + '/.ga-config';
netrcPath = userHome + '/.netrc';
if (!(fs.existsSync(gaConfigPath) && fs.existsSync(netrcPath))) {
console.log('User Config does not exists');
return;
}
var connected = false;
// get workspace path to watch after
var config = yaml.safeLoad(fs.readFileSync(gaConfigPath, 'utf8'));
// get user github username for creating websocket room
var myNetrc = netrc(netrcPath);
var room = myNetrc['ga-extra'].login;
// connect to GreyAtom's websocket server
// var socket = io.connect('https://chat.commit.live', { reconnect: true });
var socket = io.connect('http://13.126.157.180:5000', { reconnect: true });
var typeOfEvent = "remote-file-change";
socket.on('connect', function() {
connected = true;
socket.emit('join', {
room: room
});
});
var getPathToFile = function (path) {
var index = path.lastIndexOf('/');
return pathToFile = path.substr(0, index);
};
var emitEvent = function (path, event) {
var data = {
"type": typeOfEvent,
"title": event,
"message": path,
};
var payload = JSON.stringify(data);
socket.emit('my_room_event', {
room: room,
data: payload
});
};
var commonCallback = function (path, event) {
var pathToFile = getPathToFile(path);
emitEvent(pathToFile, event);
};
var commonCallbackWithEvent = function (event) {
return function (path) {
commonCallback(path, event);
}
}
var watcher = chokidar.watch(config[":workspace"], {
ignored: /(^|[\/\\])\../,
persistent: true,
depth: 0
});
// Add event listeners.
watcher
.on('add', commonCallbackWithEvent('addFile'))
.on('change', (path) => {
emitEvent(path, 'changeFile');
})
.on('unlink', (path) => {
var pathToFile = getPathToFile(path);
if (!fs.existsSync(pathToFile)) {
pathToFile = getPathToFile(pathToFile);
}
emitEvent(pathToFile, 'unlinkFile');
})
.on('addDir', commonCallbackWithEvent('addDir'))
.on('unlinkDir', commonCallbackWithEvent('unlinkDir'));