1111 */
1212import { execSync } from 'node:child_process'
1313import { existsSync } from 'node:fs'
14- import { join } from 'node:path'
14+ import { join , sep } from 'node:path'
1515
16- export const packageManagers = [ 'pnpm' , 'npm' , 'yarn' ] as const
16+ export const packageManagers = [ 'pnpm' , 'npm' , 'yarn' , 'bun' ] as const
1717export type PackageManager = ( typeof packageManagers ) [ number ]
1818
1919export function detectInvokedPackageManager ( ) : PackageManager {
20- let detectedPackageManager : PackageManager = 'npm'
21- const invoker = require . main
22-
23- if ( ! invoker ) {
24- return detectedPackageManager
20+ if ( process . env . npm_config_user_agent ) {
21+ for ( const pm of packageManagers ) {
22+ if ( process . env . npm_config_user_agent . startsWith ( `${ pm } /` ) ) {
23+ return pm
24+ }
25+ }
2526 }
2627
27- for ( const pkgManager of packageManagers ) {
28- if ( invoker . path . includes ( pkgManager ) ) {
29- detectedPackageManager = pkgManager
30- break
28+ if ( process . env . npm_execpath ) {
29+ for ( const pm of packageManagers ) {
30+ if ( process . env . npm_execpath . split ( sep ) . includes ( pm ) ) {
31+ return pm
32+ }
3133 }
3234 }
33- return detectedPackageManager
35+ return 'npm'
3436}
3537
3638/**
@@ -99,6 +101,15 @@ export function getPackageManagerCommand(
99101 lockFile : 'package-lock.json' ,
100102 }
101103 }
104+
105+ case 'bun' : {
106+ return {
107+ install : `bun install ${ silent } ` ,
108+ exec : 'bun' ,
109+ globalAdd : 'bun add -g' ,
110+ lockFile : 'bun.lock' ,
111+ }
112+ }
102113 }
103114}
104115const pmVersionCache = new Map < PackageManager , string > ( )
@@ -123,6 +134,9 @@ export function detectPackageManager(dir: string = ''): PackageManager {
123134 if ( existsSync ( join ( dir , 'pnpm-lock.yaml' ) ) ) {
124135 return 'pnpm'
125136 }
137+ if ( existsSync ( join ( dir , 'bun.lock' ) ) ) {
138+ return 'bun'
139+ }
126140
127141 return 'npm'
128142}
0 commit comments