-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathdotenv.ts
46 lines (35 loc) · 1.33 KB
/
dotenv.ts
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
37
38
39
40
41
42
43
44
45
46
import * as Dotenv from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';
import { NODE_ENV } from './env';
import { isTruthy } from './utils';
const PATH_CWD = fs.realpathSync(process.cwd());
const readDotEnvFile = (path: string) => {
if (!fs.existsSync(path)) return null;
const contentString = fs.readFileSync(path, { encoding: 'utf-8' });
return Dotenv.parse(contentString);
};
const dotenvDistPath = path.resolve(PATH_CWD, '.env.dist');
const distDotEnvFileData = readDotEnvFile(dotenvDistPath);
const requiredEnvFileVarsNames = Object.keys(distDotEnvFileData!);
const dotenvPath = path.resolve(PATH_CWD, '.env');
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
const dotenvFilesPaths = [
`${dotenvPath}.${NODE_ENV}.local`,
/*
Don't include `.env.local` for `test` environment
since normally you expect tests to produce the same
results for everyone
*/
NODE_ENV !== 'test' && `${dotenvPath}.local`,
`${dotenvPath}.${NODE_ENV}`,
dotenvPath
].filter(isTruthy);
const envFilesData: Record<string, string> = dotenvFilesPaths.reduce(
(data, path) => ({ ...data, ...readDotEnvFile(path) }),
{}
);
for (const name of requiredEnvFileVarsNames) {
if (!envFilesData[name]) throw new Error(`[.env] Required \`${name}\` value is not set in .env files`);
}
export { envFilesData };