-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cpa): update existing payload installation (#6193)
Updates create-payload-app to update an existing payload installation - Detects existing Payload installation. Fixes #6517 - If not latest, will install latest and grab the `(payload)` directory structure (ripped from `templates/blank-3.0`
- Loading branch information
Showing
6 changed files
with
237 additions
and
66 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
packages/create-payload-app/src/lib/get-package-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// @ts-expect-error no types | ||
import { detect } from 'detect-package-manager' | ||
|
||
import type { CliArgs, PackageManager } from '../types.js' | ||
|
||
export async function getPackageManager(args: { | ||
cliArgs?: CliArgs | ||
projectDir: string | ||
}): Promise<PackageManager> { | ||
const { cliArgs, projectDir } = args | ||
|
||
if (!cliArgs) { | ||
const detected = await detect({ cwd: projectDir }) | ||
return 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import execa from 'execa' | ||
|
||
import type { PackageManager } from '../types.js' | ||
|
||
import { error, warning } from '../utils/log.js' | ||
|
||
export async function installPackages(args: { | ||
packageManager: PackageManager | ||
packagesToInstall: string[] | ||
projectDir: string | ||
}) { | ||
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], { | ||
cwd: projectDir, | ||
})) | ||
break | ||
} | ||
case 'bun': { | ||
warning('Bun support is untested.') | ||
;({ exitCode, stderr, stdout } = await execa('bun', ['add', ...packagesToInstall], { | ||
cwd: projectDir, | ||
})) | ||
break | ||
} | ||
} | ||
|
||
if (exitCode !== 0) { | ||
error(`Unable to install packages. Error: ${stderr}`) | ||
} | ||
|
||
return { success: exitCode === 0 } | ||
} |
89 changes: 89 additions & 0 deletions
89
packages/create-payload-app/src/lib/update-payload-in-project.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import * as p from '@clack/prompts' | ||
import execa from 'execa' | ||
import fse from 'fs-extra' | ||
import { fileURLToPath } from 'node:url' | ||
import path from 'path' | ||
|
||
const filename = fileURLToPath(import.meta.url) | ||
const dirname = path.dirname(filename) | ||
|
||
import type { NextAppDetails } from '../types.js' | ||
|
||
import { copyRecursiveSync } from '../utils/copy-recursive-sync.js' | ||
import { info } from '../utils/log.js' | ||
import { getPackageManager } from './get-package-manager.js' | ||
import { installPackages } from './install-packages.js' | ||
|
||
export async function updatePayloadInProject( | ||
appDetails: NextAppDetails, | ||
): Promise<{ message: string; success: boolean }> { | ||
if (!appDetails.nextConfigPath) return { message: 'No Next.js config found', success: false } | ||
|
||
const projectDir = path.dirname(appDetails.nextConfigPath) | ||
|
||
const packageObj = (await fse.readJson(path.resolve(projectDir, 'package.json'))) as { | ||
dependencies?: Record<string, string> | ||
} | ||
if (!packageObj?.dependencies) { | ||
throw new Error('No package.json found in this project') | ||
} | ||
|
||
const payloadVersion = packageObj.dependencies?.payload | ||
if (!payloadVersion) { | ||
throw new Error('Payload is not installed in this project') | ||
} | ||
|
||
const packageManager = await getPackageManager({ projectDir }) | ||
|
||
// Fetch latest Payload version from npm | ||
const { exitCode: getLatestVersionExitCode, stdout: latestPayloadVersion } = await execa('npm', [ | ||
'show', | ||
'payload@beta', | ||
'version', | ||
]) | ||
if (getLatestVersionExitCode !== 0) { | ||
throw new Error('Failed to fetch latest Payload version') | ||
} | ||
|
||
if (payloadVersion === latestPayloadVersion) { | ||
return { message: `Payload v${payloadVersion} is already up to date.`, success: true } | ||
} | ||
|
||
// Update all existing Payload packages | ||
const payloadPackages = Object.keys(packageObj.dependencies).filter((dep) => | ||
dep.startsWith('@payloadcms/'), | ||
) | ||
|
||
const packageNames = ['payload', ...payloadPackages] | ||
|
||
const packagesToUpdate = packageNames.map((pkg) => `${pkg}@${latestPayloadVersion}`) | ||
|
||
info( | ||
`Updating ${packagesToUpdate.length} Payload packages to v${latestPayloadVersion}...\n\n${packageNames.map((p) => ` - ${p}`).join('\n')}`, | ||
) | ||
|
||
const { success: updateSuccess } = await installPackages({ | ||
packageManager, | ||
packagesToInstall: packagesToUpdate, | ||
projectDir, | ||
}) | ||
|
||
if (!updateSuccess) { | ||
throw new Error('Failed to update Payload packages') | ||
} | ||
info('Payload packages updated successfully.') | ||
|
||
info(`Updating Payload Next.js files...`) | ||
const templateFilesPath = dirname.endsWith('dist') | ||
? path.resolve(dirname, '../..', 'dist/template') | ||
: path.resolve(dirname, '../../../../templates/blank-3.0') | ||
|
||
const templateSrcDir = path.resolve(templateFilesPath, 'src/app/(payload)') | ||
|
||
copyRecursiveSync( | ||
templateSrcDir, | ||
path.resolve(projectDir, appDetails.isSrcDir ? 'src/app' : 'app', '(payload)'), | ||
) | ||
|
||
return { message: 'Payload updated successfully.', success: true } | ||
} |
Oops, something went wrong.