-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (35 loc) · 1.2 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
const fs = require('fs').promises;
const path = require('path');
const regex = /process\.env\.(\w+)/g;
async function findEnvKeysInFile(filePath) {
const content = await fs.readFile(filePath, 'utf-8');
const matches = content.matchAll(regex);
const keys = [...matches].map(match => match[1]);
return keys;
}
async function findEnvKeysInDirectory(dirPath) {
const files = await fs.readdir(dirPath);
let keys = new Set();
for (const file of files) {
const filePath = path.join(dirPath, file);
if ((await fs.stat(filePath)).isDirectory()) {
(await findEnvKeysInDirectory(filePath)).forEach(item => keys.add(item));
} else if (filePath.endsWith('.ts') || filePath.endsWith('.js')) {
(await findEnvKeysInFile(filePath)).forEach(item => keys.add(item));
}
}
return keys;
}
async function checkEnv(__dirname) {
let keys = await findEnvKeysInDirectory(__dirname);
let errorOn = [];
for (const iterator of keys) {
if (!process.env[iterator]) {
errorOn.push(iterator);
}
}
if (errorOn.length) {
throw new Error(`${errorOn} keys are not defined in .env`);
}
}
module.exports = checkEnv;