Skip to content

Commit

Permalink
feat: allows to specify how the env var from system should be used
Browse files Browse the repository at this point in the history
  • Loading branch information
Platane committed Mar 24, 2019
1 parent f2d5602 commit 395ed0a
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
"use strict";

var dotenv;
var fs = require('fs');

var dotenvContent;

function getValue(dotenvContent, systemContent, opts, name) {
if (opts.env && name in opts.env) return opts.env[name];

switch (opts.systemVar) {
case "overwrite":
if (name in dotenvContent && name in systemContent)
return systemContent[name];
if (name in dotenvContent) return dotenvContent[name];
return;
case "disable":
if (name in dotenvContent) return dotenvContent[name];
return;
case "all":
default:
if (name in systemContent) return systemContent[name];
if (name in dotenvContent) return dotenvContent[name];
return;
}
}

module.exports = function (options) {
var t = options.types;
Expand All @@ -10,17 +32,18 @@ module.exports = function (options) {
MemberExpression: function MemberExpression(path, state) {
if(t.isAssignmentExpression(path.parent) && path.parent.left == path.node) return;
if (path.get("object").matchesPattern("process.env")) {
if (!dotenv) {
dotenv = require('dotenv').config(state.opts);
if (!dotenvContent) {
var envFilePath = state.opts.path || './.env'
dotenvContent = require('dotenv').parse(fs.readFileSync(envFilePath));
var dotenvExpand;
try { dotenvExpand = require('dotenv-expand'); } catch(e) {}
if (dotenvExpand)
dotenvExpand(dotenv);
dotenvContent = dotenvExpand({parsed:dotenvContent, ignoreProcessEnv:true}).parsed;
}
var key = path.toComputedKey();
if (t.isStringLiteral(key)) {
var name = key.value;
var value = state.opts.env && name in state.opts.env ? state.opts.env[name] : process.env[name];
var value = getValue(dotenvContent, process.env, state.opts, name)
var me = t.memberExpression;
var i = t.identifier;
var le = t.logicalExpression;
Expand Down

0 comments on commit 395ed0a

Please sign in to comment.