1 | import * as dotenv from 'dotenv';
|
2 | import { fs, fsPath } from './libs';
|
3 |
|
4 | export interface IReadOptions {
|
5 | path?: string;
|
6 | }
|
7 |
|
8 | const parentModuleDir = () => {
|
9 | const parts = __dirname.split('/');
|
10 | return parts
|
11 | .reverse()
|
12 | .splice(parts.indexOf('node_modules') + 1, parts.length)
|
13 | .reverse()
|
14 | .join('/');
|
15 | };
|
16 |
|
17 | const toPath = (options: IReadOptions = {}) => {
|
18 | return options.path || fsPath.join(parentModuleDir(), './.env');
|
19 | };
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export function exists(options: IReadOptions = {}) {
|
25 | return fs.existsSync(toPath(options));
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export function read(options: IReadOptions = {}): any {
|
32 | return exists(options) ? dotenv.config({ path: toPath(options) }) : {};
|
33 | }
|
34 |
|
35 |
|
36 | read();
|