-
Notifications
You must be signed in to change notification settings - Fork 4
/
loadEnv.js
36 lines (30 loc) · 988 Bytes
/
loadEnv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Generate a `.env.local` file depending on the value from TARGET_ENV environment variable
*/
import fs from 'fs';
/**
* @description copies the given `.env.base.${TARGET_ENV}` file to a `.env.local` file.
*/
const env = {
production: 'production',
testnet: 'testnet',
};
function copyEnvFile() {
console.log({ envToBuild: process.env.TARGET_ENV });
const target = env[process.env.TARGET_ENV] || env.testnet;
const dotenvPath = process.cwd() + `/.env.${target}`;
const fileStats = fs.statSync(dotenvPath);
if (!fileStats.isFile()) {
console.error(`[copyEnvFile] ${dotenvPath} is not a valid file`);
}
const buildDotEnv = '.env.local';
try {
fs.copyFileSync(dotenvPath, `${process.cwd()}/${buildDotEnv}`);
console.log(`${buildDotEnv} successfully copied with TARGET_ENV=${target}`);
} catch (error) {
console.error(`[copyEnvFile] there was an error copying ${buildDotEnv} file`);
console.error(error);
}
return;
}
copyEnvFile();