Skip to content

Commit 898e97e

Browse files
authored
fix(cpa): ensure it always installs the latest version of the templates (#12488)
CPA would previously install an outdated version of the templates based on the git tag, this is now set to the `main` branch ensuring that the latest version is always installed.
1 parent 8142a00 commit 898e97e

File tree

5 files changed

+50
-27
lines changed

5 files changed

+50
-27
lines changed

packages/create-payload-app/src/lib/constants.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/create-payload-app/src/lib/templates.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { ProjectTemplate } from '../types.js'
22

33
import { error, info } from '../utils/log.js'
4-
import { PACKAGE_VERSION } from './constants.js'
54

6-
export function validateTemplate(templateName: string): boolean {
5+
export function validateTemplate({ templateName }: { templateName: string }): boolean {
76
const validTemplates = getValidTemplates()
87
if (!validTemplates.map((t) => t.name).includes(templateName)) {
98
error(`'${templateName}' is not a valid template.`)
@@ -20,13 +19,13 @@ export function getValidTemplates(): ProjectTemplate[] {
2019
name: 'blank',
2120
type: 'starter',
2221
description: 'Blank 3.0 Template',
23-
url: `https://github.com/payloadcms/payload/templates/blank#v${PACKAGE_VERSION}`,
22+
url: `https://github.com/payloadcms/payload/templates/blank#main`,
2423
},
2524
{
2625
name: 'website',
2726
type: 'starter',
2827
description: 'Website Template',
29-
url: `https://github.com/payloadcms/payload/templates/website#v${PACKAGE_VERSION}`,
28+
url: `https://github.com/payloadcms/payload/templates/website#main`,
3029
},
3130
{
3231
name: 'plugin',

packages/create-payload-app/src/lib/update-payload-in-project.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import execa from 'execa'
21
import fse from 'fs-extra'
32
import { fileURLToPath } from 'node:url'
43
import path from 'path'
@@ -9,6 +8,7 @@ const dirname = path.dirname(filename)
98
import type { NextAppDetails } from '../types.js'
109

1110
import { copyRecursiveSync } from '../utils/copy-recursive-sync.js'
11+
import { getLatestPackageVersion } from '../utils/getLatestPackageVersion.js'
1212
import { info } from '../utils/log.js'
1313
import { getPackageManager } from './get-package-manager.js'
1414
import { installPackages } from './install-packages.js'
@@ -36,15 +36,8 @@ export async function updatePayloadInProject(
3636

3737
const packageManager = await getPackageManager({ projectDir })
3838

39-
// Fetch latest Payload version from npm
40-
const { exitCode: getLatestVersionExitCode, stdout: latestPayloadVersion } = await execa('npm', [
41-
'show',
42-
'payload',
43-
'version',
44-
])
45-
if (getLatestVersionExitCode !== 0) {
46-
throw new Error('Failed to fetch latest Payload version')
47-
}
39+
// Fetch latest Payload version
40+
const latestPayloadVersion = await getLatestPackageVersion({ packageName: 'payload' })
4841

4942
if (payloadVersion === latestPayloadVersion) {
5043
return { message: `Payload v${payloadVersion} is already up to date.`, success: true }

packages/create-payload-app/src/main.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import path from 'path'
88
import type { CliArgs } from './types.js'
99

1010
import { configurePayloadConfig } from './lib/configure-payload-config.js'
11-
import { PACKAGE_VERSION } from './lib/constants.js'
1211
import { createProject } from './lib/create-project.js'
1312
import { parseExample } from './lib/examples.js'
1413
import { generateSecret } from './lib/generate-secret.js'
@@ -20,6 +19,7 @@ import { parseTemplate } from './lib/parse-template.js'
2019
import { selectDb } from './lib/select-db.js'
2120
import { getValidTemplates, validateTemplate } from './lib/templates.js'
2221
import { updatePayloadInProject } from './lib/update-payload-in-project.js'
22+
import { getLatestPackageVersion } from './utils/getLatestPackageVersion.js'
2323
import { debug, error, info } from './utils/log.js'
2424
import {
2525
feedbackOutro,
@@ -78,13 +78,18 @@ export class Main {
7878

7979
async init(): Promise<void> {
8080
try {
81+
const debugFlag = this.args['--debug']
82+
83+
const LATEST_VERSION = await getLatestPackageVersion({
84+
debug: debugFlag,
85+
packageName: 'payload',
86+
})
87+
8188
if (this.args['--help']) {
8289
helpMessage()
8390
process.exit(0)
8491
}
8592

86-
const debugFlag = this.args['--debug']
87-
8893
// eslint-disable-next-line no-console
8994
console.log('\n')
9095
p.intro(chalk.bgCyan(chalk.black(' create-payload-app ')))
@@ -200,7 +205,7 @@ export class Main {
200205

201206
const templateArg = this.args['--template']
202207
if (templateArg) {
203-
const valid = validateTemplate(templateArg)
208+
const valid = validateTemplate({ templateName: templateArg })
204209
if (!valid) {
205210
helpMessage()
206211
process.exit(1)
@@ -230,7 +235,7 @@ export class Main {
230235
}
231236

232237
if (debugFlag) {
233-
debug(`Using ${exampleArg ? 'examples' : 'templates'} from git tag: v${PACKAGE_VERSION}`)
238+
debug(`Using ${exampleArg ? 'examples' : 'templates'} from git tag: v${LATEST_VERSION}`)
234239
}
235240

236241
if (!exampleArg) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Fetches the latest version of a package from the NPM registry.
3+
*
4+
* Used in determining the latest version of Payload to use in the generated templates.
5+
*/
6+
export async function getLatestPackageVersion({
7+
debug = false,
8+
packageName = 'payload',
9+
}: {
10+
debug?: boolean
11+
/**
12+
* Package name to fetch the latest version for based on the NPM registry URL
13+
*
14+
* Eg. for `'payload'`, it will fetch the version from `https://registry.npmjs.org/payload`
15+
*
16+
* @default 'payload'
17+
*/
18+
packageName?: string
19+
}) {
20+
try {
21+
const response = await fetch(`https://registry.npmjs.org/${packageName}`)
22+
const data = await response.json()
23+
const latestVersion = data['dist-tags'].latest
24+
25+
if (debug) {
26+
console.log(`Found latest version of ${packageName}: ${latestVersion}`)
27+
}
28+
29+
return latestVersion
30+
} catch (error) {
31+
console.error('Error fetching Payload version:', error)
32+
throw error
33+
}
34+
}

0 commit comments

Comments
 (0)