@@ -4,6 +4,7 @@ import { parse as parseBunLockb } from '@socketregistry/hyrious__bun.lockb'
4
4
import spawn from '@npmcli/promise-spawn'
5
5
import browserslist from 'browserslist'
6
6
import semver from 'semver'
7
+ import which from 'which'
7
8
8
9
import { existsSync , findUp , readFileBinary , readFileUtf8 } from './fs'
9
10
import { parseJSONObject } from './json'
@@ -52,6 +53,7 @@ export type DetectOptions = {
52
53
53
54
export type DetectResult = Readonly < {
54
55
agent : AgentPlusBun
56
+ agentExecPath : string
55
57
agentVersion : string | undefined
56
58
isPrivate : boolean
57
59
isWorkspace : boolean
@@ -67,19 +69,27 @@ export type DetectResult = Readonly<{
67
69
}
68
70
} >
69
71
70
- type ReadLockFile = ( lockPath : string ) => Promise < string | undefined >
72
+ type ReadLockFile = (
73
+ lockPath : string ,
74
+ agentExecPath ?: string
75
+ ) => Promise < string | undefined >
71
76
72
77
const readLockFileByAgent : Record < AgentPlusBun , ReadLockFile > = ( ( ) => {
73
78
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 ) => {
76
86
try {
77
- return await reader ( lockPath )
87
+ return await reader ( lockPath , agentExecPath )
78
88
} catch { }
79
89
return undefined
80
90
}
81
91
return {
82
- bun : wrapReader ( async ( lockPath : string ) => {
92
+ bun : wrapReader ( async ( lockPath : string , agentExecPath ?: string ) => {
83
93
let lockBuffer : Buffer | undefined
84
94
try {
85
95
lockBuffer = < Buffer > await readFileBinary ( lockPath )
@@ -91,7 +101,7 @@ const readLockFileByAgent: Record<AgentPlusBun, ReadLockFile> = (() => {
91
101
} catch { }
92
102
// To print a Yarn lockfile to your console without writing it to disk use `bun bun.lockb`.
93
103
// https://bun.sh/guides/install/yarnlock
94
- return ( await spawn ( 'bun' , [ lockPath ] ) ) . stdout
104
+ return ( await spawn ( agentExecPath ?? 'bun' , [ lockPath ] ) ) . stdout
95
105
} ) ,
96
106
npm : wrapReader ( async ( lockPath : string ) => await readFileUtf8 ( lockPath ) ) ,
97
107
pnpm : wrapReader ( async ( lockPath : string ) => await readFileUtf8 ( lockPath ) ) ,
@@ -151,6 +161,7 @@ export async function detect({
151
161
agent = 'npm'
152
162
onUnknown ?.( pkgManager )
153
163
}
164
+ const agentExecPath = ( await which ( agent , { nothrow : true } ) ) ?? agent
154
165
155
166
let lockSrc : string | undefined
156
167
const targets = {
@@ -167,7 +178,6 @@ export async function detect({
167
178
! ! pkgJson [ 'workspaces' ] ||
168
179
( agent === 'pnpm' &&
169
180
existsSync ( path . join ( pkgPath , 'pnpm-workspace.yaml' ) ) )
170
-
171
181
let browser : boolean | undefined
172
182
let node : boolean | undefined
173
183
const browserField = getOwn ( pkgJson , 'browser' )
@@ -201,12 +211,12 @@ export async function detect({
201
211
}
202
212
lockSrc =
203
213
typeof lockPath === 'string'
204
- ? await readLockFileByAgent [ agent ] ( lockPath )
214
+ ? await readLockFileByAgent [ agent ] ( lockPath , agentExecPath )
205
215
: undefined
206
216
}
207
-
208
217
return < DetectResult > {
209
218
agent,
219
+ agentExecPath,
210
220
agentVersion,
211
221
isPrivate,
212
222
isWorkspace,
0 commit comments