| 71 | * @returns Full path to the module root, or null if not found |
| 72 | */ |
| 73 | export function getModuleRootSync(moduleName: string, filePath: string): string | null { |
| 74 | let currentDir = path.dirname(path.resolve(filePath)); |
| 75 | let isAtFsRoot = false; |
| 76 | while (!isAtFsRoot) { |
| 77 | const manifestPath = path.join(currentDir, 'package.json'); |
| 78 | try { |
| 79 | if ( |
| 80 | _fs.existsSync(manifestPath) && |
| 81 | (JSON.parse(_fs.readFileSync(manifestPath, 'utf8')) as {name?: string}).name === moduleName |
| 82 | ) { |
| 83 | return currentDir; |
| 84 | } |
| 85 | } catch { |
| 86 | // ignore |
| 87 | } |
| 88 | currentDir = path.dirname(currentDir); |
| 89 | isAtFsRoot = currentDir.length <= path.dirname(currentDir).length; |
| 90 | } |
| 91 | return null; |
| 92 | } |