-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwswatch
More file actions
executable file
·55 lines (45 loc) · 1.79 KB
/
wswatch
File metadata and controls
executable file
·55 lines (45 loc) · 1.79 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
#!/usr/bin/env node
// server for wswatch, serves the output of another program via WebSockets etc.
"use strict";
var argparse = require('argparse');
var connect = require('connect');
var socket = require('socket.io');
var child = require('child_process');
var parser = new argparse.ArgumentParser({
version: '0.1.0',
description: "Unix watch(1) in the browser"
});
parser.addArgument([ '-p', '--port' ], { type: parseInt, required: true, help: "HTTP port to serve the view" });
parser.addArgument([ '-n', '--interval' ], { defaultValue: 2, help: "Update interval in seconds (default: 2)" });
parser.addArgument([ 'command' ], { help: "Program to run" });
parser.addArgument([ 'args' ], { nargs: '...', help: "Arguments for program to run" });
var args = parser.parseArgs();
var app = connect();
app.use(connect.static('public'));
var server = app.listen(args.port);
var io = socket.listen(server);
io.set('log level', 1); // reduce logging
var command = args.command;
if (args.args) {
command += ' ' + args.args.join(' ');
}
var screen; // cache of last screen to speed up initial paint when new clients connect
function run() {
// exec rather than spawn to handle shell syntax within command
child.exec(command, function (error, stdout, stderr) {
screen = stdout.toString();
io.sockets.emit('screen', screen);
if (stderr) {
console.error("error: " + stderr);
}
if (error !== null) {
console.error("exec error: " + error);
}
});
}
run(); // initial run to populate cache
setInterval(run, args.interval * 1000);
io.sockets.on('connection', function (socket) {
socket.emit('title', command);
socket.emit('screen', screen); // initial paint from cache
});