| 429 | } |
| 430 | |
| 431 | async function seedEnvValues(environment: Environment, options: Options) { |
| 432 | const envVarValues = options.envVarValues || {} |
| 433 | const entries = Object.entries(envVarValues) |
| 434 | if (entries.length === 0) return |
| 435 | |
| 436 | const envLocalPath = joinPaths(options.targetDir, '.env.local') |
| 437 | if (!environment.exists(envLocalPath)) { |
| 438 | return |
| 439 | } |
| 440 | |
| 441 | let envContents = await environment.readFile(envLocalPath) |
| 442 | for (const [key, value] of entries) { |
| 443 | const escapedValue = value.replace(/\n/g, '\\n') |
| 444 | const nextLine = `${key}=${escapedValue}` |
| 445 | const pattern = new RegExp(`^${key}=.*$`, 'm') |
| 446 | |
| 447 | if (pattern.test(envContents)) { |
| 448 | envContents = envContents.replace(pattern, nextLine) |
| 449 | } else { |
| 450 | envContents += `${envContents.endsWith('\n') ? '' : '\n'}${nextLine}\n` |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | await environment.writeFile(envLocalPath, envContents) |
| 455 | } |
| 456 | |
| 457 | async function writeEnvExample(environment: Environment, options: Options) { |
| 458 | const envExamplePath = joinPaths(options.targetDir, '.env.example') |