forked from schadokar/scan-env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (35 loc) · 1023 Bytes
/
index.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
37
38
39
40
41
42
const dotenv = require("dotenv");
const fs = require("fs");
const path = require("path");
const scanEnv = (
envFile = ".env",
exampleEnv = ".env.example",
ignoreEnv = ".envignore"
) => {
// load envFile
dotenv.config({ path: path.resolve(envFile) });
// read and parse example env file
let buf = Buffer.from(fs.readFileSync(path.resolve(exampleEnv), "utf-8"));
const config = dotenv.parse(buf); // will return an object
let ignoreConfig = {};
if (ignoreEnv) {
try {
// read and parse ignore env file
buf = Buffer.from(fs.readFileSync(path.resolve(ignoreEnv), "utf-8"));
ignoreConfig = dotenv.parse(buf); // will return an object
}
catch (e) {}
}
const missingEnvs = [];
for (let k in config) {
// check if env is missing and not ignore
if (!process.env[k] && !ignoreConfig[k]) {
missingEnvs.push(k);
}
}
if (missingEnvs.length) {
return missingEnvs;
}
return true;
};
module.exports = scanEnv;