@@ -4,6 +4,7 @@ import { parse as parseBunLockb } from '@socketregistry/hyrious__bun.lockb'
44import spawn from '@npmcli/promise-spawn'
55import browserslist from 'browserslist'
66import semver from 'semver'
7+ import which from 'which'
78
89import { existsSync , findUp , readFileBinary , readFileUtf8 } from './fs'
910import { parseJSONObject } from './json'
@@ -52,6 +53,7 @@ export type DetectOptions = {
5253
5354export type DetectResult = Readonly < {
5455 agent : AgentPlusBun
56+ agentExecPath : string
5557 agentVersion : string | undefined
5658 isPrivate : boolean
5759 isWorkspace : boolean
@@ -67,19 +69,27 @@ export type DetectResult = Readonly<{
6769 }
6870} >
6971
70- type ReadLockFile = ( lockPath : string ) => Promise < string | undefined >
72+ type ReadLockFile = (
73+ lockPath : string ,
74+ agentExecPath ?: string
75+ ) => Promise < string | undefined >
7176
7277const readLockFileByAgent : Record < AgentPlusBun , ReadLockFile > = ( ( ) => {
7378 const wrapReader =
74- ( reader : ( lockPath : string ) => Promise < string | undefined > ) : ReadLockFile =>
75- async ( lockPath : string ) => {
79+ (
80+ reader : (
81+ lockPath : string ,
82+ agentExecPath ?: string
83+ ) => Promise < string | undefined >
84+ ) : ReadLockFile =>
85+ async ( lockPath : string , agentExecPath ?: string ) => {
7686 try {
77- return await reader ( lockPath )
87+ return await reader ( lockPath , agentExecPath )
7888 } catch { }
7989 return undefined
8090 }
8191 return {
82- bun : wrapReader ( async ( lockPath : string ) => {
92+ bun : wrapReader ( async ( lockPath : string , agentExecPath ?: string ) => {
8393 let lockBuffer : Buffer | undefined
8494 try {
8595 lockBuffer = < Buffer > await readFileBinary ( lockPath )
@@ -91,7 +101,7 @@ const readLockFileByAgent: Record<AgentPlusBun, ReadLockFile> = (() => {
91101 } catch { }
92102 // To print a Yarn lockfile to your console without writing it to disk use `bun bun.lockb`.
93103 // https://bun.sh/guides/install/yarnlock
94- return ( await spawn ( 'bun' , [ lockPath ] ) ) . stdout
104+ return ( await spawn ( agentExecPath ?? 'bun' , [ lockPath ] ) ) . stdout
95105 } ) ,
96106 npm : wrapReader ( async ( lockPath : string ) => await readFileUtf8 ( lockPath ) ) ,
97107 pnpm : wrapReader ( async ( lockPath : string ) => await readFileUtf8 ( lockPath ) ) ,
@@ -151,6 +161,7 @@ export async function detect({
151161 agent = 'npm'
152162 onUnknown ?.( pkgManager )
153163 }
164+ const agentExecPath = ( await which ( agent , { nothrow : true } ) ) ?? agent
154165
155166 let lockSrc : string | undefined
156167 const targets = {
@@ -167,7 +178,6 @@ export async function detect({
167178 ! ! pkgJson [ 'workspaces' ] ||
168179 ( agent === 'pnpm' &&
169180 existsSync ( path . join ( pkgPath , 'pnpm-workspace.yaml' ) ) )
170-
171181 let browser : boolean | undefined
172182 let node : boolean | undefined
173183 const browserField = getOwn ( pkgJson , 'browser' )
@@ -201,12 +211,12 @@ export async function detect({
201211 }
202212 lockSrc =
203213 typeof lockPath === 'string'
204- ? await readLockFileByAgent [ agent ] ( lockPath )
214+ ? await readLockFileByAgent [ agent ] ( lockPath , agentExecPath )
205215 : undefined
206216 }
207-
208217 return < DetectResult > {
209218 agent,
219+ agentExecPath,
210220 agentVersion,
211221 isPrivate,
212222 isWorkspace,
0 commit comments