|
1 | | -import { existsSync } from 'node:fs' |
2 | | -import path from 'node:path' |
3 | | - |
4 | 1 | import { resolveRealBinSync } from '@socketsecurity/lib-stable/bin/resolve' |
5 | 2 | import { whichRealSync } from '@socketsecurity/lib-stable/bin/which' |
6 | | -import { NPM } from '@socketsecurity/lib-stable/constants/agents' |
7 | | -import { WIN32 } from '@socketsecurity/lib-stable/constants/platform' |
8 | | -import { isDirSync } from '@socketsecurity/lib-stable/fs/inspect' |
9 | 3 |
|
10 | 4 | import { |
11 | 5 | createSupportedFilesFilter, |
12 | 6 | globWithGitIgnore, |
13 | 7 | pathsToGlobPatterns, |
14 | 8 | } from './glob.mts' |
15 | | -import { NODE_MODULES } from '../../constants/packages.mts' |
16 | 9 |
|
17 | 10 | import type { SocketYml } from '../socket-yaml.mts' |
18 | 11 | import type { SocketSdkSuccessResult } from '@socketsecurity/sdk-stable' |
@@ -42,73 +35,6 @@ export function findBinPathDetailsSync(binName: string): { |
42 | 35 | return { name: binName, path: theBinPath } |
43 | 36 | } |
44 | 37 |
|
45 | | -export function findNpmDirPathSync(npmBinPath: string): string | undefined { |
46 | | - // On Windows, reject Unix absolute paths (starting with / but not //). |
47 | | - // This allows UNC paths: //server/share, \\server\share. |
48 | | - // And long paths: \\?\C:\..., //?/C:/... |
49 | | - // Backslash paths (\\...) don't match startsWith('/') so they pass through. |
50 | | - /* c8 ignore start - WIN32-only branch, tests run on macOS/Linux */ |
51 | | - if (WIN32 && npmBinPath.startsWith('/') && !npmBinPath.startsWith('//')) { |
52 | | - return undefined |
53 | | - } |
54 | | - /* c8 ignore stop */ |
55 | | - const MAX_ITERATIONS = 100 |
56 | | - let thePath = npmBinPath |
57 | | - let iterations = 0 |
58 | | - while (true) { |
59 | | - /* c8 ignore start - safety guard against infinite loop on circular symlinks */ |
60 | | - if (iterations >= MAX_ITERATIONS) { |
61 | | - throw new Error( |
62 | | - `npm path resolution walked ${MAX_ITERATIONS} directories without finding lib/node_modules/npm starting from "${npmBinPath}" (current: "${thePath}"); check for a circular symlink or corrupt node install`, |
63 | | - ) |
64 | | - } |
65 | | - /* c8 ignore stop */ |
66 | | - iterations += 1 |
67 | | - const libNmNpmPath = path.join(thePath, `lib/${NODE_MODULES}/${NPM}`) |
68 | | - // mise, which uses opaque binaries, puts its npm bin in a path like: |
69 | | - // /Users/<user>/.local/share/mise/installs/node/vX.X.X/bin/npm. |
70 | | - // HOWEVER, the location of the npm install is: |
71 | | - // /Users/<user>/.local/share/mise/installs/node/vX.X.X/lib/node_modules/npm. |
72 | | - if ( |
73 | | - // Use existsSync here because statsSync, even with { throwIfNoEntry: false }, |
74 | | - // will throw an ENOTDIR error for paths like ./a-file-that-exists/a-directory-that-does-not. |
75 | | - // See https://github.com/nodejs/node/issues/56993. |
76 | | - isDirSync(libNmNpmPath) |
77 | | - ) { |
78 | | - thePath = libNmNpmPath |
79 | | - } |
80 | | - const hasNmInCurrPath = isDirSync(path.join(thePath, NODE_MODULES)) |
81 | | - const hasNmInParentPath = |
82 | | - !hasNmInCurrPath && isDirSync(path.join(thePath, `../${NODE_MODULES}`)) |
83 | | - if ( |
84 | | - // npm bin paths may look like: |
85 | | - // /usr/local/share/npm/bin/npm |
86 | | - // ~/.nvm/versions/node/vX.X.X/bin/npm |
87 | | - // %USERPROFILE%\AppData\Roaming\npm\bin\npm.cmd |
88 | | - // OR |
89 | | - // C:\Program Files\nodejs\npm.cmd |
90 | | - // |
91 | | - // In practically all cases the npm path contains a node_modules folder: |
92 | | - // /usr/local/share/npm/bin/npm/node_modules |
93 | | - // C:\Program Files\nodejs\node_modules |
94 | | - (hasNmInCurrPath || |
95 | | - // In some bespoke cases the node_modules folder is in the parent directory. |
96 | | - hasNmInParentPath) && |
97 | | - // Optimistically look for the default location. |
98 | | - (path.basename(thePath) === NPM || |
99 | | - // Chocolatey installs npm bins in the same directory as node bins. |
100 | | - (!!WIN32 && existsSync(path.join(thePath, `${NPM}.cmd`)))) |
101 | | - ) { |
102 | | - return hasNmInParentPath ? path.dirname(thePath) : thePath |
103 | | - } |
104 | | - const parent = path.dirname(thePath) |
105 | | - if (parent === thePath) { |
106 | | - return undefined |
107 | | - } |
108 | | - thePath = parent |
109 | | - } |
110 | | -} |
111 | | - |
112 | 38 | type PackageFilesForScanOptions = { |
113 | 39 | cwd?: string | undefined |
114 | 40 | config?: SocketYml | undefined |
|
0 commit comments