|
| 1 | +import path from 'node:path'; |
| 2 | +import fs from 'node:fs'; |
| 3 | + |
1 | 4 | function getTypedName(name: string) {
|
2 | 5 | return `@types/${name.replace(/^@/, '').replace('/', '__')}`;
|
3 | 6 | }
|
4 | 7 |
|
5 |
| -export { getTypedName }; |
| 8 | +/** |
| 9 | + * Locates the directory of the package.json for the given packageName. |
| 10 | + * 1. Resolves the entry file via require.resolve(). |
| 11 | + * 2. Looks for package.json in that same directory (fallback). |
| 12 | + * 3. Climb upward until the folder name matches localName |
| 13 | + * (the part after a scope slash, e.g. "@scope/sdk" => "sdk"). |
| 14 | + * 4. If that folder's package.json has "name" === packageName, return that directory. |
| 15 | + * 5. Otherwise, return the fallback directory (if it contains a package.json). |
| 16 | + * 6. If all else fails, throw an error. |
| 17 | + */ |
| 18 | +function getPackageRootDir(packageName: string | undefined): string { |
| 19 | + if (!packageName) { |
| 20 | + throw new Error('No package name provided.'); |
| 21 | + } |
| 22 | + |
| 23 | + // 1) Resolve the entry file |
| 24 | + const entryFile = require.resolve(packageName); |
| 25 | + const entryDir = path.dirname(entryFile); |
| 26 | + |
| 27 | + // 2) Fallback: check if there's a package.json in entryDir |
| 28 | + let fallbackPackageJsonPath: string | null = path.join( |
| 29 | + entryDir, |
| 30 | + 'package.json', |
| 31 | + ); |
| 32 | + if (!fs.existsSync(fallbackPackageJsonPath)) { |
| 33 | + fallbackPackageJsonPath = null; |
| 34 | + } |
| 35 | + |
| 36 | + // Figure out localName (e.g., "@scope/sdk" -> "sdk", "lodash" -> "lodash") |
| 37 | + const match = packageName.match(/^@[^/]+\/(.+)$/); |
| 38 | + const localName = match ? match[1] : packageName; |
| 39 | + |
| 40 | + // Start climbing from the entryDir |
| 41 | + let currentDir = entryDir; |
| 42 | + |
| 43 | + // 3) Use a while loop that continues until we either: |
| 44 | + // a) find a folder whose name is localName, or |
| 45 | + // b) reach the filesystem root (when path.dirname(currentDir) === currentDir) |
| 46 | + while ( |
| 47 | + path.basename(currentDir) !== localName && // Keep going if names differ |
| 48 | + path.dirname(currentDir) !== currentDir // Stop if we're at root |
| 49 | + ) { |
| 50 | + try { |
| 51 | + currentDir = path.dirname(currentDir); |
| 52 | + } catch (err) { |
| 53 | + // Permission error |
| 54 | + if (err.code === 'EACCES') { |
| 55 | + continue; |
| 56 | + } else { |
| 57 | + throw err; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // 4) If we ended because the folder name now matches localName, check package.json |
| 63 | + if (path.basename(currentDir) === localName) { |
| 64 | + const candidate = path.join(currentDir, 'package.json'); |
| 65 | + if (fs.existsSync(candidate)) { |
| 66 | + const pkgContent = JSON.parse(fs.readFileSync(candidate, 'utf8')); |
| 67 | + if (pkgContent.name === packageName) { |
| 68 | + return currentDir; // Found the correct root folder |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // 5) If we didn't find a matching package.json, fall back (if available) |
| 74 | + if (fallbackPackageJsonPath) { |
| 75 | + return entryDir; |
| 76 | + } |
| 77 | + |
| 78 | + // 6) Otherwise, throw an error |
| 79 | + throw new Error( |
| 80 | + `Could not find a matching package.json for "${packageName}" and no fallback was found.`, |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +export { getTypedName, getPackageRootDir }; |
0 commit comments