Skip to content

Commit

Permalink
Merge pull request #31 from Platane/master
Browse files Browse the repository at this point in the history
Gracefully fallback when the .env file is not found
  • Loading branch information
brysgo committed May 3, 2019
2 parents 46e555f + 9811cd1 commit 03fb7b9
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/index.js
Expand Up @@ -5,6 +5,28 @@ var pathResolve = require('path').resolve;

var dotenvContent;

function loadDotenvContent (opts){
var dotenvPath = pathResolve(process.cwd(), '.env')
var encoding = 'utf8'
var debug = false
if (opts.path != null) {
dotenvPath = opts.path
}
if (opts.encoding != null) {
encoding = opts.encoding
}
if (opts.debug != null) {
debug = true
}
var fileContent;
try { fileContent = fs.readFileSync(dotenvPath, {encoding:encoding}) } catch(e) {};
dotenvContent = fileContent ? require('dotenv').parse(fileContent, {debug:debug}) : {};
var dotenvExpand;
try { dotenvExpand = require('dotenv-expand'); } catch(e) {}
if (dotenvExpand)
dotenvContent = dotenvExpand({parsed:dotenvContent, ignoreProcessEnv:true}).parsed;
}

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

Expand Down Expand Up @@ -34,23 +56,7 @@ module.exports = function (options) {
if(t.isAssignmentExpression(path.parent) && path.parent.left == path.node) return;
if (path.get("object").matchesPattern("process.env")) {
if (!dotenvContent) {
var dotenvPath = pathResolve(process.cwd(), '.env')
var encoding = 'utf8'
var debug = false
if (state.opts.path != null) {
dotenvPath = state.opts.path
}
if (state.opts.encoding != null) {
encoding = state.opts.encoding
}
if (state.opts.debug != null) {
debug = true
}
dotenvContent = require('dotenv').parse(fs.readFileSync(dotenvPath, {encoding:encoding}), {debug:debug});
var dotenvExpand;
try { dotenvExpand = require('dotenv-expand'); } catch(e) {}
if (dotenvExpand)
dotenvContent = dotenvExpand({parsed:dotenvContent, ignoreProcessEnv:true}).parsed;
loadDotenvContent(state.opts);
}
var key = path.toComputedKey();
if (t.isStringLiteral(key)) {
Expand Down

0 comments on commit 03fb7b9

Please sign in to comment.