-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
196 lines (155 loc) · 5.38 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#! /usr/bin/env node
/* eslint-env es6 */
const server = require('http').createServer();
const WebSocketServer = require('ws').Server;
const wss = new WebSocketServer({ server: server });
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const api = require('./lib/api');
const wsMessaging = require('./lib/ws-routing');
const nodePath = require('path');
const Client = require('./lib/client');
const lockFile = require('lockfile');
const fs = require('fs');
const exec = require('child_process').exec;
const pathIsInside = require('path-is-inside');
const Stats = require('./lib/web-code-stats.compiled');
const pathJoin = require('path').join;
const pathDirname = require('path').dirname;
function getModuleRoot(m) {
return pathDirname(require.resolve(m + '/package.json'));
}
function puts(error, stdout, stderr) { console.log(stdout); console.log(stderr); }
let lastWorkingDir = '';
const args = process.argv.slice(2).join(' ').trim();
const path = args && nodePath.resolve(args);
lastWorkingDir = path || false;
const lockfile = require('path').join(__dirname, 'web-code-' + port + '.lock');
lockFile.lock(lockfile, {}, function (err) {
// There is a lock file running
if (err) {
// Check if the stored pid matches the current one
const data = fs.readFileSync(lockfile, 'utf8').split('\n');
const storedDaemonPID = data[0];
let processIsRunning = true;
try {
process.kill(storedDaemonPID, 0);
} catch (e) {
processIsRunning = false;
}
// There is a daemon already running
if (processIsRunning) {
data[1] = lastWorkingDir;
if (lastWorkingDir) {
// Update the lockfile with new working dir, tell the daemon
fs.writeFileSync(lockfile, data.join('\n'));
// Daemon
process.kill(storedDaemonPID, 'SIGPIPE');
} else {
// Process already exists so try to message to it.
console.log('Web-code daemon is already running, with pid:', storedDaemonPID);
}
return;
}
}
fs.writeFileSync(lockfile, process.pid + '\n' + lastWorkingDir);
app.use(express.static(__dirname + '/static', {
maxAge: 3600 * 1000 * 24
}));
app.use(express.static(getModuleRoot('sw-toolbox'), {
maxAge: 3600 * 1000 * 24
}));
app.use('/vs/', express.static(pathJoin(getModuleRoot('monaco-editor'), '/min/vs'), {
maxAge: 3600 * 1000 * 24
}));
app.use('/icons/', express.static(pathJoin(getModuleRoot('file-icons'), '/fonts'), {
maxAge: 3600 * 1000 * 24
}));
app.use('/axe/', express.static(getModuleRoot('axe-core'), {
maxAge: 3600 * 1000 * 24
}));
app.use('/contextmenu/', express.static(getModuleRoot('contextmenu'), {
maxAge: 3600 * 1000 * 24
}));
app.use('/fira/', express.static(__dirname + '/node_modules/FiraCode/distr', {
maxAge: 3600 * 1000 * 24
}));
app.use('/api/', api);
function heartbeat() {
this.isAlive = true;
}
setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.terminate();
ws.isAlive = false;
ws.ping('', false, true);
});
}, 5000);
wss.on('connection', function connection(ws) {
ws.on('message', wsMessaging.wsRouting);
ws.webCodeClient = new Client();
ws.webCodeClient.on('change', function fn(stats) {
ws.send(JSON.stringify(['FS_CHANGE', null, stats.toDoc()]), e => e && console.log(e));
});
ws.webCodeClient.on('add', function fn(stats) {
ws.send(JSON.stringify(['FS_ADD', null, stats.toDoc()]), e => e && console.log(e));
});
ws.webCodeClient.on('unlink', function fn(obj) {
ws.send(JSON.stringify(['FS_UNLINK', null, obj]), e => e && console.log(e));
});
ws.webCodeClient.on('addDir', function fn(stats) {
ws.send(JSON.stringify(['FS_ADD', null, stats.toDoc()]), e => e && console.log(e));
});
ws.webCodeClient.on('unlinkDir', function fn(obj) {
ws.send(JSON.stringify(['FS_UNLINK', null, obj]), e => e && console.log(e));
});
ws.on('close', function close() {
ws.webCodeClient.destroy();
ws.webCodeClient = null;
});
ws.isAlive = true;
ws.on('pong', heartbeat);
ws.send(wsMessaging.wsSendFormat('HANDSHAKE', {
path: lastWorkingDir || false
}));
});
server.on('request', app);
server.listen(port, function () {
/* eslint no-console: 0 */
console.log('Web Code Server running with PID:', process.pid);
console.log('Open up: http://127.0.0.1:' + server.address().port);
// if an address is set to open then do otherwise just run the server
if (lastWorkingDir) exec('termux-open-url http://127.0.0.1:' + server.address().port, puts);
});
function exit() {
lockFile.unlockSync(lockfile);
}
process.on('SIGTERM', function() {
process.exit();
});
process.on('SIGINT', function() {
process.exit();
});
process.on('SIGPIPE', function() {
const data = fs.readFileSync(lockfile, 'utf8').split('\n');
const path = nodePath.resolve(data[1]);
const stats = fs.statSync(path);
const statsObj = Stats.fromNodeStats(path, stats).toDoc();
// If it is a file in an already open window then open it there
if (stats.isFile()) {
for (const ws of wss.clients) {
if (ws.editorState && ws.editorState.currentlyOpenedPath) {
if(pathIsInside(path, ws.editorState.currentlyOpenedPath)) {
ws.send(wsMessaging.wsSendFormat('OPEN_FILE', statsObj));
return;
}
}
}
}
// Opening a new browser tab to that location.
lastWorkingDir = path;
exec('termux-open-url http://127.0.0.1:' + server.address().port, puts);
});
process.on('exit', exit);
});