(cwd: string, stopAt: string)
| 139 | } |
| 140 | |
| 141 | function lookupPackageJson(cwd: string, stopAt: string): PackageJson | undefined { |
| 142 | const jsonPath = findUp.sync( |
| 143 | dirName => { |
| 144 | // Stop if we reach this dir |
| 145 | if (path.normalize(dirName) === stopAt) { |
| 146 | return findUp.stop; |
| 147 | } |
| 148 | |
| 149 | return findUp.sync.exists(`${dirName}/package.json`) ? 'package.json' : undefined; |
| 150 | }, |
| 151 | { cwd }, |
| 152 | ); |
| 153 | |
| 154 | if (!jsonPath) { |
| 155 | return undefined; |
| 156 | } |
| 157 | |
| 158 | try { |
| 159 | const jsonStr = fs.readFileSync(jsonPath, 'utf8'); |
| 160 | const json = JSON.parse(jsonStr) as PackageJson; |
| 161 | |
| 162 | // Ensure it is an actual package.json |
| 163 | // This is very much not bulletproof, but should be good enough |
| 164 | if ('name' in json || 'private' in json) { |
| 165 | return json; |
| 166 | } |
| 167 | } catch { |
| 168 | // Ignore and walk up |
| 169 | } |
| 170 | |
| 171 | // Continue up the tree, if we find a fitting package.json |
| 172 | const newCwd = path.dirname(path.resolve(`${jsonPath}/..`)); |
| 173 | return lookupPackageJson(newCwd, stopAt); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Deterministically hashes a string and turns the hash into a uuid. |
no test coverage detected