Repository navigation
Expand file tree
/
Copy pathrun.mjs
More file actions
98 lines (85 loc) · 3.85 KB
/
Copy pathrun.mjs
File metadata and controls
98 lines (85 loc) · 3.85 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* 06 — validate-gate
*
* A CI-style pre-send gate: run validate() over a template and block a
* send if it has any *error*-severity diagnostic (warnings are reported
* but don't block). validateOrFail() below is the reusable gate — copy it
* straight into a CI step or a pre-send hook.
*
* NODE PATH: this example uses the WASM binding directly (validate()) —
* validate operates on the source transformed by the bare component
* pipeline, not the full build, so it needs no filesystem access and has
* no CLI dependency at all, unlike most of this suite's other examples.
*
* RUNNER SEAM — read this before changing how this file is invoked:
* - `node run.mjs <path> [<path> ...]` is the REAL gate. It calls
* validateOrFail() on the given paths, which exits the process with 1
* if any of them has an error, or falls through to exit 0 otherwise.
* verify.mjs always invokes this file with explicit argv paths so it
* observes true exit codes (bad.inky alone -> exit 1, good.inky alone
* -> exit 0).
* - `node run.mjs` with NO args is the suite-runner path (this is what
* `npm run examples` calls for every example). This example's whole
* point is to demonstrate a FAILING gate, but run-all.mjs runs every
* example unconditionally, so the no-args branch demonstrates BOTH
* good.inky (passes) and bad.inky (fails) by hand — printing
* diagnostics and the exit code the gate WOULD have produced for each
* — without ever calling the real failure exit itself. It always
* exits 0. (run-all.mjs carries a matching exemption for this
* example's own exit code, belt-and-suspenders.)
*/
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { inkyExample } from "../../bootstrap.mjs";
import { validate } from "../../lib/wasm.mjs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const dist = inkyExample("06-validate-gate");
/**
* Validate one template and print its diagnostics grouped by severity.
* Returns true iff it has at least one error-severity diagnostic.
*/
function reportDiagnostics(filePath) {
const html = fs.readFileSync(filePath, "utf-8");
const diagnostics = validate(html);
const errors = diagnostics.filter((d) => d.severity === "error");
const warnings = diagnostics.filter((d) => d.severity === "warning");
console.log(`${path.basename(filePath)}:`);
for (const d of errors) console.log(` ERROR [${d.rule}] ${d.message}`);
for (const d of warnings) console.log(` WARNING [${d.rule}] ${d.message}`);
if (diagnostics.length === 0) console.log(" (clean)");
return errors.length > 0;
}
/**
* The reusable pre-send gate. Validate every path, print diagnostics for
* each, and exit(1) as soon as it's known at least one has an error.
*/
function validateOrFail(paths) {
let hasErrors = false;
for (const p of paths) {
if (reportDiagnostics(p)) hasErrors = true;
}
if (hasErrors) process.exit(1);
}
const argPaths = process.argv.slice(2);
if (argPaths.length > 0) {
// Real invocation: exactly what a CI step or pre-send hook would run.
validateOrFail(argPaths);
console.log("gate: passed");
fs.writeFileSync(
path.join(dist, "report.txt"),
`gate: passed for ${argPaths.map((p) => path.basename(p)).join(", ")}\n`,
);
process.exit(0);
}
// No-args demo path — see the header comment above.
const good = path.join(__dirname, "good.inky");
const bad = path.join(__dirname, "bad.inky");
const goodFailed = reportDiagnostics(good);
console.log(` -> gate would exit ${goodFailed ? "1 (blocked)" : "0 (passed)"}\n`);
const badFailed = reportDiagnostics(bad);
console.log(` -> gate would exit ${badFailed ? "1 (blocked)" : "0 (passed)"}`);
fs.writeFileSync(
path.join(dist, "report.txt"),
`good.inky would exit ${goodFailed ? 1 : 0}\nbad.inky would exit ${badFailed ? 1 : 0}\n`,
);