This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
headless.js
101 lines (94 loc) · 2.45 KB
/
headless.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
#!/usr/local/bin/node
/*jshint strict: true, node:true*/
"use strict";
const fs = require("fs");
const async = require("marcosc-async");
const builder = require("../tools/build-w3c-common");
const colors = require("colors");
const exec = require("child_process").exec;
const express = require("express");
const moment = require("moment");
colors.setTheme({
data: "grey",
debug: "cyan",
error: "red",
help: "cyan",
info: "green",
input: "grey",
prompt: "grey",
verbose: "cyan",
warn: "yellow",
});
function toExecutable(cmd) {
return {
get cmd() {
return cmd;
},
run() {
return new Promise((resolve, reject) => {
exec(cmd, (err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
}
};
}
const excludedFiles = new Set([
"embedder.html",
"starter.html",
"PresentationAPI.html",
"basic.built.html",
]);
const runRespec2html = async(function*(server) {
// Run respec2html.js on each example file (except whatever gets filtered)
// and stops in error if any of them reports a warning or an error
let sources = fs.readdirSync("examples")
.filter(filename => filename.match(/\.html$/))
.filter(filename => !excludedFiles.has(filename));
// Incrementally spawn processes and add them to process counter.
const executables = sources.map((source) => {
let cmd = "phantomjs --ssl-protocol=any ./tools/respec2html.js " +
`--delay 1 -w -e ${server}/examples/${source} /dev/null 10`;
return cmd;
}).map(
toExecutable
);
var testCount = 1;
var errored = false;
for (const exe of executables) {
try {
debug(` 🚄 Running test ${testCount++} of ${sources.length}.`);
yield exe.run();
} catch (err) {
console.error(colors.error(err));
errored = true;
}
}
if(errored){
throw new Error(" ❌ A test generated an error");
}
});
function debug(msg) {
console.log(colors.debug(`${colors.input(moment().format("LTS"))} ${msg}`));
}
async.task(function*() {
const port = process.env.PORT || 3000;
const server = "http://localhost:" + port;
debug(" ✅ Starting up Express...");
const app = express();
const dir = require("path").join(__dirname, "..");
app.use(express.static(dir));
app.listen(port);
debug(" ⏲ Running ReSpec2html tests...");
yield runRespec2html(server);
})
.then(
() => process.exit(0)
)
.catch((err) => {
console.error(err);
process.exit(1);
});