Skip to content

Instantly share code, notes, and snippets.

@mrsimonemms
Created August 16, 2016 11:07
Show Gist options
  • Save mrsimonemms/ed918f56d2b4eb9dfbd39ab75a39e86f to your computer and use it in GitHub Desktop.
Save mrsimonemms/ed918f56d2b4eb9dfbd39ab75a39e86f to your computer and use it in GitHub Desktop.
Envvar replace
/**
* Replace Env Vars
*
* Looks for a matching environment variable and
* puts it into the object
*/
"use strict";
/* Node modules */
/* Third-party modules */
import * as _ from "lodash";
/* Files */
import {coerce} from "./coerce"; // This coerces strings into diff datatypes, eg String("2") becomes Number(2)
export function replaceEnvVars (obj: any) {
_.each(obj, (envVar, key) => {
if (_.isPlainObject(envVar)) {
replaceEnvVars(envVar);
} else {
/* Check if envvar passed in - begins "$" recursive */
while (/^\$/.test(process.env[envVar])) {
envVar = process.env[envVar]
.replace(/^\$/, "");
}
if (_.has(process.env, envVar)) {
/* Replace the value with the envVar */
let tmp = process.env[envVar];
obj[key] = coerce(tmp);
} else {
/* No known envvar - delete it */
delete obj[key];
}
}
});
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment