Skip to content

Commit dd66e6a

Browse files
committed
Generate entrypoint
1 parent 0b5d821 commit dd66e6a

File tree

9 files changed

+90
-24
lines changed

9 files changed

+90
-24
lines changed

eslint.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export default tseslint.config(
1111
globalIgnores(["apps/test-app/ios/**"]),
1212
globalIgnores(["packages/host/hermes/**"]),
1313
globalIgnores(["packages/node-addon-examples/examples/**"]),
14+
globalIgnores(["packages/node-tests/node/**"]),
15+
globalIgnores(["packages/node-tests/tests/**"]),
1416
globalIgnores(["packages/ferric-example/ferric_example.d.ts"]),
1517
eslint.configs.recommended,
1618
tseslint.configs.recommended,
@@ -19,7 +21,8 @@ export default tseslint.config(
1921
"apps/test-app/*.js",
2022
"packages/node-addon-examples/*.js",
2123
"packages/host/babel-plugin.js",
22-
"packages/host/react-native.config.js"
24+
"packages/host/react-native.config.js",
25+
"packages/node-tests/tests.generated.js"
2326
],
2427
languageOptions: {
2528
parserOptions: {

packages/node-tests/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node/
22
tests/
3+
4+
tests.generated.js

packages/node-tests/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@react-native-node-api/node-tests",
33
"description": "Harness for running the Node.js tests from https://github.com/nodejs/node/tree/main/test",
44
"type": "commonjs",
5-
"main": "dist/index.js",
5+
"main": "tests.generated.js",
66
"private": true,
77
"homepage": "https://github.com/callstackincubator/react-native-node-api",
88
"repository": {
@@ -15,7 +15,8 @@
1515
"gyp-to-cmake": "gyp-to-cmake ./tests",
1616
"build-tests": "tsx scripts/build-tests.mts",
1717
"bundle": "rolldown -c rolldown.config.mts",
18-
"bootstrap": "node --run copy-tests && node --run gyp-to-cmake && node --run build-tests && node --run bundle"
18+
"generate-entrypoint": "tsx scripts/generate-entrypoint.mts",
19+
"bootstrap": "node --run copy-tests && node --run gyp-to-cmake && node --run build-tests && node --run bundle && node --run generate-entrypoint"
1920
},
2021
"devDependencies": {
2122
"bufout": "^0.3.2",
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
);

packages/node-tests/src/index.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Despite the name, this file isn't generated.
2+
3+
export interface TestSuite {
4+
[key: string]: TestSuite | (() => void);
5+
}
6+
7+
export declare const suites: TestSuite;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "@tsconfig/node22/tsconfig.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"declarationMap": true,
6+
"emitDeclarationOnly": true,
7+
"outDir": "dist",
8+
"types": []
9+
},
10+
"include": ["common.ts"]
11+
}

packages/node-tests/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"files": [],
33
"references": [
4-
{ "path": "./tsconfig.tests.json" },
4+
{ "path": "./tsconfig.common.json" },
55
{ "path": "./tsconfig.node-scripts.json" }
66
]
77
}

packages/node-tests/tsconfig.tests.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)