This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathvalidate-env.js
67 lines (66 loc) · 2.05 KB
/
validate-env.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require('./src/utils/logging');
const { join } = require('path');
const { config } = require('dotenv');
const { exists } = require('fs-extra');
const ValidationMessages = require('./validate-env-messages.json');
const REQUIRED_DOT_ENV_PROPS = ['TOKEN'];
function checkIfDotEnvExists() {
const dotEnvFilePath = join(__dirname, '.env');
return new Promise((resolve, reject) => {
exists(dotEnvFilePath).then(
(doesIt) =>
doesIt ? resolve() : reject({ message: '.env file not found' })
);
});
}
function validateDotEnvFile() {
return new Promise((resolve, reject) => {
config();
const missingProperties = [];
REQUIRED_DOT_ENV_PROPS.forEach((p) => {
const dotEnvProperty = process.env[p];
if (typeof dotEnvProperty === 'undefined' || !dotEnvProperty) {
missingProperties.push(p);
}
});
const missingRecommendedProperties = Object.keys(ValidationMessages).reduce(
(missing, current) => {
const dotEnvProperty = process.env[current];
if (typeof dotEnvProperty === 'undefined' || !dotEnvProperty) {
missing.push(` - ${current}:: ${ValidationMessages[current]}`);
}
return missing;
},
[]
);
missingProperties.length === 0
? resolve(
missingRecommendedProperties.length
? missingRecommendedProperties
: []
)
: reject({
message: `Please add these valid properties to your .env file - ${missingProperties.join(
', '
)}`,
});
});
}
function allGood(props) {
if (props.length) {
console.yellow(
`Some of the recommended properties are not provided in .env file`
);
console.yellow(props.join('\n'));
}
console.green('All good... Attempting to spawn the bot');
}
function somethingsWrong(error) {
console.red(`${error.message}\n`);
// exit process with error code 1 to make sure the subsequent start task invocation is prevented
process.exit(1);
}
checkIfDotEnvExists()
.then(validateDotEnvFile)
.then(allGood)
.catch(somethingsWrong);