Skip to content

Commit 1c88903

Browse files
committed
use pkg-dir and read-pkg wrappers
1 parent c59e8f1 commit 1c88903

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

packages/react-native-node-api-modules/src/node/path-utils.ts

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "node:assert/strict";
22
import path from "node:path";
3-
import fs, { readdirSync } from "node:fs";
3+
import fs from "node:fs";
44
import { findDuplicates } from "./duplicates";
55
import chalk from "chalk";
66
import { packageDirectorySync } from "pkg-dir";
@@ -95,38 +95,26 @@ export function determineModuleContext(
9595
modulePath: string,
9696
originalPath = modulePath
9797
): ModuleContext {
98-
// (uses module-level packageNameCache)
99-
// Start from given path (file or directory)
100-
const startPath = modulePath;
101-
102-
// Recursive helper to locate package.json upward
103-
function recurse(currentPath: string): ModuleContext {
104-
const pkgJsonPath = path.join(currentPath, "package.json");
105-
const parentDir = path.dirname(currentPath);
106-
if (fs.existsSync(pkgJsonPath)) {
107-
// Resolve real path of package directory for caching
108-
const pkgDir = fs.realpathSync(currentPath);
109-
let pkgName: string;
110-
if (packageNameCache.has(pkgDir)) {
111-
pkgName = packageNameCache.get(pkgDir)!;
112-
} else {
113-
const content = fs.readFileSync(pkgJsonPath, "utf8");
114-
const json = JSON.parse(content) as { name: string };
115-
assert(typeof json.name === "string", "Expected package.json to have a name");
116-
pkgName = json.name;
117-
packageNameCache.set(pkgDir, pkgName);
118-
}
119-
const relPath = normalizeModulePath(
120-
path.relative(currentPath, originalPath)
121-
);
122-
return { packageName: pkgName, relativePath: relPath };
123-
}
124-
if (parentDir === currentPath) {
125-
throw new Error("Could not find containing package");
126-
}
127-
return recurse(parentDir);
98+
// Locate nearest package directory
99+
const pkgDir = packageDirectorySync({ cwd: modulePath });
100+
if (!pkgDir) {
101+
throw new Error("Could not find containing package");
102+
}
103+
// Read and cache package name
104+
let pkgName: string;
105+
if (packageNameCache.has(pkgDir)) {
106+
pkgName = packageNameCache.get(pkgDir)!;
107+
} else {
108+
const pkg = readPackageSync({ cwd: pkgDir });
109+
assert(typeof pkg.name === "string", "Expected package.json to have a name");
110+
pkgName = pkg.name;
111+
packageNameCache.set(pkgDir, pkgName);
128112
}
129-
return recurse(startPath);
113+
// Compute module-relative path
114+
const relPath = normalizeModulePath(
115+
path.relative(pkgDir, originalPath)
116+
);
117+
return { packageName: pkgName, relativePath: relPath };
130118
}
131119

132120
export function normalizeModulePath(modulePath: string) {
@@ -267,7 +255,7 @@ export function findNodeApiModulePaths(
267255
return [];
268256
}
269257
const candidatePath = path.join(fromPath, suffix);
270-
return readdirSync(candidatePath, { withFileTypes: true }).flatMap((file) => {
258+
return fs.readdirSync(candidatePath, { withFileTypes: true }).flatMap((file) => {
271259
if (
272260
file.isFile() &&
273261
file.name === MAGIC_FILENAME &&

0 commit comments

Comments
 (0)