File size: 6,080 Bytes
3b6afc0 |
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
const dotenv = require('dotenv');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
/**
* This class is responsible for loading the environment variables
*
* Inspired by: https://thekenyandev.com/blog/environment-variables-strategy-for-node/
*/
class Env {
constructor() {
this.envMap = {
default: '.env',
development: '.env.development',
test: '.env.test',
production: '.env.production',
};
this.init();
this.isProduction = process.env.NODE_ENV === 'production';
this.domains = {
client: process.env.DOMAIN_CLIENT,
server: process.env.DOMAIN_SERVER,
};
}
/**
* Initialize the environment variables
*/
init() {
let hasDefault = false;
// Load the default env file if it exists
if (fs.existsSync(this.envMap.default)) {
hasDefault = true;
dotenv.config({
path: this.resolve(this.envMap.default),
});
} else {
console.warn('The default .env file was not found');
}
const environment = this.currentEnvironment();
// Load the environment specific env file
const envFile = this.envMap[environment];
// check if the file exists
if (fs.existsSync(envFile)) {
dotenv.config({
path: this.resolve(envFile),
});
} else if (!hasDefault) {
console.warn('No env files found, have you completed the install process?');
}
}
/**
* Validate Config
*/
validate() {
const requiredKeys = [
'NODE_ENV',
'JWT_SECRET',
'DOMAIN_CLIENT',
'DOMAIN_SERVER',
'CREDS_KEY',
'CREDS_IV',
];
const missingKeys = requiredKeys
.map((key) => {
const variable = process.env[key];
if (variable === undefined || variable === null) {
return key;
}
})
.filter((value) => value !== undefined);
// Throw an error if any required keys are missing
if (missingKeys.length) {
const message = `
The following required env variables are missing:
${missingKeys.toString()}.
Please add them to your env file or run 'npm run install'
`;
throw new Error(message);
}
// Check JWT secret for default
if (process.env.JWT_SECRET === 'secret') {
console.warn('Warning: JWT_SECRET is set to default value');
}
}
/**
* Resolve the location of the env file
*
* @param {String} envFile
* @returns
*/
resolve(envFile) {
return path.resolve(process.cwd(), envFile);
}
/**
* Add secure keys to the env
*
* @param {String} filePath The path of the .env you are updating
* @param {String} key The env you are adding
* @param {Number} length The length of the secure key
*/
addSecureEnvVar(filePath, key, length) {
const env = {};
env[key] = this.generateSecureRandomString(length);
this.writeEnvFile(filePath, env);
}
/**
* Write the change to the env file
*/
writeEnvFile(filePath, env) {
const content = fs.readFileSync(filePath, 'utf-8');
const lines = content.split('\n');
const updatedLines = lines
.map((line) => {
if (line.trim().startsWith('#')) {
// Allow comment removal
if (env[line] === 'remove') {
return null; // Mark the line for removal
}
// Preserve comments
return line;
}
const [key, value] = line.split('=');
if (key && value && Object.prototype.hasOwnProperty.call(env, key.trim())) {
if (env[key.trim()] === 'remove') {
return null; // Mark the line for removal
}
return `${key.trim()}=${env[key.trim()]}`;
}
return line;
})
.filter((line) => line !== null); // Remove lines marked for removal
// Add any new environment variables that are not in the file yet
Object.entries(env).forEach(([key, value]) => {
if (value !== 'remove' && !updatedLines.some((line) => line.startsWith(`${key}=`))) {
updatedLines.push(`${key}=${value}`);
}
});
// Loop through updatedLines and wrap values with spaces in double quotes
const fixedLines = updatedLines.map((line) => {
// lets only split the first = sign
const [key, value] = line.split(/=(.+)/);
if (typeof value === 'undefined' || line.trim().startsWith('#')) {
return line;
}
// Skip lines with quotes and numbers already
// Todo: this could be one regex
const wrappedValue =
value.includes(' ') && !value.includes('"') && !value.includes('\'') && !/\d/.test(value)
? `"${value}"`
: value;
return `${key}=${wrappedValue}`;
});
const updatedContent = fixedLines.join('\n');
fs.writeFileSync(filePath, updatedContent);
}
/**
* Generate Secure Random Strings
*
* @param {Number} length The length of the random string
* @returns
*/
generateSecureRandomString(length = 32) {
return crypto.randomBytes(length).toString('hex');
}
/**
* Get all the environment variables
*/
all() {
return process.env;
}
/**
* Get an environment variable
*
* @param {String} variable
* @returns
*/
get(variable) {
return process.env[variable];
}
/**
* Get the current environment name
*
* @returns {String}
*/
currentEnvironment() {
return this.get('NODE_ENV');
}
/**
* Are we running in development?
*
* @returns {Boolean}
*/
isDevelopment() {
return this.currentEnvironment() === 'development';
}
/**
* Are we running tests?
*
* @returns {Boolean}
*/
isTest() {
return this.currentEnvironment() === 'test';
}
/**
* Are we running in production?
*
* @returns {Boolean}
*/
isProduction() {
return this.currentEnvironment() === 'production';
}
/**
* Are we running in CI?
*
* @returns {Boolean}
*/
isCI() {
return this.currentEnvironment() === 'ci';
}
}
const env = new Env();
module.exports = env;
|