| 311 | } |
| 312 | |
| 313 | async function seedEnvValues(environment: Environment, options: Options) { |
| 314 | const envVarValues = options.envVarValues || {} |
| 315 | const entries = Object.entries(envVarValues) |
| 316 | if (entries.length === 0) return |
| 317 | |
| 318 | const envLocalPath = resolve(options.targetDir, '.env.local') |
| 319 | if (!environment.exists(envLocalPath)) { |
| 320 | return |
| 321 | } |
| 322 | |
| 323 | let envContents = await environment.readFile(envLocalPath) |
| 324 | for (const [key, value] of entries) { |
| 325 | const escapedValue = value.replace(/\n/g, '\\n') |
| 326 | const nextLine = `${key}=${escapedValue}` |
| 327 | const pattern = new RegExp(`^${key}=.*$`, 'm') |
| 328 | |
| 329 | if (pattern.test(envContents)) { |
| 330 | envContents = envContents.replace(pattern, nextLine) |
| 331 | } else { |
| 332 | envContents += `${envContents.endsWith('\n') ? '' : '\n'}${nextLine}\n` |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | await environment.writeFile(envLocalPath, envContents) |
| 337 | } |
| 338 | |
| 339 | async function writeEnvExample(environment: Environment, options: Options) { |
| 340 | const envExamplePath = resolve(options.targetDir, '.env.example') |