Skip to content

Commit 70666a0

Browse files
authored
fix(cpa): updates CPAs w/ vercel-postgres db types to use POSTGRES_URL & updates .env.example to use generic env var strings (#10027)
CPA projects generated with the `vercel-postgres` db type were not receiving the proper DB env vars in the .env.example & .env files With the `vercel-postgres` db type, the DB env var needs to be `POSTGRES_URL` not `DATABASE_URI`. Additionally, updates the generated .env.example file to show generic env var strings. #### Blank w/ MongoDB: - `.env.example`: ``` DATABASE_URI=mongodb://127.0.0.1/your-database-name PAYLOAD_SECRET=YOUR_SECRET_HERE ``` - `.env`: ``` # Added by Payload DATABASE_URI=mongodb://127.0.0.1/test-cpa-blank-mongodb PAYLOAD_SECRET=aef857429edc7f42a90bb374 ``` #### Blank w/ Postgres: - `.env.example`: ``` DATABASE_URI=postgres://postgres:<password>@127.0.0.1:5432/your-database-name PAYLOAD_SECRET=YOUR_SECRET_HERE ``` - `.env`: ``` # Added by Payload DATABASE_URI=postgres://postgres:<password>@127.0.0.1:5432/test-cpa-blank-postgres PAYLOAD_SECRET=241bfe11fbe0a56dd9757019 ``` #### Blank w/ SQLite: - `.env.example`: ``` DATABASE_URI=file:./your-database-name.db PAYLOAD_SECRET=YOUR_SECRET_HERE ``` - `.env`: ``` # Added by Payload DATABASE_URI=file:./test-cpa-blank-sqlite.db PAYLOAD_SECRET=a7808731b93240a73a11930c ``` #### Blank w/ vercel-postgres: - `.env.example`: ``` POSTGRES_URL=postgres://postgres:<password>@127.0.0.1:5432/your-database-name PAYLOAD_SECRET=YOUR_SECRET_HERE ``` - `.env`: ``` # Added by Payload POSTGRES_URL=postgres://postgres:<password>@127.0.0.1:5432/test-cpa-blank-vercel-postgres PAYLOAD_SECRET=af3951e923e8e4662c9c3d9e ``` Fixes #9996
1 parent b0b2fc6 commit 70666a0

File tree

2 files changed

+62
-37
lines changed

2 files changed

+62
-37
lines changed

packages/create-payload-app/src/lib/manage-env-files.ts

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,66 @@ import path from 'path'
44
import type { CliArgs, DbType, ProjectTemplate } from '../types.js'
55

66
import { debug, error } from '../utils/log.js'
7+
import { dbChoiceRecord } from './select-db.js'
78

8-
const updateEnvVariables = (
9-
contents: string,
10-
databaseType: DbType | undefined,
11-
databaseUri: string,
12-
payloadSecret: string,
13-
): string => {
9+
const updateEnvExampleVariables = (contents: string, databaseType: DbType | undefined): string => {
1410
return contents
1511
.split('\n')
16-
.filter((e) => e)
1712
.map((line) => {
1813
if (line.startsWith('#') || !line.includes('=')) {
19-
return line
14+
return line // Preserve comments and unrelated lines
2015
}
2116

22-
const [key, ...valueParts] = line.split('=')
23-
let value = valueParts.join('=')
24-
25-
if (
26-
key === 'MONGODB_URI' ||
27-
key === 'MONGO_URL' ||
28-
key === 'DATABASE_URI' ||
29-
key === 'POSTGRES_URL'
30-
) {
31-
value = databaseUri
32-
if (databaseType === 'vercel-postgres') {
33-
value = databaseUri
17+
const [key] = line.split('=')
18+
19+
if (key === 'DATABASE_URI' || key === 'POSTGRES_URL' || key === 'MONGODB_URI') {
20+
const dbChoice = databaseType ? dbChoiceRecord[databaseType] : null
21+
22+
if (dbChoice) {
23+
const placeholderUri = `${dbChoice.dbConnectionPrefix}your-database-name${
24+
dbChoice.dbConnectionSuffix || ''
25+
}`
26+
return databaseType === 'vercel-postgres'
27+
? `POSTGRES_URL=${placeholderUri}`
28+
: `DATABASE_URI=${placeholderUri}`
3429
}
30+
31+
return `DATABASE_URI=your-database-connection-here` // Fallback
3532
}
3633

3734
if (key === 'PAYLOAD_SECRET' || key === 'PAYLOAD_SECRET_KEY') {
38-
value = payloadSecret
35+
return `PAYLOAD_SECRET=YOUR_SECRET_HERE`
3936
}
4037

41-
return `${key}=${value}`
38+
return line
39+
})
40+
.join('\n')
41+
}
42+
43+
const generateEnvContent = (
44+
existingEnv: string,
45+
databaseType: DbType | undefined,
46+
databaseUri: string,
47+
payloadSecret: string,
48+
): string => {
49+
const dbKey = databaseType === 'vercel-postgres' ? 'POSTGRES_URL' : 'DATABASE_URI'
50+
51+
const envVars: Record<string, string> = {}
52+
existingEnv
53+
.split('\n')
54+
.filter((line) => line.includes('=') && !line.startsWith('#'))
55+
.forEach((line) => {
56+
const [key, value] = line.split('=')
57+
envVars[key] = value
4258
})
59+
60+
// Override specific keys
61+
envVars[dbKey] = databaseUri
62+
envVars['PAYLOAD_SECRET'] = payloadSecret
63+
64+
// Rebuild content
65+
return Object.entries(envVars)
66+
.map(([key, value]) => `${key}=${value}`)
4367
.join('\n')
4468
}
4569

@@ -65,32 +89,33 @@ export async function manageEnvFiles(args: {
6589
try {
6690
let updatedExampleContents: string
6791

92+
// Update .env.example
6893
if (template?.type === 'starter') {
6994
if (!fs.existsSync(envExamplePath)) {
7095
error(`.env.example file not found at ${envExamplePath}`)
7196
process.exit(1)
7297
}
7398

7499
const envExampleContents = await fs.readFile(envExamplePath, 'utf8')
75-
updatedExampleContents = updateEnvVariables(
76-
envExampleContents,
77-
databaseType,
78-
databaseUri,
79-
payloadSecret,
80-
)
81-
82-
await fs.writeFile(envExamplePath, updatedExampleContents)
100+
updatedExampleContents = updateEnvExampleVariables(envExampleContents, databaseType)
101+
102+
await fs.writeFile(envExamplePath, updatedExampleContents.trimEnd() + '\n')
83103
debug(`.env.example file successfully updated`)
84104
} else {
85-
updatedExampleContents = `# Added by Payload\nDATABASE_URI=${databaseUri}\nPAYLOAD_SECRET=${payloadSecret}\n`
105+
updatedExampleContents = `# Added by Payload\nDATABASE_URI=your-connection-string-here\nPAYLOAD_SECRET=YOUR_SECRET_HERE\n`
106+
await fs.writeFile(envExamplePath, updatedExampleContents.trimEnd() + '\n')
86107
}
87108

88-
const existingEnvContents = fs.existsSync(envPath) ? await fs.readFile(envPath, 'utf8') : ''
89-
const updatedEnvContents = existingEnvContents
90-
? `${existingEnvContents}\n# Added by Payload\n${updatedExampleContents}`
91-
: `# Added by Payload\n${updatedExampleContents}`
109+
// Merge existing variables and create or update .env
110+
const envExampleContents = await fs.readFile(envExamplePath, 'utf8')
111+
const envContent = generateEnvContent(
112+
envExampleContents,
113+
databaseType,
114+
databaseUri,
115+
payloadSecret,
116+
)
117+
await fs.writeFile(envPath, `# Added by Payload\n${envContent.trimEnd()}\n`)
92118

93-
await fs.writeFile(envPath, updatedEnvContents)
94119
debug(`.env file successfully created or updated`)
95120
} catch (err: unknown) {
96121
error('Unable to manage environment files')

packages/create-payload-app/src/lib/select-db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type DbChoice = {
1010
value: DbType
1111
}
1212

13-
const dbChoiceRecord: Record<DbType, DbChoice> = {
13+
export const dbChoiceRecord: Record<DbType, DbChoice> = {
1414
mongodb: {
1515
dbConnectionPrefix: 'mongodb://127.0.0.1/',
1616
title: 'MongoDB',

0 commit comments

Comments
 (0)