-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgenerate-entrypoint.mts
More file actions
62 lines (53 loc) · 1.59 KB
/
generate-entrypoint.mts
File metadata and controls
62 lines (53 loc) · 1.59 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
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
const packageRoot = path.join(import.meta.dirname, "..");
const entrypointPath = path.join(packageRoot, "tests.generated.js");
const testPaths = fs.globSync("**/*.bundle.js", {
cwd: path.join(packageRoot, "tests"),
});
interface TestSuite {
[key: string]: string | TestSuite;
}
const suites: TestSuite = {};
for (const testPath of testPaths) {
const paths = testPath.split(path.sep);
const testName = paths.pop();
assert(typeof testName === "string");
let parent: TestSuite = suites;
for (const part of paths) {
if (!parent[part]) {
// Init if missing
parent[part] = {};
}
assert(typeof parent[part] === "object");
parent = parent[part];
}
parent[path.basename(testName, ".bundle.js")] = path.join("tests", testPath);
}
function suiteToString(suite: TestSuite, indent = 1): string {
const padding = " ".repeat(indent);
return Object.entries(suite)
.map(([key, value]) => {
if (typeof value === "string") {
return `${padding}"${key}": require("./${value}")`;
} else {
return `${padding}"${key}": {\n${suiteToString(
value,
indent + 1,
)}\n${padding}}`;
}
})
.join(", ");
}
const comment = "Generated by ./scripts/generate-entrypoint.mts";
console.log(
`Writing entrypoint to ${path.relative(
import.meta.dirname,
entrypointPath,
)} for ${testPaths.length} tests ...`,
);
fs.writeFileSync(
entrypointPath,
`/* ${comment} */\nmodule.exports.suites = {\n${suiteToString(suites)}\n};`,
);