| 35 | * @returns {boolean} is the package installed? |
| 36 | */ |
| 37 | const isInstalled = (packageName) => { |
| 38 | if (process.versions.pnp) { |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | const path = require("path"); |
| 43 | const fs = require("graceful-fs"); |
| 44 | |
| 45 | let dir = __dirname; |
| 46 | |
| 47 | do { |
| 48 | try { |
| 49 | if ( |
| 50 | fs.statSync(path.join(dir, "node_modules", packageName)).isDirectory() |
| 51 | ) { |
| 52 | return true; |
| 53 | } |
| 54 | } catch (_error) { |
| 55 | // Nothing |
| 56 | } |
| 57 | } while (dir !== (dir = path.dirname(dir))); |
| 58 | |
| 59 | // https://github.com/nodejs/node/blob/v18.9.1/lib/internal/modules/cjs/loader.js#L1274 |
| 60 | const { globalPaths } = |
| 61 | /** @type {typeof import("module") & { globalPaths: string[] }} */ |
| 62 | (require("module")); |
| 63 | |
| 64 | for (const internalPath of globalPaths) { |
| 65 | try { |
| 66 | if (fs.statSync(path.join(internalPath, packageName)).isDirectory()) { |
| 67 | return true; |
| 68 | } |
| 69 | } catch (_error) { |
| 70 | // Nothing |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * @param {CliOption} cli options |