-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
35 lines (29 loc) · 979 Bytes
/
index.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
import { readFileSync } from 'fs';
import { resolve } from 'path';
import createBuffer from './create-buffer';
const environment =
process.env.NODE_ENV || (process.env.NODE_ENV = 'development');
const filePath = resolve(process.cwd(), `${environment}.env`);
try {
const file: Buffer = readFileSync(filePath);
const buffer = createBuffer();
for (const bit of file) {
buffer.addBit(bit);
}
const bytes = buffer.end();
const strings = bytes.map((buf) => buf.toString('utf-8'));
strings.forEach((str) => {
const index = str.indexOf('=');
const key = str.substr(0, index);
const value = str.substr(index + 1);
process.env[key] = value;
});
console.info(`SETDOTENV: current environment ${process.env.NODE_ENV}`);
console.info(
`SETDOTENV: current env file ${filePath}. Was uploaded ${strings.length} variables`,
);
} catch (error) {
console.error(
`SETDOTENV: env file ${filePath} not found or includes wrong format`,
);
}