-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·70 lines (58 loc) · 1.58 KB
/
index.js
File metadata and controls
executable file
·70 lines (58 loc) · 1.58 KB
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
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
function printHelpAndExit(exitCode) {
console.error([
'Usage: ' + __filename + ' [-p] <file-to-watch>',
'',
'Options:',
' -h, --help Show this screen',
' -p, --public Start ngrok proxy to let others connect to this server',
].join('\n'));
process.exit(exitCode);
}
var fileName, allowPublicAccess;
process.argv.slice(2).forEach(function(arg) {
if (arg == '-h' || arg == '--help') {
printHelpAndExit(0);
}
if (arg == '-p' || arg == '--public') {
allowPublicAccess = true;
} else {
fileName = arg;
}
});
if (!fileName) {
printHelpAndExit(1);
}
function getSnapshot() {
return {
content: fs.readFileSync(fileName, 'utf8'),
modifiedAt: fs.statSync(fileName).mtime.getTime(),
};
}
var snapshots = [getSnapshot()];
io.on('connection', function (socket) {
socket.emit('init', snapshots);
});
fs.watch(fileName, function() {
var last = snapshots[snapshots.length - 1];
var current = getSnapshot();
if (current.content !== last.content && current.content.length > 0) {
io.emit('change', current);
snapshots.push(current);
}
});
console.log('Serving ' + fileName + ' on http://localhost:3030/');
if (allowPublicAccess) {
var ngrok = require('ngrok');
ngrok.connect(3030, function (err, url) {
console.log('Public URL:', url);
});
}
app.use(express.static(path.join(__dirname, 'public')));
server.listen(3030);