-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathverify.js
More file actions
82 lines (74 loc) · 2.18 KB
/
verify.js
File metadata and controls
82 lines (74 loc) · 2.18 KB
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
// @ts-check
import "global-jsdom/register";
/** @typedef {{ [key: string]: Function | Elm }} Elm */
class KnownError extends Error {}
async function run() {
const [, , file, warnAboutFlags = "false", ...restArgs] = process.argv;
if (file === undefined) {
throw new KnownError(
`Expected the .js file to verify as the first argument.`,
);
}
if (restArgs.length > 0) {
throw new KnownError(
`Expected one or two arguments, but got ${restArgs.length} extra: ${JSON.stringify(restArgs)}`,
);
}
await import(file);
/** @type {Elm | undefined} */
const Elm = /** @type {any} */ (window).Elm;
if (
Elm === undefined ||
Elm === null ||
Array.isArray(Elm) ||
typeof Elm !== "object"
) {
throw new KnownError(`Bad window.Elm: ${Elm}`);
}
initAllElmApps(warnAboutFlags === "true", ["Elm"], Elm);
}
/**
* @param {boolean} warnAboutFlags
* @param {Array<string>} namespace
* @param {Elm} Elm
*/
function initAllElmApps(warnAboutFlags, namespace, Elm) {
for (const [key, value] of Object.entries(Elm)) {
const nextNamespace = [...namespace, key];
if (typeof value === "function") {
const node = document.createElement("x-elm-minification-benchmarks");
node.setAttribute("data-elm", "");
document.body.append(node);
try {
value({ node });
} catch (error) {
if (
error instanceof Error &&
error.message.includes(
"https://github.com/elm/core/blob/1.0.0/hints/2.md",
)
) {
if (warnAboutFlags) {
console.warn(
"The Elm program depends on automatic flag decoding, cannot test fully that the code runs after minification.",
);
}
continue;
} else {
throw error;
}
}
if (document.body.contains(node)) {
throw new KnownError(
`${nextNamespace.join(".")} did not render properly:\n${document.body.outerHTML}`,
);
}
} else {
initAllElmApps(warnAboutFlags, nextNamespace, value);
}
}
}
run().catch((error) => {
console.error(error instanceof KnownError ? error.message : error);
process.exitCode = 1;
});