Skip to content

Commit 43c2b8f

Browse files
kraenhansenclaude
andcommitted
docs: fill in HOW-IT-WORKS.md and CLI.md placeholders (#429)
- HOW-IT-WORKS.md: replace the three TODO comments near the top with a real, runnable example of calculator-lib's native C addon (mirrors docs/USAGE.md) plus the JS that requires and calls it, and clone instructions for readers who want to follow along with the source referenced later in the document. - CLI.md: hand-write documentation for all five react-native-node-api CLI commands (vendor-hermes, link, list, info, patch-xcode-project), their options and the shared library-naming strategies, sourced from packages/host/src/node/cli/program.ts, hermes.ts and options.ts, with a note to keep it in sync with those definitions. Fixes #425 Claude-Session: https://claude.ai/code/session_01DaK9eAAF5G8wj6UT8VekAm Co-authored-by: Claude <noreply@anthropic.com>
1 parent 7531199 commit 43c2b8f

2 files changed

Lines changed: 126 additions & 4 deletions

File tree

docs/CLI.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,67 @@
11
# The `react-native-node-api` command-line interface (CLI)
22

3-
<!-- TODO: Write detailed documentation of each command and their parameters -->
3+
The `react-native-node-api` package installs a `react-native-node-api` binary, which app and library authors use to vendor Hermes, link Node-API modules into an app and inspect how the library-naming scheme resolves for a given module.
4+
5+
```bash
6+
npx react-native-node-api <command> [options]
7+
```
8+
9+
Run `npx react-native-node-api help` or `npx react-native-node-api help <command>` to see this same information from the CLI itself.
10+
11+
> [!NOTE]
12+
> This document is hand-written from the [Commander](https://github.com/tj/commander.js) program definition in [`packages/host/src/node/cli/program.ts`](../packages/host/src/node/cli/program.ts) (with the `vendor-hermes` command defined in [`hermes.ts`](../packages/host/src/node/cli/hermes.ts)). It needs to be kept in sync by hand whenever a command or its options change.
13+
14+
## `vendor-hermes [from]`
15+
16+
Clones the pinned commit of Hermes' `static_h` branch (which carries Hermes' first-party Node-API implementation) into the `sdks/node-api-hermes` directory of the app's `react-native` package, so the native build can compile against it. Prints the path to the vendored checkout on success.
17+
18+
- `[from]` — Path to a file inside the app package. Defaults to the current working directory.
19+
- `--react-native-package <package-name>` — The React Native package to vendor Hermes into. Defaults to `react-native`.
20+
- `--silent` — Don't print anything except the final path. Defaults to `false`.
21+
- `--force` — Don't check timestamps of input files to skip unnecessary rebuilds; removes and re-clones an existing checkout. Defaults to `false`.
22+
23+
## `link [path]`
24+
25+
Auto-links the Node-API modules found among the app's dependencies for one or more platforms, copying (and, on Apple, signing) them into place.
26+
27+
- `[path]` — Some path inside the app package. Defaults to the current working directory.
28+
- `--android` — Link Android modules.
29+
- `--apple` — Link Apple modules.
30+
- `--prune` — Delete previously vendored modules that are no longer auto-linked. Defaults to `true`.
31+
- `--package-name <strategy>` — Controls how a dependency's package name is transformed into a library name. One of `strip`, `keep` or `omit` (see [Library naming](#library-naming) below). Defaults to `strip`, or the `NODE_API_PACKAGE_NAME` environment variable if set.
32+
- `--path-suffix <strategy>` — Controls how the path of the addon inside a package is transformed into a library name. One of `strip`, `keep` or `omit` (see [Library naming](#library-naming) below). Defaults to `strip`, or the `NODE_API_PATH_SUFFIX` environment variable if set.
33+
34+
At least one of `--android` / `--apple` must be passed, or the command exits with an error listing the supported platforms.
35+
36+
## `list [from-path]`
37+
38+
Lists the Node-API modules found among the dependencies of the package at (or above) a path, without linking them.
39+
40+
- `[from-path]` — Some path inside the app package. Defaults to the current working directory.
41+
- `--json` — Output the result as JSON instead of a human-readable summary. Defaults to `false`.
42+
- `--package-name <strategy>` — Same as for `link` (see [Library naming](#library-naming)).
43+
- `--path-suffix <strategy>` — Same as for `link` (see [Library naming](#library-naming)).
44+
45+
## `info <path>`
46+
47+
Utility to print the resolved module path, package name and computed library name for a single Node-API module, given its path. Useful for debugging naming collisions.
48+
49+
- `<path>` — Path to a Node-API module (e.g. an `*.android.node` directory or `*.apple.node` framework).
50+
- `--package-name <strategy>` — Same as for `link` (see [Library naming](#library-naming)).
51+
- `--path-suffix <strategy>` — Same as for `link` (see [Library naming](#library-naming)).
52+
53+
## `patch-xcode-project [path]`
54+
55+
Patches the app's Xcode project to add a build phase which copies, renames and signs the Node-API frameworks (equivalent to running `link --apple` as part of the Xcode build). Only supported on macOS.
56+
57+
- `[path]` — Some path inside the app package. Defaults to the current working directory.
58+
59+
## Library naming
60+
61+
`--package-name` and `--path-suffix` both control how the [cross-platform library name](./PREBUILDS.md) (`package-name--path-component--addon-name`) is derived, and accept the same three strategies. Given a package `@my-org/my-pkg` with an addon at `build/Release/my-addon.node`:
62+
63+
| Strategy | `--package-name` effect | `--path-suffix` effect |
64+
| -------- | ----------------------------------------- | --------------------------------------------------- |
65+
| `strip` | Scope is dropped: `my-pkg--my-addon` | Path is reduced to its basename: `my-pkg--my-addon` |
66+
| `keep` | Scope is kept: `my-org--my-pkg--my-addon` | Full path is kept: `my-pkg--build-Release-my-addon` |
67+
| `omit` | Package name is dropped: `my-addon` | Path is dropped: `my-pkg` |

docs/HOW-IT-WORKS.md

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,67 @@
22

33
This document will outline what happens throughout the various parts of the system, when the app calls the `add` method on the library introduced in the ["usage" document](./USAGE.md).
44

5-
<!-- TODO: Add Clone this repo: ... -->
6-
<!-- TODO: Add C++ code snippet -->
7-
<!-- TODO: Add JS code snippet on requiring and calling it -->
5+
If you want to follow along with the source code referenced throughout this document (such as `packages/host/cpp/HermesNapiHost.cpp`), clone this repo:
6+
7+
```bash
8+
git clone https://github.com/callstackincubator/react-native-node-api.git
9+
```
10+
11+
`calculator-lib`'s native code is a small Node-API addon written in C (see the ["usage" document](./USAGE.md#implement-native-code) for the full walkthrough of writing and building it):
12+
13+
```cpp
14+
// addon.c
15+
16+
#include <assert.h>
17+
#include <node_api.h>
18+
19+
static napi_value Add(napi_env env, napi_callback_info info) {
20+
napi_status status;
21+
22+
size_t argc = 2;
23+
napi_value args[2];
24+
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
25+
assert(status == napi_ok);
26+
27+
double value0, value1;
28+
status = napi_get_value_double(env, args[0], &value0);
29+
assert(status == napi_ok);
30+
status = napi_get_value_double(env, args[1], &value1);
31+
assert(status == napi_ok);
32+
33+
napi_value sum;
34+
status = napi_create_double(env, value0 + value1, &sum);
35+
assert(status == napi_ok);
36+
37+
return sum;
38+
}
39+
40+
#define DECLARE_NAPI_METHOD(name, func) \
41+
{ name, 0, func, 0, 0, 0, napi_default, 0 }
42+
43+
NAPI_MODULE_INIT(/* napi_env env, napi_value exports */) {
44+
napi_status status;
45+
46+
napi_property_descriptor addDescriptor = DECLARE_NAPI_METHOD("add", Add);
47+
status = napi_define_properties(env, exports, 1, &addDescriptor);
48+
assert(status == napi_ok);
49+
50+
return exports;
51+
}
52+
```
53+
54+
`calculator-lib`'s JavaScript entrypoint requires the prebuilt binary produced from that C code:
55+
56+
```javascript
57+
module.exports = require("./prebuild.node");
58+
```
59+
60+
And `my-app` imports and calls it:
61+
62+
```javascript
63+
import { add } from "calculator-lib";
64+
console.log("1 + 2 =", add(1, 2));
65+
```
866

967
## `my-app` makes an `import`
1068

0 commit comments

Comments
 (0)