-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathspeclj.js
139 lines (120 loc) · 3.32 KB
/
speclj.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
Speclj + Headless Chrome Test Runner
Usage:
node bin/speclj.js [auto]
auto: will only run specs updated after the last run. (default: false)
Each run produced/touches a timestamp file, .specljs-timestamp
*/
const path = require('path');
const fs = require("fs");
var autoArg = process.argv.pop();
var timestampFile = path.resolve(__dirname, "../.specljs-timestamp");
var specsHTMLFile = path.resolve(__dirname, "specs.html");
var nsPrefix = "speclj"
function lastModified(filepath) {
try {
var stats = fs.statSync(filepath);
return stats.mtime;
} catch (error) {
return null;
}
};
function writeTimestamp() {
if (lastModified(timestampFile) != null) {
fs.unlinkSync(timestampFile);
}
fs.closeSync(fs.openSync(timestampFile, 'w'));
};
function readTimestamp() {
return lastModified(timestampFile);
};
function autoMode() {
return autoArg == "auto" && readTimestamp() != null;
};
function findUpdatedSpecs(rdeps, deps) {
var minMillis = readTimestamp().getTime();
var updated = {};
for(var ns in rdeps) {
var file = deps.idToPath_[ns];
var path = file.substring(7);
if (lastModified(path).getTime() >= minMillis) {
updated[ns] = true;
}
}
return updated;
};
function buildReverseDeps(deps) {
var rdeps = {};
for(var ns in deps.idToPath_) {
if (ns.startsWith(nsPrefix)) {
var file = deps.idToPath_[ns];
var requires = deps.dependencies_[file].requires
for (var i = 0; i < requires.length; i++) {
var rdep = requires[i];
if (rdep.startsWith(nsPrefix)) {
if (!(rdep in rdeps)) {
rdeps[rdep] = {}
}
rdeps[rdep][ns] = true;
}
}
if(!(ns in rdeps)) {
rdeps[ns] = {}
}
}
}
return rdeps;
};
function reduceToSpecs(affected) {
var result = {};
for (var ns in affected) {
if (ns.endsWith("_spec")) {
result[ns.replace(/_/g, "-")] = true
}
}
return result;
};
function findAffectedSpecs(deps) {
var rdeps = buildReverseDeps(deps);
var updated = findUpdatedSpecs(rdeps, deps);
var result = {};
var walkDeps = function (nses) {
for(var ns in nses) {
if (!(ns in result)) {
result[ns] = true;
walkDeps(rdeps[ns])
}
}
};
walkDeps(updated);
return reduceToSpecs(result);
};
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.exposeFunction('autoMode', () => autoMode());
await page.exposeFunction('findAffectedSpecs', (deps) => findAffectedSpecs(deps));
await page.exposeFunction('writeTimestamp', () => writeTimestamp());
await page.goto('file://' + specsHTMLFile);
page.on('console', msg => {
if (msg.text() === "Failed to load resource: net::ERR_FILE_NOT_FOUND") {
return; // because we aren't loading specs.html over http, many local resources (images, fonts) are not found.
}
console.log(msg.text());
});
var code = await page.evaluate(async () => {
try {
var specs = await window.autoMode() ? await window.findAffectedSpecs(goog.debugLoader_) : null;
var result = runSpecsFiltered(specs);
await window.writeTimestamp();
return result;
}
catch (e) {
console.error(e);
return 1;
}
});
await browser.close();
process.exit(code);
})();