Skip to content

Commit

Permalink
fix(cpa): improve package manager detection (#6546)
Browse files Browse the repository at this point in the history
Improves package manager detection.

Closes #6231
  • Loading branch information
denolfe authored May 29, 2024
1 parent e749529 commit 8506385
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 50 deletions.
1 change: 0 additions & 1 deletion packages/create-payload-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/create-payload-app/src/lib/create-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
37 changes: 18 additions & 19 deletions packages/create-payload-app/src/lib/get-package-manager.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -9,22 +9,21 @@ export async function getPackageManager(args: {
}): Promise<PackageManager> {
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'
}
9 changes: 6 additions & 3 deletions packages/create-payload-app/src/lib/init-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand Down
22 changes: 7 additions & 15 deletions packages/create-payload-app/src/lib/install-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as p from '@clack/prompts'
import execa from 'execa'
import fse from 'fs-extra'
import { fileURLToPath } from 'node:url'
Expand Down Expand Up @@ -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')}`,
)
Expand Down
10 changes: 0 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8506385

Please sign in to comment.