@@ -47,6 +47,7 @@ async function getAgentVersion(
47
47
}
48
48
49
49
const LOCKS : Record < string , Agent > = {
50
+ 'bun.lock' : BUN ,
50
51
'bun.lockb' : BUN ,
51
52
// If both package-lock.json and npm-shrinkwrap.json are present in the root
52
53
// of a project, npm-shrinkwrap.json will take precedence and package-lock.json
@@ -66,43 +67,46 @@ const LOCKS: Record<string, Agent> = {
66
67
'node_modules/.package-lock.json' : NPM
67
68
}
68
69
69
- type ReadLockFile = (
70
- lockPath : string ,
71
- agentExecPath : string
72
- ) => Promise < string | undefined >
70
+ type ReadLockFile =
71
+ | ( ( lockPath : string ) => Promise < string | undefined > )
72
+ | ( ( lockPath : string , agentExecPath : string ) => Promise < string | undefined > )
73
73
74
74
const readLockFileByAgent : Record < Agent , ReadLockFile > = ( ( ) => {
75
- function wrapReader (
76
- reader : (
77
- lockPath : string ,
78
- agentExecPath : string
79
- ) => Promise < string | undefined >
80
- ) : ReadLockFile {
81
- return async ( lockPath : string , agentExecPath : string ) => {
75
+ function wrapReader < T extends ( ...args : any [ ] ) => Promise < any > > (
76
+ reader : T
77
+ ) : ( ...args : Parameters < T > ) => Promise < Awaited < ReturnType < T > > | undefined > {
78
+ return async ( ...args : any [ ] ) : Promise < any > => {
82
79
try {
83
- return await reader ( lockPath , agentExecPath )
80
+ return await reader ( ... args )
84
81
} catch { }
85
82
return undefined
86
83
}
87
84
}
85
+
86
+ const binaryReader = wrapReader ( readFileBinary )
87
+
88
88
const defaultReader = wrapReader (
89
89
async ( lockPath : string ) => await readFileUtf8 ( lockPath )
90
90
)
91
+
91
92
return {
92
93
[ BUN ] : wrapReader ( async ( lockPath : string , agentExecPath : string ) => {
93
- let lockBuffer : Buffer | undefined
94
- try {
95
- lockBuffer = < Buffer > await readFileBinary ( lockPath )
96
- } catch {
97
- return undefined
94
+ const ext = path . extname ( lockPath )
95
+ if ( ext === '.lock' ) {
96
+ return await defaultReader ( lockPath )
97
+ }
98
+ if ( ext === '.lockb' ) {
99
+ const lockBuffer = await binaryReader ( lockPath )
100
+ if ( lockBuffer ) {
101
+ try {
102
+ return parseBunLockb ( lockBuffer )
103
+ } catch { }
104
+ }
105
+ // To print a Yarn lockfile to your console without writing it to disk
106
+ // use `bun bun.lockb`.
107
+ // https://bun.sh/guides/install/yarnlock
108
+ return ( await spawn ( agentExecPath , [ lockPath ] ) ) . stdout . trim ( )
98
109
}
99
- try {
100
- return < string > parseBunLockb ( lockBuffer )
101
- } catch { }
102
- // To print a Yarn lockfile to your console without writing it to disk
103
- // use `bun bun.lockb`.
104
- // https://bun.sh/guides/install/yarnlock
105
- return ( await spawn ( agentExecPath , [ lockPath ] ) ) . stdout . trim ( )
106
110
} ) ,
107
111
[ NPM ] : defaultReader ,
108
112
[ PNPM ] : defaultReader ,
@@ -121,6 +125,7 @@ export type DetectResult = Readonly<{
121
125
agent : Agent
122
126
agentExecPath : string
123
127
agentVersion : SemVer | undefined
128
+ lockBasename : string | undefined
124
129
lockPath : string | undefined
125
130
lockSrc : string | undefined
126
131
minimumNodeVersion : string
@@ -139,7 +144,8 @@ export async function detect({
139
144
onUnknown
140
145
} : DetectOptions = { } ) : Promise < DetectResult > {
141
146
let lockPath = await findUp ( Object . keys ( LOCKS ) , { cwd } )
142
- const isHiddenLockFile = lockPath ?. endsWith ( '.package-lock.json' ) ?? false
147
+ let lockBasename = lockPath ? path . basename ( lockPath ) : undefined
148
+ const isHiddenLockFile = lockBasename === '.package-lock.json'
143
149
const pkgJsonPath = lockPath
144
150
? path . resolve ( lockPath , `${ isHiddenLockFile ? '../' : '' } ../package.json` )
145
151
: await findUp ( 'package.json' , { cwd } )
@@ -173,9 +179,9 @@ export async function detect({
173
179
agent === undefined &&
174
180
! isHiddenLockFile &&
175
181
typeof pkgJsonPath === 'string' &&
176
- typeof lockPath === 'string'
182
+ typeof lockBasename === 'string'
177
183
) {
178
- agent = < Agent > LOCKS [ path . basename ( lockPath ) ]
184
+ agent = < Agent > LOCKS [ lockBasename ]
179
185
}
180
186
if ( agent === undefined ) {
181
187
agent = NPM
@@ -238,12 +244,14 @@ export async function detect({
238
244
? await readLockFileByAgent [ agent ] ( lockPath , agentExecPath )
239
245
: undefined
240
246
} else {
247
+ lockBasename = undefined
241
248
lockPath = undefined
242
249
}
243
250
return < DetectResult > {
244
251
agent,
245
252
agentExecPath,
246
253
agentVersion,
254
+ lockBasename,
247
255
lockPath,
248
256
lockSrc,
249
257
minimumNodeVersion,
0 commit comments