Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 55 additions & 14 deletions packages/host/src/node/path-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import path from "node:path";
import fs from "node:fs";
import { packageDirectorySync } from "pkg-dir";
import { readPackageSync } from "read-pkg";
import { NormalizedPackageJson, readPackageSync } from "read-pkg";
import { createRequire } from "node:module";

import { chalk, prettyPath } from "@react-native-node-api/cli-utils";
Expand Down Expand Up @@ -294,31 +294,72 @@ export function visualizeLibraryMap(libraryMap: LibraryMap) {
return result.join("\n");
}

export function findPackageConfigurationByPath(
fromPath: string,
): ReactNativeNodeAPIConfiguration {
const packageRoot = packageDirectorySync({ cwd: fromPath });
assert(packageRoot, `Could not find package root from ${fromPath}`);

const {
reactNativeNodeApi,
}: NormalizedPackageJson & ReactNativeNodeAPIConfiguration = readPackageSync({
cwd: packageRoot,
});

return { reactNativeNodeApi };
}

export interface ReactNativeNodeAPIConfiguration {
reactNativeNodeApi?: {
scan?: {
dependencies?: string[];
};
};
}

type PackageJsonWithNodeApi = NormalizedPackageJson &
ReactNativeNodeAPIConfiguration;

/**
* Search upwards from a directory to find a package.json and
* return a record mapping from each dependencies of that package to their path on disk.
* return a record mapping from each dependency of that package to their path on disk.
* Also checks all dependencies from reactNativeNodeApi field in dependencies package.json
*/
export function findPackageDependencyPaths(
fromPath: string,
): Record<string, string> {
const packageRoot = packageDirectorySync({ cwd: fromPath });
assert(packageRoot, `Could not find package root from ${fromPath}`);

const requireFromPackageRoot = createRequire(
path.join(packageRoot, "noop.js"),
);
const requireFromRoot = createRequire(path.join(packageRoot, "noop.js"));

const { dependencies = {}, reactNativeNodeApi } = readPackageSync({
cwd: packageRoot,
}) as PackageJsonWithNodeApi;

const { dependencies = {} } = readPackageSync({ cwd: packageRoot });
const initialDeps = Object.keys(dependencies).concat(
reactNativeNodeApi?.scan?.dependencies ?? [],
);

return Object.fromEntries(
Object.keys(dependencies).flatMap((dependencyName) => {
const resolvedDependencyRoot = resolvePackageRoot(
requireFromPackageRoot,
dependencyName,
);
return resolvedDependencyRoot
? [[dependencyName, resolvedDependencyRoot]]
: [];
initialDeps.flatMap((name) => {
const root = resolvePackageRoot(requireFromRoot, name);
if (!root) return [];

const nested =
findPackageConfigurationByPath(root)?.reactNativeNodeApi?.scan
?.dependencies ?? [];

const nestedEntries = nested
.map((nestedName) => {
const nestedRoot = resolvePackageRoot(requireFromRoot, nestedName);
return nestedRoot
? ([nestedName, nestedRoot] as [string, string])
: null;
})
.filter((entry): entry is [string, string] => entry !== null);

return [[name, root] as [string, string], ...nestedEntries];
}),
);
}
Expand Down
Loading