@thinkmill/devops-env-vars
Advanced tools
Comparing version 0.0.2 to 1.0.0
103
index.js
@@ -1,18 +0,1 @@ | ||
/* | ||
USAGE: | ||
const envVars = require('@thinkmill/devops-env-vars'); | ||
const c = envVars.collectEnvVars(process.env, ['live', 'staging', 'development']); | ||
const config = envVars.mergeConfig(c, { | ||
// In development we can default the NODE_ENV but production envs should set it themselves | ||
NODE_ENV: { required: !c.IN_DEVELOPMENT, default: 'development' }, | ||
// .. | ||
}); | ||
*/ | ||
'use strict'; | ||
@@ -22,7 +5,4 @@ | ||
const debugLib = require('debug'); | ||
const dotenv = require('dotenv'); | ||
const fs = require('fs'); | ||
const Netmask = require('netmask').Netmask; | ||
const os = require('os'); | ||
const path = require('path'); | ||
@@ -36,21 +16,13 @@ | ||
'10.119.0.0/16': 'testing', | ||
// Ireland (eu-west-1) | ||
'10.130.0.0/16': 'live', | ||
'10.132.0.0/16': 'testing', | ||
'10.131.0.0/16': 'staging', | ||
'10.131.0.0/16': 'staging', | ||
}; | ||
// The different environments we support | ||
const SUPPORTED_ENVS = ['live', 'staging', 'testing', 'development']; | ||
// Clean and default an array of environments that might be supported | ||
function cleanSupportedEnvs (envs) { | ||
const debug = debugLib('@thinkmill/devops-env-vars:cleanSupportedEnvs'); | ||
const valid = ['live', 'staging', 'testing', 'development']; | ||
if (!envs || Array.isArray(envs)) return valid; | ||
const clean = envs.filter(env => valid.includes(env)); | ||
debug(`Supported environments cleaned to: ${chalk.cyan(clean.join(', '))}`) | ||
return clean; | ||
} | ||
// Function to get the local ip address of the current server | ||
@@ -61,3 +33,3 @@ // Only works for IPv4; assumes a single external IP per server | ||
const ifaces = os.networkInterfaces(); | ||
var values = Object.keys(ifaces).map(function (name) { | ||
@@ -70,3 +42,3 @@ return ifaces[name]; | ||
}); | ||
const serverIp = values.length ? values[0].address : '0.0.0.0'; | ||
@@ -79,8 +51,8 @@ debug(`Server IP identified as ${chalk.cyan(serverIp)}`); | ||
// Figures out which APP_ENV to use, based on the value supplied, the supported envs and the servers IP address | ||
function determineAppEnv (_processAppEnv, supportedEnvs) { | ||
function determineAppEnv (_processAppEnv) { | ||
const debug = debugLib('@thinkmill/devops-env-vars:determineAppEnv'); | ||
// Validate the supplied process APP_ENV | ||
const processAppEnv = (supportedEnvs.indexOf(_processAppEnv) > -1) ? _processAppEnv : undefined; | ||
const processAppEnv = (SUPPORTED_ENVS.indexOf(_processAppEnv) > -1) ? _processAppEnv : undefined; | ||
// User supplied is give precedence | ||
@@ -91,10 +63,10 @@ if (processAppEnv) { | ||
} | ||
// If the servers ip exists in one of the defined subnets, return that environment | ||
const serverIp = getServerIp(); | ||
var envRtn = 'development'; | ||
Object.keys(VPC_IP_RANGES).forEach(cidr => { | ||
const cidrEnv = VPC_IP_RANGES[cidr]; | ||
if (new Netmask(cidr).contains(serverIp) && supportedEnvs.includes(cidrEnv)) { | ||
if (new Netmask(cidr).contains(serverIp) && SUPPORTED_ENVS.includes(cidrEnv)) { | ||
debug(`APP_ENV determined from server IP as ${chalk.cyan(cidrEnv)} (${chalk.green(serverIp)} is within ${chalk.green(cidr)})`); | ||
@@ -104,3 +76,3 @@ envRtn = cidrEnv; | ||
}); | ||
// Default to development | ||
@@ -113,5 +85,7 @@ debug(`APP_ENV returning as ${chalk.cyan(envRtn)}`); | ||
// Build a set of flag constants; one for each supported environment | ||
function buildAppFlags (appEnv, supportedEnvs) { | ||
function buildAppFlags (appEnv) { | ||
var flags = {}; | ||
supportedEnvs.forEach(env => flags[`IN_${env.toUpperCase()}`] = (env === appEnv)); | ||
SUPPORTED_ENVS.forEach(env => { | ||
flags[`IN_${env.toUpperCase()}`] = (env === appEnv); | ||
}); | ||
return flags; | ||
@@ -121,36 +95,13 @@ } | ||
// Attempts to read a local .env file for the current APP_ENV | ||
// These usually hold security sensitive values specific to the environment (ie. that we don't want in git) | ||
// function readFileVars (envFilePath) { | ||
// const debug = debugLib('@thinkmill/devops-env-vars:readFileVars'); | ||
// debug(`Attempting to read local .env from ${chalk.cyan(envFilePath)}`); | ||
// // Attempt to read the file | ||
// try { | ||
// const file = fs.readFileSync(envFilePath); | ||
// // We use the parse() method here; if parses the .env format but doens't inject the result into process.env | ||
// const cont = dotenv.parse(file); | ||
// debug(`Success - Local .env vars loaded from ${chalk.cyan(envFilePath)}`); | ||
// return cont; | ||
// } | ||
// catch (e) { | ||
// debug(`Failure - Local .env vars not found at ${chalk.cyan(envFilePath)}. Error was: `, e.message); | ||
// return {}; | ||
// } | ||
// } | ||
// Handles the logic of merging, validating and defaulting the config vars | ||
function mergeConfig (appEnv, appFlags, processEnv, rules) { | ||
const debug = debugLib('@thinkmill/devops-env-vars:mergeConfig') | ||
const debug = debugLib('@thinkmill/devops-env-vars:mergeConfig'); | ||
const ruleKeys = Object.keys(rules); | ||
var config = {}; | ||
for (let i = 0; i < ruleKeys.length; i++) { | ||
const varName = ruleKeys[i]; | ||
const varRule = rules[varName]; | ||
if (processEnv.hasOwnProperty(varName)) { | ||
@@ -160,3 +111,3 @@ debug(`${chalk.cyan(varName)} setting from processEnv: '${chalk.yellow(processEnv[varName])}'`); | ||
} | ||
if (varRule.required && !config.hasOwnProperty(varName)) { | ||
@@ -166,3 +117,3 @@ debug(`${chalk.cyan(varName)} not set and required; throwing error...`); | ||
} | ||
if (varRule.hasOwnProperty('default') && !config.hasOwnProperty(varName)) { | ||
@@ -173,6 +124,6 @@ debug(`${chalk.cyan(varName)} not set; defaulting to ${chalk.red(varRule.default)}`); | ||
} | ||
// Add the APP_ENV and flags | ||
Object.assign(config, appFlags, { APP_ENV: appEnv }); | ||
debug(`Finald config:`, config); | ||
@@ -184,2 +135,2 @@ return config; | ||
// Export the ip and the subnet check function | ||
module.exports = { cleanSupportedEnvs, getServerIp, determineAppEnv, buildAppFlags, mergeConfig }; | ||
module.exports = { getServerIp, determineAppEnv, buildAppFlags, mergeConfig }; |
{ | ||
"name": "@thinkmill/devops-env-vars", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"description": "Helper functions that encapsulate our treatment of environment vars for KeystoneJS apps", | ||
@@ -21,5 +21,7 @@ "main": "index.js", | ||
"debug": "^2.2.0", | ||
"dotenv": "^2.0.0", | ||
"netmask": "^1.0.6" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^3.2.2" | ||
} | ||
} |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
16505
3
6
212
1
1
194
0
1
- Removeddotenv@^2.0.0
- Removeddotenv@2.0.0(transitive)