Skip to content

Commit e3788ca

Browse files
committed
Add addon-examples package
1 parent f36a3f8 commit e3788ca

File tree

11 files changed

+122
-4
lines changed

11 files changed

+122
-4
lines changed

apps/test-app/App.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
import React from "react";
2-
import { StyleSheet, Text, View } from "react-native";
2+
import { StyleSheet, Text, View, Button } from "react-native";
33

4-
import { requireNodeAddon } from "react-native-node-api-modules";
4+
// import { requireNodeAddon } from "react-native-node-api-modules";
5+
import nodeAddonExamples from "react-native-node-addon-examples";
56

67
function App(): React.JSX.Element {
78
return (
89
<View style={styles.container}>
910
<Text style={styles.title}>React Native Node-API Modules</Text>
11+
{Object.entries(nodeAddonExamples).map(([suiteName, examples]) => (
12+
<View key={suiteName} style={styles.suite}>
13+
<Text>{suiteName}</Text>
14+
{Object.entries(examples).map(([exampleName, requireExample]) => (
15+
<Button
16+
key={exampleName}
17+
title={exampleName}
18+
onPress={requireExample}
19+
/>
20+
))}
21+
</View>
22+
))}
1023
</View>
1124
);
1225
}
@@ -17,6 +30,12 @@ const styles = StyleSheet.create({
1730
justifyContent: "center",
1831
alignItems: "center",
1932
},
33+
suite: {
34+
borderWidth: 1,
35+
width: "96%",
36+
margin: 10,
37+
padding: 10,
38+
},
2039
title: {
2140
fontSize: 20,
2241
},

apps/test-app/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2031,4 +2031,4 @@ SPEC CHECKSUMS:
20312031

20322032
PODFILE CHECKSUM: d05cf05eb3b848d847fa52b59bd348ebd57d3cb5
20332033

2034-
COCOAPODS: 1.16.2
2034+
COCOAPODS: 1.15.2

apps/test-app/metro.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { makeMetroConfig } = require("@rnx-kit/metro-config");
2+
const { resolveRequest } = require("react-native-node-api-modules/metro-resolver");
23

34
module.exports = makeMetroConfig({
45
transformer: {
@@ -9,4 +10,7 @@ module.exports = makeMetroConfig({
910
},
1011
}),
1112
},
13+
resolver: {
14+
resolveRequest,
15+
}
1216
});

apps/test-app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"react": "19.0.0",
2424
"react-native": "0.79.1",
2525
"react-native-test-app": "^4.3.3",
26-
"react-native-node-api-modules": "*"
26+
"react-native-node-api-modules": "*",
27+
"react-native-node-addon-examples": "*"
2728
}
2829
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
examples/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
"1-getting-started": {
3+
// "1_hello_world": () => require("./examples/1-getting-started/1_hello_world/napi/hello.js")
4+
"2_function_arguments": () => require("./examples/1-getting-started/2_function_arguments/napi/addon.js")
5+
}
6+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "react-native-node-addon-examples",
3+
"private": true,
4+
"scripts": {
5+
"e2e": "rm -rf tests && npm run copy-examples && npm run gyp-to-cmake && npm run build",
6+
"copy-examples": "tsx scripts/copy-examples.mts",
7+
"gyp-to-cmake": "gyp-to-cmake ./examples",
8+
"build": "tsx scripts/build-examples.mts"
9+
},
10+
"devDependencies": {
11+
"node-addon-examples": "github:nodejs/node-addon-examples#4213d4c9d07996ae68629c67926251e117f8e52a",
12+
"gyp-to-cmake": "*",
13+
"react-native-node-api-cmake": "*"
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { execSync } from "node:child_process";
2+
3+
import { findCMakeProjects } from "./cmake-projects.mjs";
4+
5+
const projectDirectories = findCMakeProjects();
6+
7+
for (const projectDirectory of projectDirectories) {
8+
console.log(`Running "react-native-node-api-cmake" in ${projectDirectory}`);
9+
execSync("react-native-node-api-cmake --triplet arm64-apple-ios-sim", {
10+
cwd: projectDirectory,
11+
stdio: "inherit",
12+
});
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { readdirSync, statSync } from "node:fs";
2+
import { join } from "node:path";
3+
4+
export const EXAMPLES_DIR = new URL("../examples", import.meta.url).pathname;
5+
6+
export function findCMakeProjects(dir = EXAMPLES_DIR): string[] {
7+
let results: string[] = [];
8+
const files = readdirSync(dir);
9+
10+
for (const file of files) {
11+
const fullPath = join(dir, file);
12+
if (statSync(fullPath).isDirectory()) {
13+
results = results.concat(findCMakeProjects(fullPath));
14+
} else if (file === "CMakeLists.txt") {
15+
results.push(dir);
16+
}
17+
}
18+
19+
return results;
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { createRequire } from "node:module";
2+
import { cpSync, rmSync } from "node:fs";
3+
import path from "node:path";
4+
5+
import { EXAMPLES_DIR } from "./cmake-projects.mjs";
6+
7+
const ALLOW_LIST = [
8+
// "1-getting-started/1_hello_world/napi/",
9+
"1-getting-started/2_function_arguments/napi/",
10+
// "1-getting-started/3_callbacks/napi/",
11+
// "1-getting-started/4_object_factory/napi/"
12+
];
13+
14+
console.log("Copying files to", EXAMPLES_DIR);
15+
// Clean up the destination directory before copying
16+
rmSync(EXAMPLES_DIR, { recursive: true, force: true });
17+
18+
const require = createRequire(import.meta.url);
19+
20+
const EXAMPLES_PACKAGE_PATH = require.resolve(
21+
"node-addon-examples/package.json"
22+
);
23+
const SRC_DIR = path.join(path.dirname(EXAMPLES_PACKAGE_PATH), "src");
24+
console.log("Copying files from", SRC_DIR);
25+
26+
for (const src of ALLOW_LIST) {
27+
const srcPath = path.join(SRC_DIR, src);
28+
const destPath = path.join(EXAMPLES_DIR, src);
29+
console.log("Copying from", srcPath, "to", destPath);
30+
cpSync(srcPath, destPath, { recursive: true });
31+
}

0 commit comments

Comments
 (0)