|
1 | 1 | import assert from "node:assert/strict"; |
2 | 2 | import path from "node:path"; |
3 | | -import fs, { readdirSync } from "node:fs"; |
| 3 | +import fs from "node:fs"; |
4 | 4 | import { findDuplicates } from "./duplicates"; |
5 | 5 | import chalk from "chalk"; |
6 | 6 | import { packageDirectorySync } from "pkg-dir"; |
@@ -95,38 +95,26 @@ export function determineModuleContext( |
95 | 95 | modulePath: string, |
96 | 96 | originalPath = modulePath |
97 | 97 | ): 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); |
128 | 112 | } |
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 }; |
130 | 118 | } |
131 | 119 |
|
132 | 120 | export function normalizeModulePath(modulePath: string) { |
@@ -267,7 +255,7 @@ export function findNodeApiModulePaths( |
267 | 255 | return []; |
268 | 256 | } |
269 | 257 | const candidatePath = path.join(fromPath, suffix); |
270 | | - return readdirSync(candidatePath, { withFileTypes: true }).flatMap((file) => { |
| 258 | + return fs.readdirSync(candidatePath, { withFileTypes: true }).flatMap((file) => { |
271 | 259 | if ( |
272 | 260 | file.isFile() && |
273 | 261 | file.name === MAGIC_FILENAME && |
|
0 commit comments