@@ -23,11 +23,16 @@ export async function writeEnvFile(args: {
23
23
const envOutputPath = path . join ( projectDir , '.env' )
24
24
25
25
try {
26
- if ( fs . existsSync ( envOutputPath ) ) {
27
- if ( template ?. type === 'starter' ) {
28
- // Parse .env file into key/value pairs
29
- const envFile = await fs . readFile ( path . join ( projectDir , '.env.example' ) , 'utf8' )
30
- const envWithValues : string [ ] = envFile
26
+ let fileContents : string
27
+
28
+ if ( template ?. type === 'starter' ) {
29
+ // Parse .env file into key/value pairs
30
+ const envExample = path . join ( projectDir , '.env.example' )
31
+ const envFile = await fs . readFile ( envExample , 'utf8' )
32
+
33
+ fileContents =
34
+ `# Added by Payload\n` +
35
+ envFile
31
36
. split ( '\n' )
32
37
. filter ( ( e ) => e )
33
38
. map ( ( line ) => {
@@ -46,18 +51,17 @@ export async function writeEnvFile(args: {
46
51
47
52
return `${ key } =${ value } `
48
53
} )
54
+ . join ( '\n' )
55
+ } else {
56
+ fileContents = `# Added by Payload\nDATABASE_URI=${ databaseUri } \nPAYLOAD_SECRET=${ payloadSecret } \n`
57
+ }
49
58
50
- // Write new .env file
51
- await fs . writeFile ( envOutputPath , envWithValues . join ( '\n' ) )
52
- } else {
53
- const existingEnv = await fs . readFile ( envOutputPath , 'utf8' )
54
- const newEnv =
55
- existingEnv + `\nDATABASE_URI=${ databaseUri } \nPAYLOAD_SECRET=${ payloadSecret } \n`
56
- await fs . writeFile ( envOutputPath , newEnv )
57
- }
59
+ if ( fs . existsSync ( envOutputPath ) ) {
60
+ const existingEnv = await fs . readFile ( envOutputPath , 'utf8' )
61
+ const newEnv = existingEnv + '\n# Added by Payload' + fileContents
62
+ await fs . writeFile ( envOutputPath , newEnv )
58
63
} else {
59
- const content = `DATABASE_URI=${ databaseUri } \nPAYLOAD_SECRET=${ payloadSecret } `
60
- await fs . outputFile ( `${ projectDir } /.env` , content )
64
+ await fs . writeFile ( envOutputPath , fileContents )
61
65
}
62
66
} catch ( err : unknown ) {
63
67
error ( 'Unable to write .env file' )
0 commit comments