@@ -50,6 +50,18 @@ const node_path_1 = require("node:path");
50
50
const workspace_schema_1 = require ( "../../lib/config/workspace-schema" ) ;
51
51
const config_1 = require ( "./config" ) ;
52
52
const memoize_1 = require ( "./memoize" ) ;
53
+ /**
54
+ * A map of package managers to their corresponding lockfile names.
55
+ */
56
+ const LOCKFILE_NAMES = {
57
+ [ workspace_schema_1 . PackageManager . Yarn ] : 'yarn.lock' ,
58
+ [ workspace_schema_1 . PackageManager . Pnpm ] : 'pnpm-lock.yaml' ,
59
+ [ workspace_schema_1 . PackageManager . Bun ] : [ 'bun.lockb' , 'bun.lock' ] ,
60
+ [ workspace_schema_1 . PackageManager . Npm ] : 'package-lock.json' ,
61
+ } ;
62
+ /**
63
+ * Utilities for interacting with various package managers.
64
+ */
53
65
let PackageManagerUtils = ( ( ) => {
54
66
let _instanceExtraInitializers = [ ] ;
55
67
let _getVersion_decorators ;
@@ -64,6 +76,9 @@ let PackageManagerUtils = (() => {
64
76
if ( _metadata ) Object . defineProperty ( this , Symbol . metadata , { enumerable : true , configurable : true , writable : true , value : _metadata } ) ;
65
77
}
66
78
context = __runInitializers ( this , _instanceExtraInitializers ) ;
79
+ /**
80
+ * @param context The context for the package manager utilities, including workspace and global configuration.
81
+ */
67
82
constructor ( context ) {
68
83
this . context = context ;
69
84
}
@@ -211,10 +226,11 @@ let PackageManagerUtils = (() => {
211
226
if ( packageManager ) {
212
227
return packageManager ;
213
228
}
214
- const hasNpmLock = this . hasLockfile ( workspace_schema_1 . PackageManager . Npm ) ;
215
- const hasYarnLock = this . hasLockfile ( workspace_schema_1 . PackageManager . Yarn ) ;
216
- const hasPnpmLock = this . hasLockfile ( workspace_schema_1 . PackageManager . Pnpm ) ;
217
- const hasBunLock = this . hasLockfile ( workspace_schema_1 . PackageManager . Bun ) ;
229
+ const filesInRoot = ( 0 , node_fs_1 . readdirSync ) ( this . context . root ) ;
230
+ const hasNpmLock = this . hasLockfile ( workspace_schema_1 . PackageManager . Npm , filesInRoot ) ;
231
+ const hasYarnLock = this . hasLockfile ( workspace_schema_1 . PackageManager . Yarn , filesInRoot ) ;
232
+ const hasPnpmLock = this . hasLockfile ( workspace_schema_1 . PackageManager . Pnpm , filesInRoot ) ;
233
+ const hasBunLock = this . hasLockfile ( workspace_schema_1 . PackageManager . Bun , filesInRoot ) ;
218
234
// PERF NOTE: `this.getVersion` spawns the package a the child_process which can take around ~300ms at times.
219
235
// Therefore, we should only call this method when needed. IE: don't call `this.getVersion(PackageManager.Pnpm)` unless truly needed.
220
236
// The result of this method is not stored in a variable because it's memoized.
@@ -259,24 +275,17 @@ let PackageManagerUtils = (() => {
259
275
// Potentially with a prompt to choose and optionally set as the default.
260
276
return workspace_schema_1 . PackageManager . Npm ;
261
277
}
262
- hasLockfile ( packageManager ) {
263
- let lockfileName ;
264
- switch ( packageManager ) {
265
- case workspace_schema_1 . PackageManager . Yarn :
266
- lockfileName = 'yarn.lock' ;
267
- break ;
268
- case workspace_schema_1 . PackageManager . Pnpm :
269
- lockfileName = 'pnpm-lock.yaml' ;
270
- break ;
271
- case workspace_schema_1 . PackageManager . Bun :
272
- lockfileName = 'bun.lockb' ;
273
- break ;
274
- case workspace_schema_1 . PackageManager . Npm :
275
- default :
276
- lockfileName = 'package-lock.json' ;
277
- break ;
278
- }
279
- return ( 0 , node_fs_1 . existsSync ) ( ( 0 , node_path_1 . join ) ( this . context . root , lockfileName ) ) ;
278
+ /**
279
+ * Checks if a lockfile for a specific package manager exists in the root directory.
280
+ * @param packageManager The package manager to check for.
281
+ * @param filesInRoot An array of file names in the root directory.
282
+ * @returns True if the lockfile exists, false otherwise.
283
+ */
284
+ hasLockfile ( packageManager , filesInRoot ) {
285
+ const lockfiles = LOCKFILE_NAMES [ packageManager ] ;
286
+ return typeof lockfiles === 'string'
287
+ ? filesInRoot . includes ( lockfiles )
288
+ : lockfiles . some ( ( lockfile ) => filesInRoot . includes ( lockfile ) ) ;
280
289
}
281
290
getConfiguredPackageManager ( ) {
282
291
const getPackageManager = ( source ) => {
0 commit comments