Skip to content

Commit d4b5b99

Browse files
committed
Add findBinPathDetails
1 parent 8c37e7f commit d4b5b99

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/utils/path-resolve.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import fs from 'node:fs/promises'
1+
import { promises as fs, realpathSync } from 'node:fs'
22
import path from 'node:path'
33

44
import ignore from 'ignore'
55
import micromatch from 'micromatch'
66
import { glob as tinyGlob } from 'tinyglobby'
7+
import which from 'which'
78

9+
import constants from '../constants'
810
import { directoryPatterns } from './ignore-by-default'
911

1012
import type { SocketYml } from '@socketsecurity/config'
@@ -15,6 +17,8 @@ type GlobWithGitIgnoreOptions = GlobOptions & {
1517
socketConfig?: SocketYml | undefined
1618
}
1719

20+
const { shadowBinPath } = constants
21+
1822
async function filterGlobResultToSupportedFiles(
1923
entries: string[],
2024
supportedFiles: SocketSdkReturnType<'getReportSupportedFiles'>['data']
@@ -177,6 +181,28 @@ export function findRoot(filepath: string): string | undefined {
177181
}
178182
}
179183

184+
export async function findBinPathDetails(binName: string): Promise<{
185+
name: string
186+
path: string | undefined
187+
shadowed: boolean
188+
}> {
189+
let shadowIndex = -1
190+
const bins =
191+
(await which(binName, {
192+
all: true,
193+
nothrow: true
194+
})) ?? []
195+
const binPath = bins.find((binPath, i) => {
196+
// Skip our bin directory if it's in the front.
197+
if (realpathSync(path.dirname(binPath)) === shadowBinPath) {
198+
shadowIndex = i
199+
return false
200+
}
201+
return true
202+
})
203+
return { name: binName, path: binPath, shadowed: shadowIndex !== -1 }
204+
}
205+
180206
export async function getPackageFiles(
181207
cwd: string,
182208
inputPaths: string[],

0 commit comments

Comments
 (0)