Skip to content

Commit bd733b8

Browse files
authored
Derive tag on fork from .hermesversion in "react-native" (#158)
1 parent 5cbffb8 commit bd733b8

File tree

2 files changed

+64
-32
lines changed

2 files changed

+64
-32
lines changed

.changeset/upset-paws-follow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-native-node-api": minor
3+
---
4+
5+
Derive the tag used to clone the React Native fork bringing Node-API support from the .hermesversion file in the react-native package.

packages/host/src/node/cli/hermes.ts

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { prettyPath } from "../path-utils";
1212
const HOST_PACKAGE_ROOT = path.resolve(__dirname, "../../..");
1313
// FIXME: make this configurable with reasonable fallback before public release
1414
const HERMES_GIT_URL = "https://github.com/kraenhansen/hermes.git";
15-
const HERMES_GIT_TAG = "node-api-for-react-native-0.79.0";
1615

1716
export const command = new Command("vendor-hermes")
1817
.argument("[from]", "Path to a file inside the app package", process.cwd())
@@ -26,6 +25,27 @@ export const command = new Command("vendor-hermes")
2625
try {
2726
const appPackageRoot = packageDirectorySync({ cwd: from });
2827
assert(appPackageRoot, "Failed to find package root");
28+
const reactNativePath = path.dirname(
29+
require.resolve("react-native/package.json", {
30+
// Ensures we'll be patching the React Native package actually used by the app
31+
paths: [appPackageRoot],
32+
})
33+
);
34+
const hermesVersionPath = path.join(
35+
reactNativePath,
36+
"sdks",
37+
".hermesversion"
38+
);
39+
const hermesVersion = fs.readFileSync(hermesVersionPath, "utf8").trim();
40+
if (!silent) {
41+
console.log(`Using Hermes version: ${hermesVersion}`);
42+
}
43+
44+
const reactNativeJsiPath = path.join(
45+
reactNativePath,
46+
"ReactCommon/jsi/jsi/"
47+
);
48+
2949
const hermesPath = path.join(HOST_PACKAGE_ROOT, "hermes");
3050
if (force && fs.existsSync(hermesPath)) {
3151
await oraPromise(
@@ -40,42 +60,48 @@ export const command = new Command("vendor-hermes")
4060
);
4161
}
4262
if (!fs.existsSync(hermesPath)) {
43-
await oraPromise(
44-
spawn(
45-
"git",
46-
[
47-
"clone",
48-
"--recursive",
49-
"--depth",
50-
"1",
51-
"--branch",
52-
HERMES_GIT_TAG,
53-
HERMES_GIT_URL,
54-
hermesPath,
55-
],
63+
const patchedTag = `node-api-${hermesVersion}`;
64+
try {
65+
await oraPromise(
66+
spawn(
67+
"git",
68+
[
69+
"clone",
70+
"--recursive",
71+
"--depth",
72+
"1",
73+
"--branch",
74+
patchedTag,
75+
HERMES_GIT_URL,
76+
hermesPath,
77+
],
78+
{
79+
outputMode: "buffered",
80+
}
81+
),
5682
{
57-
outputMode: "buffered",
83+
text: `Cloning custom Hermes into ${prettyPath(hermesPath)}`,
84+
successText: "Cloned custom Hermes",
85+
failText: (err) =>
86+
`Failed to clone custom Hermes: ${err.message}`,
87+
isEnabled: !silent,
5888
}
59-
),
60-
{
61-
text: `Cloning custom Hermes into ${prettyPath(hermesPath)}`,
62-
successText: "Cloned custom Hermes",
63-
failText: (err) => `Failed to clone custom Hermes: ${err.message}`,
64-
isEnabled: !silent,
89+
);
90+
} catch (error) {
91+
if (error instanceof SpawnFailure) {
92+
error.flushOutput("both");
93+
console.error(
94+
`\n🛑 React Native uses the ${hermesVersion} tag and cloning our fork failed.`,
95+
`Please see the Node-API package's peer dependency on "react-native" for supported versions.`
96+
);
97+
process.exitCode = 1;
98+
return;
99+
} else {
100+
throw error;
65101
}
66-
);
102+
}
67103
}
68104
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-
);
79105

80106
assert(
81107
fs.existsSync(hermesJsiPath),
@@ -96,6 +122,7 @@ export const command = new Command("vendor-hermes")
96122
);
97123
console.log(hermesPath);
98124
} catch (error) {
125+
process.exitCode = 1;
99126
if (error instanceof SpawnFailure) {
100127
error.flushOutput("both");
101128
}

0 commit comments

Comments
 (0)