-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (42 loc) · 1.26 KB
/
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
43
44
45
46
47
48
49
50
51
const dotenv = require("dotenv");
const fs = require("fs");
const path = require("path");
const scanEnv = (
envFile = ".env",
exampleEnv = ".env.example",
ignoreEnv = null
) => {
// 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) {
// read and parse ignore env file
buf = Buffer.from(fs.readFileSync(path.resolve(ignoreEnv), "utf-8"));
ignoreConfig = dotenv.parse(buf); // will return an object
}
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) {
console.warn(
`Env Scan Report
------------------------------------------------------------------------
Total Missing Environment Variables:
${missingEnvs.length}
Missing Environment Variables:
${missingEnvs.join("\n")}
------------------------------------------------------------------------
`
);
return false;
}
return true;
};
module.exports = scanEnv;