Skip to content

Commit 53173c3

Browse files
Minor fixes from using package outside of a mono-repo (#93)
* Make the reactNativeDir resolve from the app package root * Clean hermes using async fs * Add babel entry-point fallback to "files" * Add React Native 0.79.2 to supported versions * Update packages/react-native-node-api-modules/src/node/cli/hermes.ts Co-authored-by: Matt Hargett <[email protected]> --------- Co-authored-by: Matt Hargett <[email protected]>
1 parent 7b07561 commit 53173c3

File tree

4 files changed

+52
-21
lines changed

4 files changed

+52
-21
lines changed

apps/test-app/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ msbuild.binlog
1313

1414
# Ignoring the Podfile.lock as the `react-native-node-api-modules` hash updates too frequently
1515
Podfile.lock
16+
17+
hermes/

packages/react-native-node-api-modules/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"!android/build",
3232
"ios",
3333
"include",
34+
"babel-plugin.js",
3435
"scripts/patch-hermes.rb",
3536
"weak-node-api/**",
3637
"!weak-node-api/build/",
@@ -89,7 +90,7 @@
8990
},
9091
"peerDependencies": {
9192
"@babel/core": "^7.26.10",
92-
"react-native": "0.79.1"
93+
"react-native": "0.79.1 || 0.79.2"
9394
},
9495
"codegenConfig": {
9596
"name": "NodeApiHostSpec",

packages/react-native-node-api-modules/scripts/patch-hermes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::UI.warn "!!! PATCHING HERMES WITH NODE-API SUPPORT !!!"
22

3-
VENDORED_HERMES_DIR ||= `npx react-native-node-api-modules vendor-hermes --silent`.strip
3+
VENDORED_HERMES_DIR ||= `npx react-native-node-api-modules vendor-hermes --silent '#{Pod::Config.instance.installation_root}'`.strip
44
if Dir.exist?(VENDORED_HERMES_DIR)
55
Pod::UI.info "Hermes vendored into #{VENDORED_HERMES_DIR.inspect}"
66
else

packages/react-native-node-api-modules/src/node/cli/hermes.ts

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,45 @@
1+
import assert from "node:assert/strict";
12
import fs from "node:fs";
23
import path from "node:path";
34

45
import { Command } from "@commander-js/extra-typings";
56
import { spawn, SpawnFailure } from "bufout";
67
import { oraPromise } from "ora";
7-
import { prettyPath } from "../path-utils";
8+
import { packageDirectorySync } from "pkg-dir";
89

9-
const HERMES_PATH = path.resolve(__dirname, "../../../hermes");
10+
import { getLatestMtime, prettyPath } from "../path-utils";
11+
12+
const HOST_PACKAGE_ROOT = path.resolve(__dirname, "../../..");
13+
// FIXME: make this configurable with reasonable fallback before public release
1014
const HERMES_GIT_URL = "https://github.com/kraenhansen/hermes.git";
1115
const HERMES_GIT_TAG = "node-api-for-react-native-0.79.0";
12-
const REACT_NATIVE_DIR = path.dirname(
13-
require.resolve("react-native/package.json")
14-
);
1516

1617
export const command = new Command("vendor-hermes")
18+
.argument("[from]", "Path to a file inside the app package", process.cwd())
1719
.option("--silent", "Don't print anything except the final path", false)
1820
.option(
1921
"--force",
2022
"Don't check timestamps of input files to skip unnecessary rebuilds",
2123
false
2224
)
23-
.action(async ({ force, silent }) => {
25+
.action(async (from, { force, silent }) => {
2426
try {
25-
if (force) {
26-
fs.rmSync(HERMES_PATH, { recursive: true, force: true });
27+
const appPackageRoot = packageDirectorySync({ cwd: from });
28+
assert(appPackageRoot, "Failed to find package root");
29+
const hermesPath = path.join(HOST_PACKAGE_ROOT, "hermes");
30+
if (force && fs.existsSync(hermesPath)) {
31+
await oraPromise(
32+
fs.promises.rm(hermesPath, { recursive: true, force: true }),
33+
{
34+
text: "Removing existing Hermes clone",
35+
successText: "Removed existing Hermes clone",
36+
failText: (error) =>
37+
`Failed to remove existing Hermes clone: ${error.message}`,
38+
isEnabled: !silent,
39+
}
40+
);
2741
}
28-
if (!fs.existsSync(HERMES_PATH)) {
42+
if (!fs.existsSync(hermesPath)) {
2943
await oraPromise(
3044
spawn(
3145
"git",
@@ -37,27 +51,41 @@ export const command = new Command("vendor-hermes")
3751
"--branch",
3852
HERMES_GIT_TAG,
3953
HERMES_GIT_URL,
40-
HERMES_PATH,
54+
hermesPath,
4155
],
4256
{
4357
outputMode: "buffered",
4458
}
4559
),
4660
{
47-
text: `Cloning custom Hermes into ${prettyPath(HERMES_PATH)}`,
61+
text: `Cloning custom Hermes into ${prettyPath(hermesPath)}`,
4862
successText: "Cloned custom Hermes",
4963
failText: (err) => `Failed to clone custom Hermes: ${err.message}`,
5064
isEnabled: !silent,
5165
}
5266
);
67+
}
68+
const hermesJsiPath = path.join(hermesPath, "API/jsi/jsi");
69+
const reactNativePath = path.dirname(
70+
require.resolve("react-native/package.json", {
71+
// Ensures we'll be patching the React Native package actually used by the app
72+
paths: [appPackageRoot],
73+
})
74+
);
75+
const reactNativeJsiPath = path.join(
76+
reactNativePath,
77+
"ReactCommon/jsi/jsi/"
78+
);
79+
80+
if (
81+
fs.existsSync(reactNativeJsiPath) &&
82+
(force ||
83+
getLatestMtime(hermesJsiPath) > getLatestMtime(reactNativeJsiPath))
84+
) {
5385
await oraPromise(
54-
fs.promises.cp(
55-
path.join(HERMES_PATH, "API/jsi/jsi"),
56-
path.join(REACT_NATIVE_DIR, "ReactCommon/jsi/jsi/"),
57-
{
58-
recursive: true,
59-
}
60-
),
86+
fs.promises.cp(hermesJsiPath, reactNativeJsiPath, {
87+
recursive: true,
88+
}),
6189
{
6290
text: `Copying JSI from Hermes to React Native`,
6391
successText: "Copied JSI from Hermes to React Native",
@@ -67,7 +95,7 @@ export const command = new Command("vendor-hermes")
6795
}
6896
);
6997
}
70-
console.log(HERMES_PATH);
98+
console.log(hermesPath);
7199
} catch (error) {
72100
if (error instanceof SpawnFailure) {
73101
error.flushOutput("both");

0 commit comments

Comments
 (0)