( nodeModulesDirectory: string, parentDirectories?: string[] )
| 7 | * Explains why a `node_modules` directory should be skipped, or `null` if safe. |
| 8 | */ |
| 9 | export async function getSkippedNodeModulesReason( |
| 10 | nodeModulesDirectory: string, |
| 11 | parentDirectories?: string[] |
| 12 | ): Promise<string | null> { |
| 13 | const parentDirectory = path.dirname(nodeModulesDirectory); |
| 14 | parentDirectories ??= [parentDirectory]; |
| 15 | |
| 16 | for (const directory of parentDirectories) { |
| 17 | let unsafeParentReason: string | null; |
| 18 | |
| 19 | try { |
| 20 | unsafeParentReason = await getUnsafeDirectoryReason(directory); |
| 21 | } catch (error) { |
| 22 | unsafeParentReason = `could not inspect: ${getErrorMessage(error)}`; |
| 23 | } |
| 24 | |
| 25 | if (unsafeParentReason) { |
| 26 | return `${directory} is ${unsafeParentReason}`; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | const result = await statIfExists(nodeModulesDirectory); |
| 31 | |
| 32 | if ('missing' in result) { |
| 33 | return null; |
| 34 | } |
| 35 | |
| 36 | if ('reason' in result) { |
| 37 | return result.reason; |
| 38 | } |
| 39 | |
| 40 | if (!result.stats.isDirectory()) { |
| 41 | return 'not a directory'; |
| 42 | } |
| 43 | |
| 44 | const unsafeNodeModulesReason = getUnsafeStatsReason(result.stats); |
| 45 | |
| 46 | if (unsafeNodeModulesReason) { |
| 47 | return unsafeNodeModulesReason; |
| 48 | } |
| 49 | |
| 50 | return await getSkippedLocalBinDirectoryReason( |
| 51 | path.join(nodeModulesDirectory, '.bin') |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Explains why a `.bin` directory should be skipped, or `null` if safe. |
no test coverage detected