forked from CapacitorSet/box-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.js
86 lines (72 loc) · 2.04 KB
/
run.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
var cp = require('child_process'),
fs = require("fs"),
walk = require("walk"),
async = require("async");
var argv = require('minimist')(process.argv.slice(2));
let timeout = argv.timeout || 10;
if (!argv.timeout)
console.log("Using a 10 seconds timeout, pass --timeout to specify another timeout in seconds")
var options = process.argv
.slice(2)
.filter(path => {try{fs.statSync(path);return false}catch(e){return true}});
var tasks = process.argv
.slice(2)
.filter(path => {try{fs.statSync(path);return true}catch(e){return false}})
.map(path => fs.statSync(path).isDirectory() ?
cb => {
let files = [];
walk.walkSync(path, {
listeners: {
file: (root, stat, next) => {files.push({root, name:stat.name}); next();}
}
});
return files.map(
({root, name}) => analyze(root + name, name)
);
} :
() => analyze(path, path)
);
if (tasks.length == 0) {
console.log("Please pass one or more filenames or directories as an argument.");
process.exit(-1);
}
process.setMaxListeners(Infinity);
tasks.forEach(task => task());
function isDir(path) {
let exists = false;
try {
exists = fs.statSync(path).isDirectory();
} catch(e) {}
return exists;
}
function analyze(path, filename) {
let directory = filename + ".results";
let i = 1;
while (isDir(directory)) {
i++;
directory = filename + "." + i + ".results";
}
fs.mkdirSync(directory);
directory += "/"; // For ease of use
let worker = cp.fork('./analyze', [path, directory, ...options]);
let killTimeout;
worker.on('message', function(data) {
clearTimeout(killTimeout);
worker.kill();
});
worker.on('exit', function (code, signal) {
clearTimeout(killTimeout);
worker.kill();
});
worker.on('error', function (err) {
clearTimeout(killTimeout);
worker.kill();
});
killTimeout = setTimeout(function killOnTimeOut() {
console.log(`Analysis for ${filename} timed out.`);
worker.kill();
}, timeout * 1000);
process.on('exit', () => worker.kill());
process.on('SIGINT', () => worker.kill());
// process.on('uncaughtException', () => worker.kill());
}