-
Notifications
You must be signed in to change notification settings - Fork 1
/
instrument.js
87 lines (73 loc) · 2.18 KB
/
instrument.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 fs = require('fs'),
path = require('path'),
esmorph = require('esmorph'),
args = process.argv.splice(2),
total = 0,
failures = 0;
function instrument(name, source) {
var modifiers, morphed;
modifiers = [];
modifiers.push(esmorph.Tracer.FunctionEntrance('Enter'));
modifiers.push(esmorph.Tracer.FunctionExit('Exit'));
console.log('Writing', name, '...');
morphed = esmorph.modify(source, modifiers);
// console.log(morphed);
fs.writeFileSync("resources/"+name+"_converted", morphed);
++total;
}
function processFile(filename) {
var name, source, expected;
if (filename.match('_morphed.js') || filename.match('_changed.js')) {
return;
}
name = path.basename(filename);
source = fs.readFileSync(filename, 'utf-8');
//expected = fs.readFileSync(filename.replace('.js', '_morphed.js'), 'utf-8');
instrument(name, source);
}
// http://stackoverflow.com/q/5827612/
function walk(dir, done) {
var results = [];
fs.readdir(dir, function (err, list) {
if (err) {
return done(err);
}
var i = 0;
(function next() {
var file = list[i++];
if (!file) {
return done(null, results);
}
file = dir + '/' + file;
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function (err, res) {
results = results.concat(res);
next();
});
} else {
results.push(file);
next();
}
});
}());
});
}
function processDir(dirname) {
console.log("in process dir");
walk(path.resolve(__dirname, dirname), function (err, results) {
if (err) {
console.log('Error', err);
return;
}
results.forEach(processFile);
console.log();
console.log('Tests:', total, ' Failures:', failures);
process.exit(failures === 0 ? 0 : 1);
});
}
if (args.length === 0) {
processDir('resources');
} else {
args.forEach(processFile);
}