Skip to content

Commit 9c6d606

Browse files
authored
Add cmake-file-api package, implementing a TypeScript wrapper of the CMake file-based API (#257)
* Update shared TS configs * Add new package scaffold * Add copilot instructions * Add package specific instructions * Add a test task * Fix readIndex * Add codemodel * Add target object kind * Add cache object kind * Add cmakeFiles object kind * Add toolchains object kind * Add configureLog object kind * Add separate query functions * Add reply error index * Add an index * Update version mechanism * Make ClientStatefulQueryReply more DRY
1 parent 58d17e6 commit 9c6d606

27 files changed

+4563
-16
lines changed

.github/copilot-instructions.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copilot Instructions for React Native Node-API
2+
3+
This is a **monorepo** that brings Node-API support to React Native, enabling native addons written in C/C++/Rust to run on React Native across iOS and Android.
4+
5+
## Package-Specific Instructions
6+
7+
**IMPORTANT**: Before working on any package, always check for and read package-specific `copilot-instructions.md` files in the package directory. These contain critical preferences and patterns for that specific package.
8+
9+
## Architecture Overview
10+
11+
**Core Flow**: JS `require("./addon.node")` → Babel transform → `requireNodeAddon()` TurboModule call → native library loading → Node-API module initialization
12+
13+
### Package Architecture
14+
15+
See the [README.md](../README.md#packages) for detailed descriptions of each package and their roles in the system. Key packages include:
16+
17+
- `packages/host` - Core Node-API runtime and Babel plugin
18+
- `packages/cmake-rn` - CMake wrapper for native builds
19+
- `packages/cmake-file-api` - TypeScript wrapper for CMake File API with Zod validation
20+
- `packages/ferric` - Rust/Cargo wrapper with napi-rs integration
21+
- `packages/gyp-to-cmake` - Legacy binding.gyp compatibility
22+
- `apps/test-app` - Integration testing harness
23+
24+
## Critical Build Dependencies
25+
26+
- **Custom Hermes**: Currently depends on a patched Hermes with Node-API support (see [facebook/hermes#1377](https://github.com/facebook/hermes/pull/1377))
27+
- **Prebuilt Binary Spec**: All tools must output to the exact naming scheme:
28+
- Android: `*.android.node/` with jniLibs structure + `react-native-node-api-module` marker file
29+
- iOS: `*.apple.node` (XCFramework renamed) + marker file
30+
31+
## Essential Workflows
32+
33+
### Development Setup
34+
35+
```bash
36+
npm ci && npm run build # Install deps and build all packages
37+
npm run bootstrap # Build native components (weak-node-api, examples)
38+
```
39+
40+
### Package Development
41+
42+
- **TypeScript project references**: Use `tsc --build` for incremental compilation
43+
- **Workspace scripts**: Most build/test commands use npm workspaces (`--workspace` flag)
44+
- **Focus on Node.js packages**: AI development primarily targets the Node.js tooling packages rather than native mobile code
45+
- **No TypeScript type asserts**: You have to ask explicitly and justify if you want to add `as` type assertions.
46+
47+
## Key Patterns
48+
49+
### Babel Transformation
50+
51+
The core magic happens in `packages/host/src/node/babel-plugin/plugin.ts`:
52+
53+
```js
54+
// Input: require("./addon.node")
55+
// Output: require("react-native-node-api").requireNodeAddon("pkg-name--addon")
56+
```
57+
58+
### CMake Integration
59+
60+
For linking against Node-API in CMakeLists.txt:
61+
62+
```cmake
63+
include(${WEAK_NODE_API_CONFIG})
64+
target_link_libraries(addon PRIVATE weak-node-api)
65+
```
66+
67+
### Cross-Platform Naming
68+
69+
Library names use double-dash separation: `package-name--path-component--addon-name`
70+
71+
### Testing
72+
73+
- **Individual packages**: Some packages have VS Code test tasks and others have their own `npm test` scripts for focused iteration (e.g., `npm test --workspace cmake-rn`). Use the latter only if the former is missing.
74+
- **Cross-package**: Use root-level `npm test` for cross-package testing once individual package tests pass
75+
- **Mobile integration**: Available but not the primary AI development focus - ask the developer to run those tests as needed
76+
77+
**Documentation**: Integration details, platform setup, and toolchain configuration are covered in existing repo documentation files.

.vscode/tasks.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Test cmake-file-api",
6+
"command": "node",
7+
"args": ["--run", "test"],
8+
"options": {
9+
"cwd": "${workspaceFolder}/packages/cmake-file-api"
10+
},
11+
"group": "test"
12+
}
13+
]
14+
}

configs/tsconfig.node-tests.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": "./tsconfig.node.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"emitDeclarationOnly": true,
6+
"declarationMap": false
7+
},
8+
"include": ["${configDir}/src/**/*.test.ts"],
9+
"exclude": []
10+
/*
11+
"references": [
12+
{
13+
"path": "./tsconfig.json"
14+
}
15+
]
16+
*/
17+
}

configs/tsconfig.cli.json renamed to configs/tsconfig.node.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
"rootDir": "${configDir}/src",
88
"types": ["node"]
99
},
10-
"include": ["${configDir}/src/*.ts"],
11-
"exclude": ["${configDir}/**.test.ts"]
10+
"include": ["${configDir}/src/"],
11+
"exclude": ["${configDir}/**/*.test.ts"]
1212
}

package-lock.json

Lines changed: 30 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
"type": "module",
55
"private": true,
66
"workspaces": [
7-
"apps/test-app",
87
"packages/cli-utils",
9-
"packages/gyp-to-cmake",
8+
"packages/cmake-file-api",
109
"packages/cmake-rn",
1110
"packages/ferric",
11+
"packages/gyp-to-cmake",
1212
"packages/host",
1313
"packages/node-addon-examples",
1414
"packages/node-tests",
15-
"packages/ferric-example"
15+
"packages/ferric-example",
16+
"apps/test-app"
1617
],
1718
"homepage": "https://github.com/callstackincubator/react-native-node-api#readme",
1819
"scripts": {

packages/cli-utils/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": "../../configs/tsconfig.cli.json"
2+
"extends": "../../configs/tsconfig.node.json"
33
}

packages/cmake-file-api/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# CMake File API (unofficial)
2+
3+
The CMake File API provides an interface for querying CMake's configuration and project information.
4+
5+
The API is based on files, where queries are written by client tools and read by CMake and replies are then written by CMake and read by client tools. The API is versioned, and the current version is v1 and these files are located in a directory named `.cmake/api/v1` in the build directory.
6+
7+
This package provides a TypeScript interface to create query files and read replies and is intended to serve the same purpose to the TypeScript community that the [`cmake-file-api` crate](https://crates.io/crates/cmake-file-api), serves to the Rust community.

0 commit comments

Comments
 (0)