Skip to content

Commit a2e695d

Browse files
authored
Use package name and relative paths when hashing (#11)
* Use package name and relative path when hashing * Add utility command to print the hash of an xcframework * Add tests for hashNodeApiModulePath * Delete package.json files to avoid collisions * Update comment * Rename hashNodeApiModulePath to hashModulePath and fix its implementation * Rename getNodeApiRequireCallArgument to getLibraryInstallName * Add print-xcframeworks command * Exclude paths in findXCFrameworkPaths * Improve helpers * fixup! Rename getNodeApiRequireCallArgument to getLibraryInstallName * Asserting file existence in hashModulePath * fixup! Add print-xcframeworks command * Add missing .length * Use reassignment over recursion
1 parent 4c325de commit a2e695d

File tree

10 files changed

+572
-103
lines changed

10 files changed

+572
-103
lines changed

package-lock.json

Lines changed: 182 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/node-addon-examples/scripts/copy-examples.mts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,15 @@ for (const src of ALLOW_LIST) {
6666
const destPath = path.join(EXAMPLES_DIR, src);
6767
console.log("Copying from", srcPath, "to", destPath);
6868
fs.cpSync(srcPath, destPath, { recursive: true });
69+
// Delete all package.json files recursively, as they have duplicate names, causing collisions when hashing
70+
for (const entry of fs.readdirSync(destPath, {
71+
withFileTypes: true,
72+
recursive: true,
73+
})) {
74+
if (entry.name === "package.json") {
75+
const filePath = path.join(entry.parentPath, entry.name);
76+
console.log("Deleting", filePath);
77+
fs.rmSync(filePath);
78+
}
79+
}
6980
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
"bufout": "^0.3.1",
4949
"chalk": "^5.4.1",
5050
"commander": "^13.1.0",
51-
"ora": "^8.2.0"
51+
"ora": "^8.2.0",
52+
"pkg-dir": "^8.0.0",
53+
"read-pkg": "^9.0.1"
5254
},
5355
"devDependencies": {
5456
"@babel/core": "^7.26.10",

packages/react-native-node-api-modules/src/node/babel-plugin/plugin.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { transformFileSync } from "@babel/core";
66

77
import { plugin } from "./plugin.js";
88
import { setupTempDirectory } from "../test-utils.js";
9-
import { getNodeApiRequireCallArgument } from "../path-utils.js";
9+
import { getLibraryInstallName } from "../path-utils.js";
1010

1111
describe("plugin", () => {
1212
it("transforms require calls, regardless", (context) => {
@@ -38,10 +38,10 @@ describe("plugin", () => {
3838
`,
3939
});
4040

41-
const ADDON_1_REQUIRE_ARG = getNodeApiRequireCallArgument(
41+
const ADDON_1_REQUIRE_ARG = getLibraryInstallName(
4242
path.join(tempDirectoryPath, "addon-1")
4343
);
44-
const ADDON_2_REQUIRE_ARG = getNodeApiRequireCallArgument(
44+
const ADDON_2_REQUIRE_ARG = getLibraryInstallName(
4545
path.join(tempDirectoryPath, "addon-2")
4646
);
4747

packages/react-native-node-api-modules/src/node/babel-plugin/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import type { PluginObj, NodePath } from "@babel/core";
44
import * as t from "@babel/types";
55

66
import {
7-
getNodeApiRequireCallArgument,
7+
getLibraryInstallName,
88
isNodeApiModule,
99
replaceWithNodeExtension,
1010
} from "../path-utils";
1111

1212
export function replaceWithRequireNodeAddon(p: NodePath, modulePath: string) {
13-
const requireCallArgument = getNodeApiRequireCallArgument(
13+
const requireCallArgument = getLibraryInstallName(
1414
replaceWithNodeExtension(modulePath)
1515
);
1616
p.replaceWith(

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ describe("findPackageDependencyPaths", () => {
1010
const tempDir = setupTempDirectory(context, {
1111
"node_modules/lib-a/package.json": JSON.stringify({
1212
name: "lib-a",
13+
main: "index.js",
1314
}),
15+
"node_modules/lib-a/index.js": "",
1416
"test-package/node_modules/lib-b/package.json": JSON.stringify({
1517
name: "lib-b",
18+
main: "index.js",
1619
}),
20+
"test-package/node_modules/lib-b/index.js": "",
1721
"test-package/package.json": JSON.stringify({
1822
name: "test-package",
1923
dependencies: {
@@ -43,10 +47,50 @@ describe("findXCFrameworkPaths", () => {
4347
"sub-directory/lib-b.xcframework/react-native-node-api-module": "",
4448
});
4549
const result = findXCFrameworkPaths(tempDir);
46-
assert.deepEqual(result, [
50+
assert.deepEqual(result.sort(), [
4751
path.join(tempDir, "root.xcframework"),
4852
path.join(tempDir, "sub-directory/lib-a.xcframework"),
4953
path.join(tempDir, "sub-directory/lib-b.xcframework"),
5054
]);
5155
});
56+
57+
it("respects default exclude patterns", (context) => {
58+
const tempDir = setupTempDirectory(context, {
59+
"root.xcframework/react-native-node-api-module": "",
60+
"child-dir/dependency/lib.xcframework/react-native-node-api-module": "",
61+
"child-dir/node_modules/dependency/lib.xcframework/react-native-node-api-module":
62+
"",
63+
});
64+
const result = findXCFrameworkPaths(tempDir);
65+
assert.deepEqual(result.sort(), [
66+
path.join(tempDir, "child-dir/dependency/lib.xcframework"),
67+
path.join(tempDir, "root.xcframework"),
68+
]);
69+
});
70+
71+
it("respects explicit exclude patterns", (context) => {
72+
const tempDir = setupTempDirectory(context, {
73+
"root.xcframework/react-native-node-api-module": "",
74+
"child-dir/dependency/lib.xcframework/react-native-node-api-module": "",
75+
"child-dir/node_modules/dependency/lib.xcframework/react-native-node-api-module":
76+
"",
77+
});
78+
const result = findXCFrameworkPaths(tempDir, { excludePatterns: [/root/] });
79+
assert.deepEqual(result.sort(), [
80+
path.join(tempDir, "child-dir/dependency/lib.xcframework"),
81+
path.join(tempDir, "child-dir/node_modules/dependency/lib.xcframework"),
82+
]);
83+
});
84+
85+
it("disregards parts futher up in filesystem when excluding", (context) => {
86+
const tempDir = setupTempDirectory(context, {
87+
"node_modules/root.xcframework/react-native-node-api-module": "",
88+
"node_modules/child-dir/node_modules/dependency/lib.xcframework/react-native-node-api-module":
89+
"",
90+
});
91+
const result = findXCFrameworkPaths(path.join(tempDir, "node_modules"));
92+
assert.deepEqual(result, [
93+
path.join(tempDir, "node_modules/root.xcframework"),
94+
]);
95+
});
5296
});

0 commit comments

Comments
 (0)