|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +const packageRoot = path.join(import.meta.dirname, ".."); |
| 6 | +const entrypointPath = path.join(packageRoot, "tests.generated.js"); |
| 7 | + |
| 8 | +const testPaths = fs.globSync("**/*.bundle.js", { |
| 9 | + cwd: path.join(packageRoot, "tests"), |
| 10 | +}); |
| 11 | + |
| 12 | +interface TestSuite { |
| 13 | + [key: string]: string | TestSuite; |
| 14 | +} |
| 15 | + |
| 16 | +const suites: TestSuite = {}; |
| 17 | + |
| 18 | +for (const testPath of testPaths) { |
| 19 | + const paths = testPath.split(path.sep); |
| 20 | + const testName = paths.pop(); |
| 21 | + assert(typeof testName === "string"); |
| 22 | + let parent: TestSuite = suites; |
| 23 | + for (const part of paths) { |
| 24 | + if (!parent[part]) { |
| 25 | + // Init if missing |
| 26 | + parent[part] = {}; |
| 27 | + } |
| 28 | + assert(typeof parent[part] === "object"); |
| 29 | + parent = parent[part]; |
| 30 | + } |
| 31 | + parent[path.basename(testName, ".bundle.js")] = path.join("tests", testPath); |
| 32 | +} |
| 33 | + |
| 34 | +function suiteToString(suite: TestSuite, indent = 1): string { |
| 35 | + const padding = " ".repeat(indent); |
| 36 | + return Object.entries(suite) |
| 37 | + .map(([key, value]) => { |
| 38 | + if (typeof value === "string") { |
| 39 | + return `${padding}"${key}": () => require("./${value}")`; |
| 40 | + } else { |
| 41 | + return `${padding}"${key}": {\n${suiteToString( |
| 42 | + value, |
| 43 | + indent + 1 |
| 44 | + )}\n${padding}}`; |
| 45 | + } |
| 46 | + }) |
| 47 | + .join(", "); |
| 48 | +} |
| 49 | + |
| 50 | +const comment = "Generated by ./scripts/generate-entrypoint.mts"; |
| 51 | + |
| 52 | +console.log( |
| 53 | + `Writing entrypoint to ${path.relative( |
| 54 | + import.meta.dirname, |
| 55 | + entrypointPath |
| 56 | + )} for ${testPaths.length} tests ...` |
| 57 | +); |
| 58 | + |
| 59 | +fs.writeFileSync( |
| 60 | + entrypointPath, |
| 61 | + `/* ${comment} */\nmodule.exports.suites = {\n${suiteToString(suites)}\n};` |
| 62 | +); |
0 commit comments