Skip to content

Commit

Permalink
fix(cpa): ensures .env & .env.example file modifications occur before…
Browse files Browse the repository at this point in the history
… git initialization (#10312)

### What?

Previously, `.env` & `.env.example` modifications during `cpa` occurred
after a project was initialized.

### Why?

As a result, these modifications would be seen as uncommitted
modifications in the project repo.

### How?

Now, we make these modifications in the `createProject` script before
the project is initialized.

Also, updates the **template** `.env.example` files to use the generic
db name `your-database-name` for better alignment & clarity.

Fixes #10232
  • Loading branch information
PatrikKozak authored Jan 3, 2025
1 parent daf314c commit 3847718
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
14 changes: 14 additions & 0 deletions packages/create-payload-app/src/lib/create-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { configurePayloadConfig } from './configure-payload-config.js'
import { configurePluginProject } from './configure-plugin-project.js'
import { downloadExample } from './download-example.js'
import { downloadTemplate } from './download-template.js'
import { generateSecret } from './generate-secret.js'
import { manageEnvFiles } from './manage-env-files.js'

const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
Expand Down Expand Up @@ -142,6 +144,18 @@ export async function createProject(
}
}

// Call manageEnvFiles before initializing Git
if (dbDetails) {
await manageEnvFiles({
cliArgs,
databaseType: dbDetails.type,
databaseUri: dbDetails.dbUri,
payloadSecret: generateSecret(),
projectDir,
template: 'template' in args ? args.template : undefined,
})
}

// Remove yarn.lock file. This is only desired in Payload Cloud.
const lockPath = path.resolve(projectDir, 'pnpm-lock.yaml')
if (fse.existsSync(lockPath)) {
Expand Down
10 changes: 0 additions & 10 deletions packages/create-payload-app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ export class Main {
}
case 'starter': {
const dbDetails = await selectDb(this.args, projectName)
const payloadSecret = generateSecret()

await createProject({
cliArgs: this.args,
Expand All @@ -266,15 +265,6 @@ export class Main {
template,
})

await manageEnvFiles({
cliArgs: this.args,
databaseType: dbDetails.type,
databaseUri: dbDetails.dbUri,
payloadSecret,
projectDir,
template,
})

break
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/_template/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DATABASE_URI=mongodb://127.0.0.1/payload-template-blank-3-0
DATABASE_URI=mongodb://127.0.0.1/your-database-name
PAYLOAD_SECRET=YOUR_SECRET_HERE
2 changes: 1 addition & 1 deletion templates/blank/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DATABASE_URI=mongodb://127.0.0.1/payload-template-blank-3-0
DATABASE_URI=mongodb://127.0.0.1/your-database-name
PAYLOAD_SECRET=YOUR_SECRET_HERE
4 changes: 2 additions & 2 deletions templates/website/.env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Database connection string
DATABASE_URI=mongodb://127.0.0.1/payload-template-website
DATABASE_URI=mongodb://127.0.0.1/your-database-name

# Or use a PG connection string
#DATABASE_URI=postgresql://127.0.0.1:5432/payload-template-website
#DATABASE_URI=postgresql://127.0.0.1:5432/your-database-name

# Used to encrypt JWT tokens
PAYLOAD_SECRET=YOUR_SECRET_HERE
Expand Down
4 changes: 2 additions & 2 deletions templates/with-payload-cloud/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DATABASE_URI=mongodb://127.0.0.1/payload-template-blank-3-0
PAYLOAD_SECRET=YOUR_SECRET_HERE
DATABASE_URI=mongodb://127.0.0.1/your-database-name
PAYLOAD_SECRET=YOUR_SECRET_HERE

0 comments on commit 3847718

Please sign in to comment.