|
| 1 | +import fs from 'fs-extra' |
| 2 | +import path from 'path' |
| 3 | + |
| 4 | +import type { CliArgs, DbType, ProjectTemplate } from '../types.js' |
| 5 | + |
| 6 | +import { debug, error } from '../utils/log.js' |
| 7 | + |
| 8 | +const updateEnvVariables = ( |
| 9 | + contents: string, |
| 10 | + databaseType: DbType | undefined, |
| 11 | + databaseUri: string, |
| 12 | + payloadSecret: string, |
| 13 | +): string => { |
| 14 | + return contents |
| 15 | + .split('\n') |
| 16 | + .filter((e) => e) |
| 17 | + .map((line) => { |
| 18 | + if (line.startsWith('#') || !line.includes('=')) { |
| 19 | + return line |
| 20 | + } |
| 21 | + |
| 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 |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if (key === 'PAYLOAD_SECRET' || key === 'PAYLOAD_SECRET_KEY') { |
| 38 | + value = payloadSecret |
| 39 | + } |
| 40 | + |
| 41 | + return `${key}=${value}` |
| 42 | + }) |
| 43 | + .join('\n') |
| 44 | +} |
| 45 | + |
| 46 | +/** Parse and swap .env.example values and write .env */ |
| 47 | +export async function manageEnvFiles(args: { |
| 48 | + cliArgs: CliArgs |
| 49 | + databaseType?: DbType |
| 50 | + databaseUri: string |
| 51 | + payloadSecret: string |
| 52 | + projectDir: string |
| 53 | + template?: ProjectTemplate |
| 54 | +}): Promise<void> { |
| 55 | + const { cliArgs, databaseType, databaseUri, payloadSecret, projectDir, template } = args |
| 56 | + |
| 57 | + if (cliArgs['--dry-run']) { |
| 58 | + debug(`DRY RUN: Environment files managed`) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + const envExamplePath = path.join(projectDir, '.env.example') |
| 63 | + const envPath = path.join(projectDir, '.env') |
| 64 | + |
| 65 | + try { |
| 66 | + let updatedExampleContents: string |
| 67 | + |
| 68 | + if (template?.type === 'starter') { |
| 69 | + if (!fs.existsSync(envExamplePath)) { |
| 70 | + error(`.env.example file not found at ${envExamplePath}`) |
| 71 | + process.exit(1) |
| 72 | + } |
| 73 | + |
| 74 | + 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) |
| 83 | + debug(`.env.example file successfully updated`) |
| 84 | + } else { |
| 85 | + updatedExampleContents = `# Added by Payload\nDATABASE_URI=${databaseUri}\nPAYLOAD_SECRET=${payloadSecret}\n` |
| 86 | + } |
| 87 | + |
| 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}` |
| 92 | + |
| 93 | + await fs.writeFile(envPath, updatedEnvContents) |
| 94 | + debug(`.env file successfully created or updated`) |
| 95 | + } catch (err: unknown) { |
| 96 | + error('Unable to manage environment files') |
| 97 | + if (err instanceof Error) { |
| 98 | + error(err.message) |
| 99 | + } |
| 100 | + process.exit(1) |
| 101 | + } |
| 102 | +} |
0 commit comments