From 2cecf0fd6da973252b66b1efa93afc11b33dba0c Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Wed, 29 May 2024 09:13:17 -0400 Subject: [PATCH] fix(cpa): improve package manager detection --- packages/create-payload-app/package.json | 1 - .../src/lib/create-project.ts | 3 +- .../src/lib/get-package-manager.ts | 37 +++++++++---------- .../create-payload-app/src/lib/init-next.ts | 9 +++-- .../src/lib/install-packages.ts | 22 ++++------- .../src/lib/update-payload-in-project.ts | 2 +- pnpm-lock.yaml | 10 ----- 7 files changed, 34 insertions(+), 50 deletions(-) diff --git a/packages/create-payload-app/package.json b/packages/create-payload-app/package.json index 21c944f0eea..abaf761072a 100644 --- a/packages/create-payload-app/package.json +++ b/packages/create-payload-app/package.json @@ -41,7 +41,6 @@ "command-exists": "^1.2.9", "comment-json": "^4.2.3", "degit": "^2.8.4", - "detect-package-manager": "^3.0.1", "esprima-next": "^6.0.3", "execa": "^5.0.0", "figures": "^6.1.0", diff --git a/packages/create-payload-app/src/lib/create-project.ts b/packages/create-payload-app/src/lib/create-project.ts index 73f82980c47..5fc3b4c398f 100644 --- a/packages/create-payload-app/src/lib/create-project.ts +++ b/packages/create-payload-app/src/lib/create-project.ts @@ -9,7 +9,7 @@ import path from 'path' import type { CliArgs, DbDetails, PackageManager, ProjectTemplate } from '../types.js' import { tryInitRepoAndCommit } from '../utils/git.js' -import { debug, error, warning } from '../utils/log.js' +import { debug, error, info, warning } from '../utils/log.js' import { configurePayloadConfig } from './configure-payload-config.js' const filename = fileURLToPath(import.meta.url) @@ -99,6 +99,7 @@ export async function createProject(args: { } if (!cliArgs['--no-deps']) { + info(`Using ${packageManager}.\n`) spinner.message('Installing dependencies...') const result = await installDeps({ cliArgs, packageManager, projectDir }) if (result) { 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 00f566ae326..e718a7ee452 100644 --- a/packages/create-payload-app/src/lib/get-package-manager.ts +++ b/packages/create-payload-app/src/lib/get-package-manager.ts @@ -1,5 +1,5 @@ -// @ts-expect-error no types -import { detect } from 'detect-package-manager' +import commandExists from 'command-exists' +import fse from 'fs-extra' import type { CliArgs, PackageManager } from '../types.js' @@ -9,22 +9,21 @@ export async function getPackageManager(args: { }): Promise { const { cliArgs, projectDir } = args - if (!cliArgs) { - const detected = await detect({ cwd: projectDir }) - return detected || 'npm' + // Check for yarn.lock, package-lock.json, or pnpm-lock.yaml + let detected: PackageManager = 'npm' + if ( + cliArgs?.['--use-pnpm'] || + fse.existsSync(`${projectDir}/pnpm-lock.yaml`) || + (await commandExists('pnpm')) + ) { + detected = 'pnpm' + } else if ( + (cliArgs?.['--use-yarn'] && fse.existsSync(`${projectDir}/yarn.lock`)) || + (await commandExists('yarn')) + ) { + detected = 'yarn' + } else if (cliArgs?.['--use-npm'] && fse.existsSync(`${projectDir}/package-lock.json`)) { + detected = 'npm' } - - let packageManager: PackageManager = 'npm' - - if (cliArgs['--use-npm']) { - packageManager = 'npm' - } else if (cliArgs['--use-yarn']) { - packageManager = 'yarn' - } else if (cliArgs['--use-pnpm']) { - packageManager = 'pnpm' - } else { - const detected = await detect({ cwd: projectDir }) - packageManager = detected || 'npm' - } - return packageManager + return detected || 'npm' } diff --git a/packages/create-payload-app/src/lib/init-next.ts b/packages/create-payload-app/src/lib/init-next.ts index ae33d293634..2bb162144f1 100644 --- a/packages/create-payload-app/src/lib/init-next.ts +++ b/packages/create-payload-app/src/lib/init-next.ts @@ -221,9 +221,12 @@ function installAndConfigurePayload( } async function installDeps(projectDir: string, packageManager: PackageManager, dbType: DbType) { - const packagesToInstall = ['payload', '@payloadcms/next', '@payloadcms/richtext-lexical'].map( - (pkg) => `${pkg}@beta`, - ) + const packagesToInstall = [ + 'payload', + '@payloadcms/next', + '@payloadcms/richtext-lexical', + '@payloadcms/plugin-cloud', + ].map((pkg) => `${pkg}@beta`) packagesToInstall.push(`@payloadcms/db-${dbType}@beta`) diff --git a/packages/create-payload-app/src/lib/install-packages.ts b/packages/create-payload-app/src/lib/install-packages.ts index ed03e5efad7..c147eda1b6c 100644 --- a/packages/create-payload-app/src/lib/install-packages.ts +++ b/packages/create-payload-app/src/lib/install-packages.ts @@ -12,30 +12,22 @@ export async function installPackages(args: { const { packageManager, packagesToInstall, projectDir } = args let exitCode = 0 - let stdout = '' let stderr = '' switch (packageManager) { case 'npm': { - ;({ exitCode, stderr, stdout } = await execa( - 'npm', - ['install', '--save', ...packagesToInstall], - { - cwd: projectDir, - }, - )) - break - } - case 'yarn': - case 'pnpm': { - ;({ exitCode, stderr, stdout } = await execa(packageManager, ['add', ...packagesToInstall], { + ;({ exitCode, stderr } = await execa('npm', ['install', '--save', ...packagesToInstall], { cwd: projectDir, })) break } + case 'yarn': + case 'pnpm': case 'bun': { - warning('Bun support is untested.') - ;({ exitCode, stderr, stdout } = await execa('bun', ['add', ...packagesToInstall], { + if (packageManager === 'bun') { + warning('Bun support is untested.') + } + ;({ exitCode, stderr } = await execa(packageManager, ['add', ...packagesToInstall], { cwd: projectDir, })) break diff --git a/packages/create-payload-app/src/lib/update-payload-in-project.ts b/packages/create-payload-app/src/lib/update-payload-in-project.ts index bd4b8c48ac1..5e9450db787 100644 --- a/packages/create-payload-app/src/lib/update-payload-in-project.ts +++ b/packages/create-payload-app/src/lib/update-payload-in-project.ts @@ -1,4 +1,3 @@ -import * as p from '@clack/prompts' import execa from 'execa' import fse from 'fs-extra' import { fileURLToPath } from 'node:url' @@ -58,6 +57,7 @@ export async function updatePayloadInProject( const packagesToUpdate = packageNames.map((pkg) => `${pkg}@${latestPayloadVersion}`) + info(`Using ${packageManager}.\n`) info( `Updating ${packagesToUpdate.length} Payload packages to v${latestPayloadVersion}...\n\n${packageNames.map((p) => ` - ${p}`).join('\n')}`, ) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dcd5f3cd202..fc496215cf3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -290,9 +290,6 @@ importers: degit: specifier: ^2.8.4 version: 2.8.4 - detect-package-manager: - specifier: ^3.0.1 - version: 3.0.1 esprima-next: specifier: ^6.0.3 version: 6.0.3 @@ -9930,13 +9927,6 @@ packages: engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} dev: true - /detect-package-manager@3.0.1: - resolution: {integrity: sha512-qoHDH6+lMcpJPAScE7+5CYj91W0mxZNXTwZPrCqi1KMk+x+AoQScQ2V1QyqTln1rHU5Haq5fikvOGHv+leKD8A==} - engines: {node: '>=12'} - dependencies: - execa: 5.1.1 - dev: false - /diff-sequences@27.5.1: resolution: {integrity: sha512-k1gCAXAsNgLwEL+Y8Wvl+M6oEFj5bgazfZULpS5CneoPPXRaCCW7dm+q21Ky2VEE5X+VeRDBVg1Pcvvsr4TtNQ==} engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0}