From c6246618ba65ebe29c9444227ae54382919352a3 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 6 Sep 2024 15:57:20 +0300 Subject: [PATCH] fix(cpa): detect package manager from command execution environment (#8087) Previously, on some machines this command: `pnpx create-payload-app@beta app` created a project using `npm`, instead of `pnpm`, the same with `yarn`. Also, the way we detected the package manager was always prioritizing `pnpm`, even if they executed the command with `yarn` / `npm`. Now we are relying only on from which package manager user executed `create-payload-app`. The code for detection is grabbed from create-next-app https://github.com/vercel/next.js/blob/canary/packages/create-next-app/helpers/get-pkg-manager.ts --- .../src/lib/get-package-manager.ts | 37 +++++++++---------- packages/create-payload-app/src/main.ts | 2 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/packages/create-payload-app/src/lib/get-package-manager.ts b/packages/create-payload-app/src/lib/get-package-manager.ts index edaab86802f..a977dc045d6 100644 --- a/packages/create-payload-app/src/lib/get-package-manager.ts +++ b/packages/create-payload-app/src/lib/get-package-manager.ts @@ -1,12 +1,8 @@ -import execa from 'execa' import fse from 'fs-extra' import type { CliArgs, PackageManager } from '../types.js' -export async function getPackageManager(args: { - cliArgs?: CliArgs - projectDir: string -}): Promise { +export function getPackageManager(args: { cliArgs?: CliArgs; projectDir: string }): PackageManager { const { cliArgs, projectDir } = args try { @@ -21,14 +17,8 @@ export async function getPackageManager(args: { } else if (cliArgs?.['--use-bun'] || fse.existsSync(`${projectDir}/bun.lockb`)) { detected = 'bun' } else { - // Otherwise check for existing commands - if (await commandExists('pnpm')) { - detected = 'pnpm' - } else if (await commandExists('yarn')) { - detected = 'yarn' - } else { - detected = 'npm' - } + // Otherwise check the execution environment + detected = getEnvironmentPackageManager() } return detected @@ -37,11 +27,20 @@ export async function getPackageManager(args: { } } -async function commandExists(command: string): Promise { - try { - await execa.command(`command -v ${command}`) - return true - } catch { - return false +function getEnvironmentPackageManager(): PackageManager { + const userAgent = process.env.npm_config_user_agent || '' + + if (userAgent.startsWith('yarn')) { + return 'yarn' + } + + if (userAgent.startsWith('pnpm')) { + return 'pnpm' } + + if (userAgent.startsWith('bun')) { + return 'bun' + } + + return 'npm' } diff --git a/packages/create-payload-app/src/main.ts b/packages/create-payload-app/src/main.ts index e3a4bc8f4c9..4fe287f4870 100644 --- a/packages/create-payload-app/src/main.ts +++ b/packages/create-payload-app/src/main.ts @@ -133,7 +133,7 @@ export class Main { ? path.dirname(nextConfigPath) : path.resolve(process.cwd(), slugify(projectName)) - const packageManager = await getPackageManager({ cliArgs: this.args, projectDir }) + const packageManager = getPackageManager({ cliArgs: this.args, projectDir }) if (nextConfigPath) { p.log.step(