Skip to content

Commit 001884f

Browse files
committed
add ReactNativeNodeAPIConfiguration and reading configuration from dependencies
1 parent 0969349 commit 001884f

File tree

1 file changed

+54
-22
lines changed

1 file changed

+54
-22
lines changed

packages/host/src/node/path-utils.ts

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
22
import path from "node:path";
33
import fs from "node:fs";
44
import { packageDirectorySync } from "pkg-dir";
5-
import { readPackageSync } from "read-pkg";
5+
import { NormalizedPackageJson, readPackageSync } from "read-pkg";
66
import { createRequire } from "node:module";
77

88
import { chalk, prettyPath } from "@react-native-node-api/cli-utils";
@@ -294,41 +294,73 @@ export function visualizeLibraryMap(libraryMap: LibraryMap) {
294294
return result.join("\n");
295295
}
296296

297+
export function findPackageConfigurationByPath(
298+
fromPath: string,
299+
): ReactNativeNodeAPIConfiguration {
300+
const packageRoot = packageDirectorySync({ cwd: fromPath });
301+
assert(packageRoot, `Could not find package root from ${fromPath}`);
302+
303+
const {
304+
reactNativeNodeApi,
305+
}: NormalizedPackageJson & ReactNativeNodeAPIConfiguration = readPackageSync({
306+
cwd: packageRoot,
307+
});
308+
309+
return { reactNativeNodeApi };
310+
}
311+
312+
export interface ReactNativeNodeAPIConfiguration {
313+
reactNativeNodeApi?: {
314+
scan?: {
315+
dependencies?: string[];
316+
};
317+
};
318+
}
319+
320+
type PackageJsonWithNodeApi = NormalizedPackageJson &
321+
ReactNativeNodeAPIConfiguration;
322+
297323
/**
298324
* Search upwards from a directory to find a package.json and
299-
* return a record mapping from each dependencies of that package to their path on disk.
325+
* return a record mapping from each dependency of that package to their path on disk.
326+
* Also checks all dependencies from reactNativeNodeApi field in dependencies package.json
300327
*/
301328
export function findPackageDependencyPaths(
302329
fromPath: string,
303330
): Record<string, string> {
304331
const packageRoot = packageDirectorySync({ cwd: fromPath });
305332
assert(packageRoot, `Could not find package root from ${fromPath}`);
306333

307-
const requireFromPackageRoot = createRequire(
308-
path.join(packageRoot, "noop.js"),
309-
);
334+
const requireFromRoot = createRequire(path.join(packageRoot, "noop.js"));
310335

311-
const { dependencies = {}, napiDependencies = [] } = readPackageSync({
336+
const { dependencies = {}, reactNativeNodeApi } = readPackageSync({
312337
cwd: packageRoot,
313-
});
338+
}) as PackageJsonWithNodeApi;
314339

315-
const safeNApiDependencies: string[] = Array.isArray(napiDependencies)
316-
? napiDependencies.map(String)
317-
: [];
340+
const initialDeps = Object.keys(dependencies).concat(
341+
reactNativeNodeApi?.scan?.dependencies ?? [],
342+
);
318343

319344
return Object.fromEntries(
320-
Object.keys(dependencies)
321-
.concat(safeNApiDependencies)
322-
.flatMap((dependencyName) => {
323-
const resolvedDependencyRoot = resolvePackageRoot(
324-
requireFromPackageRoot,
325-
dependencyName,
326-
);
327-
328-
return resolvedDependencyRoot
329-
? [[dependencyName, resolvedDependencyRoot]]
330-
: [];
331-
}),
345+
initialDeps.flatMap((name) => {
346+
const root = resolvePackageRoot(requireFromRoot, name);
347+
if (!root) return [];
348+
349+
const nested =
350+
findPackageConfigurationByPath(root)?.reactNativeNodeApi?.scan
351+
?.dependencies ?? [];
352+
353+
const nestedEntries = nested
354+
.map((nestedName) => {
355+
const nestedRoot = resolvePackageRoot(requireFromRoot, nestedName);
356+
return nestedRoot
357+
? ([nestedName, nestedRoot] as [string, string])
358+
: null;
359+
})
360+
.filter((entry): entry is [string, string] => entry !== null);
361+
362+
return [[name, root] as [string, string], ...nestedEntries];
363+
}),
332364
);
333365
}
334366

0 commit comments

Comments
 (0)