-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cpa): updates
.env.example
env vars along side .env
vars ba…
…sed on selected DB (#9757) ### What? Previously, the create-payload-app install would properly update the env vars in the new created `.env` file but kept outdated env vars in the `.env.example` file. I.e If selecting a `postgres` DB for the blank or website template: `.env` --> `DATABASE_URI` --> `postgres://postgres:<password>@127.0.0.1:5432/test-cpa` `.env.example` --> `DATABASE_URI` --> `mongodb://127.0.0.1/payload-template-blank-3-0` ### Now `.env` --> `DATABASE_URI` --> `postgres://postgres:<password>@127.0.0.1:5432/test-cpa` `.env.example` --> `DATABASE_URI` --> `postgres://postgres:<password>@127.0.0.1:5432/test-cpa`
- Loading branch information
1 parent
a1a0a07
commit 1aa23d3
Showing
3 changed files
with
108 additions
and
87 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
packages/create-payload-app/src/lib/manage-env-files.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,102 @@ | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
|
||
import type { CliArgs, DbType, ProjectTemplate } from '../types.js' | ||
|
||
import { debug, error } from '../utils/log.js' | ||
|
||
const updateEnvVariables = ( | ||
contents: string, | ||
databaseType: DbType | undefined, | ||
databaseUri: string, | ||
payloadSecret: string, | ||
): string => { | ||
return contents | ||
.split('\n') | ||
.filter((e) => e) | ||
.map((line) => { | ||
if (line.startsWith('#') || !line.includes('=')) { | ||
return line | ||
} | ||
|
||
const [key, ...valueParts] = line.split('=') | ||
let value = valueParts.join('=') | ||
|
||
if ( | ||
key === 'MONGODB_URI' || | ||
key === 'MONGO_URL' || | ||
key === 'DATABASE_URI' || | ||
key === 'POSTGRES_URL' | ||
) { | ||
value = databaseUri | ||
if (databaseType === 'vercel-postgres') { | ||
value = databaseUri | ||
} | ||
} | ||
|
||
if (key === 'PAYLOAD_SECRET' || key === 'PAYLOAD_SECRET_KEY') { | ||
value = payloadSecret | ||
} | ||
|
||
return `${key}=${value}` | ||
}) | ||
.join('\n') | ||
} | ||
|
||
/** Parse and swap .env.example values and write .env */ | ||
export async function manageEnvFiles(args: { | ||
cliArgs: CliArgs | ||
databaseType?: DbType | ||
databaseUri: string | ||
payloadSecret: string | ||
projectDir: string | ||
template?: ProjectTemplate | ||
}): Promise<void> { | ||
const { cliArgs, databaseType, databaseUri, payloadSecret, projectDir, template } = args | ||
|
||
if (cliArgs['--dry-run']) { | ||
debug(`DRY RUN: Environment files managed`) | ||
return | ||
} | ||
|
||
const envExamplePath = path.join(projectDir, '.env.example') | ||
const envPath = path.join(projectDir, '.env') | ||
|
||
try { | ||
let updatedExampleContents: string | ||
|
||
if (template?.type === 'starter') { | ||
if (!fs.existsSync(envExamplePath)) { | ||
error(`.env.example file not found at ${envExamplePath}`) | ||
process.exit(1) | ||
} | ||
|
||
const envExampleContents = await fs.readFile(envExamplePath, 'utf8') | ||
updatedExampleContents = updateEnvVariables( | ||
envExampleContents, | ||
databaseType, | ||
databaseUri, | ||
payloadSecret, | ||
) | ||
|
||
await fs.writeFile(envExamplePath, updatedExampleContents) | ||
debug(`.env.example file successfully updated`) | ||
} else { | ||
updatedExampleContents = `# Added by Payload\nDATABASE_URI=${databaseUri}\nPAYLOAD_SECRET=${payloadSecret}\n` | ||
} | ||
|
||
const existingEnvContents = fs.existsSync(envPath) ? await fs.readFile(envPath, 'utf8') : '' | ||
const updatedEnvContents = existingEnvContents | ||
? `${existingEnvContents}\n# Added by Payload\n${updatedExampleContents}` | ||
: `# Added by Payload\n${updatedExampleContents}` | ||
|
||
await fs.writeFile(envPath, updatedEnvContents) | ||
debug(`.env file successfully created or updated`) | ||
} catch (err: unknown) { | ||
error('Unable to manage environment files') | ||
if (err instanceof Error) { | ||
error(err.message) | ||
} | ||
process.exit(1) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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