@@ -52,12 +52,6 @@ export interface ScanParentDirsResult {
5252 * or `undefined` if not found.
5353 */
5454 lockfileVersion ?: number ;
55- /**
56- * The contents of the `packageManager` field from `package.json` if found.
57- * The value may come from a different `package.json` file than the one
58- * specified by `packageJsonPath`, in the case of a monorepo.
59- */
60- packageJsonPackageManager ?: string ;
6155}
6256
6357export interface TraverseUpDirectoriesProps {
@@ -315,19 +309,17 @@ export async function scanParentDirs(
315309 readPackageJson && pkgJsonPath
316310 ? JSON . parse ( await fs . readFile ( pkgJsonPath , 'utf8' ) )
317311 : undefined ;
318- const {
319- paths : [ yarnLockPath , npmLockPath , pnpmLockPath , bunLockPath ] ,
320- packageJsonPackageManager,
321- } = await walkParentDirsMulti ( {
322- base,
323- start : destPath ,
324- filenames : [
325- 'yarn.lock' ,
326- 'package-lock.json' ,
327- 'pnpm-lock.yaml' ,
328- 'bun.lockb' ,
329- ] ,
330- } ) ;
312+ const [ yarnLockPath , npmLockPath , pnpmLockPath , bunLockPath ] =
313+ await walkParentDirsMulti ( {
314+ base,
315+ start : destPath ,
316+ filenames : [
317+ 'yarn.lock' ,
318+ 'package-lock.json' ,
319+ 'pnpm-lock.yaml' ,
320+ 'bun.lockb' ,
321+ ] ,
322+ } ) ;
331323 let lockfilePath : string | undefined ;
332324 let lockfileVersion : number | undefined ;
333325 let cliType : CliType ;
@@ -367,25 +359,23 @@ export async function scanParentDirs(
367359 // TODO: read "bun-lockfile-format-v0"
368360 lockfileVersion = 0 ;
369361 } else {
370- cliType = detectPackageManagerNameWithoutLockfile (
371- packageJsonPackageManager
372- ) ;
362+ cliType = packageJson
363+ ? detectPackageManagerNameWithoutLockfile ( packageJson )
364+ : 'npm' ;
373365 }
374366
375367 const packageJsonPath = pkgJsonPath || undefined ;
376368 return {
377369 cliType,
378370 packageJson,
379- packageJsonPackageManager,
380371 lockfilePath,
381372 lockfileVersion,
382373 packageJsonPath,
383374 } ;
384375}
385376
386- function detectPackageManagerNameWithoutLockfile (
387- packageJsonPackageManager ?: string
388- ) {
377+ function detectPackageManagerNameWithoutLockfile ( packageJson : PackageJson ) {
378+ const packageJsonPackageManager = packageJson . packageManager ;
389379 if ( usingCorepack ( process . env , packageJsonPackageManager ) ) {
390380 const corepackPackageManager = validateVersionSpecifier (
391381 packageJsonPackageManager
@@ -439,35 +429,20 @@ async function walkParentDirsMulti({
439429 base,
440430 start,
441431 filenames,
442- } : WalkParentDirsMultiProps ) : Promise < {
443- paths : ( string | undefined ) [ ] ;
444- packageJsonPackageManager ?: string ;
445- } > {
446- let packageManager : string | undefined ;
447-
432+ } : WalkParentDirsMultiProps ) : Promise < ( string | undefined ) [ ] > {
448433 for ( const dir of traverseUpDirectories ( { start, base } ) ) {
449434 const fullPaths = filenames . map ( f => path . join ( dir , f ) ) ;
450435 const existResults = await Promise . all (
451436 fullPaths . map ( f => fs . pathExists ( f ) )
452437 ) ;
453438 const foundOneOrMore = existResults . some ( b => b ) ;
454- const packageJsonPath = path . join ( dir , 'package.json' ) ;
455- const packageJson : PackageJson | null = await fs
456- . readJSON ( packageJsonPath )
457- . catch ( ( ) => null ) ;
458- if ( packageJson ?. packageManager ) {
459- packageManager = packageJson . packageManager ;
460- }
461439
462440 if ( foundOneOrMore ) {
463- return {
464- paths : fullPaths . map ( ( f , i ) => ( existResults [ i ] ? f : undefined ) ) ,
465- packageJsonPackageManager : packageManager ,
466- } ;
441+ return fullPaths . map ( ( f , i ) => ( existResults [ i ] ? f : undefined ) ) ;
467442 }
468443 }
469444
470- return { paths : [ ] , packageJsonPackageManager : packageManager } ;
445+ return [ ] ;
471446}
472447
473448function isSet < T > ( v : any ) : v is Set < T > {
@@ -490,12 +465,8 @@ export async function runNpmInstall(
490465
491466 try {
492467 await runNpmInstallSema . acquire ( ) ;
493- const {
494- cliType,
495- packageJsonPath,
496- lockfileVersion,
497- packageJsonPackageManager,
498- } = await scanParentDirs ( destPath ) ;
468+ const { cliType, packageJsonPath, packageJson, lockfileVersion } =
469+ await scanParentDirs ( destPath , true ) ;
499470
500471 if ( ! packageJsonPath ) {
501472 debug (
@@ -530,7 +501,7 @@ export async function runNpmInstall(
530501 opts . env = getEnvForPackageManager ( {
531502 cliType,
532503 lockfileVersion,
533- packageJsonPackageManager,
504+ packageJsonPackageManager : packageJson ?. packageManager ,
534505 nodeVersion,
535506 env,
536507 } ) ;
@@ -1024,12 +995,14 @@ export async function runCustomInstallCommand({
1024995 spawnOpts ?: SpawnOptions ;
1025996} ) {
1026997 console . log ( `Running "install" command: \`${ installCommand } \`...` ) ;
1027- const { cliType, lockfileVersion, packageJsonPackageManager } =
1028- await scanParentDirs ( destPath ) ;
998+ const { cliType, lockfileVersion, packageJson } = await scanParentDirs (
999+ destPath ,
1000+ true
1001+ ) ;
10291002 const env = getEnvForPackageManager ( {
10301003 cliType,
10311004 lockfileVersion,
1032- packageJsonPackageManager,
1005+ packageJsonPackageManager : packageJson ?. packageManager ,
10331006 nodeVersion,
10341007 env : spawnOpts ?. env || { } ,
10351008 } ) ;
@@ -1048,8 +1021,10 @@ export async function runPackageJsonScript(
10481021) {
10491022 assert ( path . isAbsolute ( destPath ) ) ;
10501023
1051- const { packageJson, cliType, lockfileVersion, packageJsonPackageManager } =
1052- await scanParentDirs ( destPath , true ) ;
1024+ const { packageJson, cliType, lockfileVersion } = await scanParentDirs (
1025+ destPath ,
1026+ true
1027+ ) ;
10531028 const scriptName = getScriptName (
10541029 packageJson ,
10551030 typeof scriptNames === 'string' ? [ scriptNames ] : scriptNames
@@ -1065,7 +1040,7 @@ export async function runPackageJsonScript(
10651040 env : getEnvForPackageManager ( {
10661041 cliType,
10671042 lockfileVersion,
1068- packageJsonPackageManager,
1043+ packageJsonPackageManager : packageJson ?. packageManager ,
10691044 nodeVersion : undefined ,
10701045 env : cloneEnv ( process . env , spawnOpts ?. env ) ,
10711046 } ) ,
0 commit comments