-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
485d343
commit 625ed09
Showing
7 changed files
with
66 additions
and
52 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,49 +1,55 @@ | ||
import path from 'node:path'; | ||
import fs from 'node:fs/promises'; | ||
import fs from "node:fs/promises"; | ||
import path from "node:path"; | ||
|
||
const envFiles = ['.env', '.env.local']; | ||
const envFiles = [".env", ".env.local"]; | ||
|
||
const resolveEnvPaths = async () => { | ||
const cwd = process.cwd(); | ||
const paths = envFiles.map(file => path.join(cwd, file)); | ||
|
||
const existingFiles = await Promise.all( | ||
paths.map(async (filePath) => { | ||
try { | ||
await fs.access(filePath); | ||
return filePath; | ||
} catch { | ||
return null; | ||
} | ||
}) | ||
); | ||
|
||
return existingFiles.filter(Boolean); | ||
const cwd = process.cwd(); | ||
const paths = envFiles.map((file) => path.join(cwd, file)); | ||
|
||
const existingFiles = await Promise.all( | ||
paths.map(async (filePath) => { | ||
try { | ||
await fs.access(filePath); | ||
return filePath; | ||
} catch { | ||
return null; | ||
} | ||
}), | ||
); | ||
|
||
return existingFiles.filter(Boolean); | ||
}; | ||
|
||
const writeEnvFile = async (filePath: string, variables: Record<string, string>, existingEnv = '') => { | ||
const newEnvFile = Object.entries(variables).reduce((acc, [key, value]) => { | ||
if (!acc.includes(`${key}=`)) { | ||
return `${acc}\n${key}=${value}`; | ||
} | ||
return acc; | ||
}, existingEnv); | ||
|
||
await fs.writeFile(filePath, newEnvFile); | ||
} | ||
const writeEnvFile = async ( | ||
filePath: string, | ||
variables: Record<string, string>, | ||
existingEnv = "", | ||
) => { | ||
const newEnvFile = Object.entries(variables).reduce((acc, [key, value]) => { | ||
if (!acc.includes(`${key}=`)) { | ||
return `${acc}\n${key}=${value}`; | ||
} | ||
return acc; | ||
}, existingEnv); | ||
|
||
await fs.writeFile(filePath, newEnvFile); | ||
}; | ||
|
||
export const appendEnvironmentVariables = async (variables: Record<string, string>) => { | ||
const envPaths = await resolveEnvPaths(); | ||
export const appendEnvironmentVariables = async ( | ||
variables: Record<string, string>, | ||
) => { | ||
const envPaths = await resolveEnvPaths(); | ||
|
||
if (envPaths.length === 0) { | ||
await writeEnvFile('.env.local', variables); | ||
} else { | ||
for (const envPath of envPaths) { | ||
if (!envPath) continue; | ||
if (envPaths.length === 0) { | ||
await writeEnvFile(".env.local", variables); | ||
} else { | ||
for (const envPath of envPaths) { | ||
if (!envPath) continue; | ||
|
||
const envFile = await fs.readFile(envPath, 'utf8'); | ||
const envFile = await fs.readFile(envPath, "utf8"); | ||
|
||
await writeEnvFile(envPath, variables, envFile); | ||
} | ||
} | ||
} | ||
await writeEnvFile(envPath, variables, envFile); | ||
} | ||
} | ||
}; |
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
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
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
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
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