|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import path from "node:path"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import { createRequire } from "node:module"; |
| 5 | + |
| 6 | +import { Command } from "@commander-js/extra-typings"; |
| 7 | + |
| 8 | +const XCFRAMEWORK_NAME = "node-api.xcframework"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Search upwards from a directory to find a package.json and |
| 12 | + * return a list of the resolved paths of all dependencies of that package. |
| 13 | + */ |
| 14 | +export function findPackageDependencyPaths(from: string): string[] { |
| 15 | + const candidatePath = path.join(from, "package.json"); |
| 16 | + const parentDir = path.dirname(from); |
| 17 | + if (fs.existsSync(candidatePath)) { |
| 18 | + const require = createRequire(from); |
| 19 | + const contents = fs.readFileSync(candidatePath, "utf-8"); |
| 20 | + const json = JSON.parse(contents) as unknown; |
| 21 | + // Assert the package.json has the expected structure |
| 22 | + assert( |
| 23 | + typeof json === "object" && json !== null, |
| 24 | + "Expected package.json to be an object" |
| 25 | + ); |
| 26 | + if ( |
| 27 | + "dependencies" in json && |
| 28 | + typeof json.dependencies === "object" && |
| 29 | + json.dependencies !== null |
| 30 | + ) { |
| 31 | + return Object.keys(json.dependencies) |
| 32 | + .map((dependencyName) => { |
| 33 | + try { |
| 34 | + return path.dirname( |
| 35 | + require.resolve(`${dependencyName}/package.json`) |
| 36 | + ); |
| 37 | + } catch { |
| 38 | + return undefined; |
| 39 | + } |
| 40 | + }) |
| 41 | + .filter((p) => typeof p === "string"); |
| 42 | + } else { |
| 43 | + return []; |
| 44 | + } |
| 45 | + } else if (parentDir === from) { |
| 46 | + throw new Error("package.json not found in any parent directory"); |
| 47 | + } else { |
| 48 | + return findPackageDependencyPaths(parentDir); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export function findXCFrameworkPaths(dependencyPath: string): string[] { |
| 53 | + return fs |
| 54 | + .readdirSync(dependencyPath, { withFileTypes: true }) |
| 55 | + .flatMap((file) => { |
| 56 | + if (file.isDirectory()) { |
| 57 | + if (file.name === XCFRAMEWORK_NAME) { |
| 58 | + return [path.join(dependencyPath, file.name)]; |
| 59 | + } else { |
| 60 | + // Traverse into the child directory |
| 61 | + return findXCFrameworkPaths(path.join(dependencyPath, file.name)); |
| 62 | + } |
| 63 | + } else { |
| 64 | + return []; |
| 65 | + } |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +export const program = new Command("react-native-node-api-modules"); |
| 70 | + |
| 71 | +program |
| 72 | + .command("print-xcframework-paths") |
| 73 | + .argument("<installation-root>", "Parent directory of the Podfile", (p) => |
| 74 | + path.resolve(process.cwd(), p) |
| 75 | + ) |
| 76 | + .action((installationRoot: string) => { |
| 77 | + // Find the package.json of the app |
| 78 | + const dependencyPaths = findPackageDependencyPaths(installationRoot); |
| 79 | + const xcframeworkPaths = dependencyPaths.flatMap(findXCFrameworkPaths); |
| 80 | + // Find all node-api.xcframeworks files in the dependencies |
| 81 | + console.log(JSON.stringify(xcframeworkPaths, null, 2)); |
| 82 | + }); |
| 83 | + |
1 | 84 | export function run(argv: string[]) { |
2 | | - console.log("Hello from react-native-node-api-modules"); |
| 85 | + program.parse(argv); |
3 | 86 | } |
0 commit comments