-
Notifications
You must be signed in to change notification settings - Fork 0
/
renode
executable file
·83 lines (77 loc) · 2.44 KB
/
renode
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
#! /usr/local/bin/node
var fs = require('fs');
var util = require('util');
var rutil = require('./util');
var spawn = require('child_process').spawn;
var growl = require('growl');
var path = require('path');
var processName = '';
function notify(message) {
console.log('[' + processName + '] ' + message);
growl.notify('[' + processName + '] ' + message);
}
function keepAlive(target) {
var proc = null;
var sources = [];
var kill = function(signal, source) {
if (proc) {
sources.push(source);
proc.kill(signal || 'SIGTERM');
proc = null;
}
else launch(source);
}
var launch = function(source) {
notify((source ? 're' : '') + 'spawning process' + (source ? ' because of change in ' + source : ''));
proc = spawn('node', [target]);
proc.stdout.on('data', function (data) {
console.log(data.toString('utf8'));
});
proc.stderr.on('data', function (data) {
console.error(data.toString('utf8'));
});
proc.on('exit', function (code, signal) {
proc = null;
if (signal == 'SIGHUP') process.nextTick(launch.bind(this, !!sources.length ? sources.shift() : null));
else if (code == 0) notify('process ended');
else notify('process crashed');
});
}
fs.watchFile(target, function (curr, prev) {
if (curr.mtime.getTime() == prev.mtime.getTime()) return;
kill('SIGHUP', target);
});
for (var i = 1; i < arguments.length; ++i) {
(function(source) {
fs.watchFile(source, function (curr, prev) {
if (curr.mtime.getTime() == prev.mtime.getTime()) return;
kill('SIGHUP', source);
});
})(arguments[i]);
}
launch();
}
function showHelp() {
console.log('usage: renode <js file> [[watch file 2] ... [watch file n]]')
}
if (process.argv.length < 3) {
showHelp();
return;
}
if (!rutil.exists(process.argv[2])) {
console.error(process.argv[2] + ' does not exist');
showHelp();
return;
}
processName = path.basename(process.argv[2]);
var args = [process.argv[2]];
var watchFiles = process.argv.slice(3);
watchFiles.forEach(function(watchFile) {
var expandedFiles = rutil.findFiles(watchFile);
expandedFiles.forEach(function(file) {
if (args.indexOf(file) == -1) {
args.push(file);
}
});
});
keepAlive.apply(this, args);