|
16 | 16 |
|
17 | 17 | import { findUp } from 'find-up'
|
18 | 18 | import { existsSync, promises as fs } from 'fs'
|
19 |
| -import { dirname } from 'path' |
| 19 | +import path from 'path' |
| 20 | +import { fileURLToPath } from 'url' |
20 | 21 |
|
21 | 22 | import { resolveFrom } from './resolveFrom.js'
|
22 | 23 |
|
| 24 | +const filename = fileURLToPath(import.meta.url) |
| 25 | +const dirname = path.dirname(filename) |
| 26 | + |
23 | 27 | export type NecessaryDependencies = {
|
24 | 28 | missing: string[]
|
25 | 29 | resolved: Map<
|
@@ -49,12 +53,31 @@ export async function getDependencies(
|
49 | 53 | try {
|
50 | 54 | const pkgPath = await fs.realpath(resolveFrom(baseDir, pkg))
|
51 | 55 |
|
52 |
| - const pkgDir = dirname(pkgPath) |
| 56 | + const pkgDir = path.dirname(pkgPath) |
53 | 57 |
|
54 | 58 | let packageJsonFilePath = null
|
55 | 59 |
|
56 |
| - await findUp('package.json', { type: 'file', cwd: pkgDir }).then((path) => { |
57 |
| - packageJsonFilePath = path |
| 60 | + const processCwd = process.cwd() |
| 61 | + const payloadPkgDirname = path.resolve(dirname, '../../../') // pkg dir (outside src) |
| 62 | + |
| 63 | + // if node_modules is in payloadPkgDirname, go to parent dir which contains node_modules |
| 64 | + if (payloadPkgDirname.includes('node_modules')) { |
| 65 | + payloadPkgDirname.split('node_modules').slice(0, -1) |
| 66 | + } |
| 67 | + |
| 68 | + await findUp('package.json', { type: 'file', cwd: pkgDir }).then((foundPath) => { |
| 69 | + if (foundPath) { |
| 70 | + const resolvedFoundPath = path.resolve(foundPath) |
| 71 | + const resolvedCwd = path.resolve(processCwd) |
| 72 | + |
| 73 | + if ( |
| 74 | + resolvedFoundPath.startsWith(resolvedCwd) || |
| 75 | + resolvedFoundPath.startsWith(payloadPkgDirname) |
| 76 | + ) { |
| 77 | + // We don't want to match node modules outside the user's project. Checking for both process.cwd and dirname is a reliable way to do this. |
| 78 | + packageJsonFilePath = foundPath |
| 79 | + } |
| 80 | + } |
58 | 81 | })
|
59 | 82 |
|
60 | 83 | if (packageJsonFilePath && existsSync(packageJsonFilePath)) {
|
|
0 commit comments