Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

override values from .env with values form environment variables #63

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions babel-plugin-dotenv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var fs = require('fs');
var sysPath = require('path');
var process = require('process');

var environmentCopy = Object.assign({}, process.env)

module.exports = function (data) {
var t = data.types;

Expand Down Expand Up @@ -30,13 +32,19 @@ module.exports = function (data) {
}
var importedId = specifier.imported.name
var localId = specifier.local.name;
if(!(config.hasOwnProperty(importedId))) {
throw path.get('specifiers')[idx].buildCodeFrameError('Try to import dotenv variable "' + importedId + '" which is not defined in any ' + configFile + ' files.')
if(!(config.hasOwnProperty(importedId)) && !environmentCopy.hasOwnProperty(importedId)) {
throw path.get('specifiers')[idx].buildCodeFrameError(
'Try to import dotenv variable "'
+ importedId
+ '" which is not defined in any '
+ configFile
+ ' files or as an environment variable.'
)
}

var binding = path.scope.getBinding(localId);
binding.referencePaths.forEach(function(refPath){
refPath.replaceWith(t.valueToNode(config[importedId]))
refPath.replaceWith(t.valueToNode(environmentCopy[importedId] || config[importedId]))
});
})

Expand Down
10 changes: 10 additions & 0 deletions babel-plugin-dotenv/test/fixtures/override-value/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
"babel-plugin-transform-es2015-modules-commonjs",
["../../../", {
"replacedModuleName": "babel-dotenv",
"configDir": "test/fixtures/default"
}]
]
}

2 changes: 2 additions & 0 deletions babel-plugin-dotenv/test/fixtures/override-value/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FOO_ENV=abc123

2 changes: 2 additions & 0 deletions babel-plugin-dotenv/test/fixtures/override-value/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { FOO_ENV } from 'babel-dotenv';
console.log(FOO_ENV);
17 changes: 15 additions & 2 deletions babel-plugin-dotenv/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ var createPluginsWithConfigDir = function(configDir) {
}

describe('myself in some tests', function() {
it('should throw if variable not exist', function() {
before(function() {
process.env.FOO_ENV = 'foo'
})

after(function() {
delete process.env.FOO_ENV
})

it('should throw if variable not exist and no environemnt variable is defined', function() {
expect(function(){
babel.transformFileSync('test/fixtures/variable-not-exist/source.js')
}).to.throwException(function (e) {
expect(e.message).to.contain("Try to import dotenv variable \"foo\" which is not defined in any .env files.");
expect(e.message).to.contain("Try to import dotenv variable \"foo\" which is not defined in any .env files or as an environment variable.");
});
});

Expand All @@ -27,6 +35,11 @@ describe('myself in some tests', function() {
});
});

it('should override values defined in .env with environment variable', function() {
var result = babel.transformFileSync('test/fixtures/override-value/source.js')
expect(result.code).to.be('\'use strict\';\n\nconsole.log(\'foo\');')
})

it('should load default env from .env', function(){
var result = babel.transformFileSync('test/fixtures/default/source.js')
expect(result.code).to.be('\'use strict\';\n\nconsole.log(\'abc123\');\nconsole.log(\'username\');')
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var path = require('path');

module.exports = () => ({
plugins: [
[require('babel-plugin-dotenv'), {
[require('./babel-plugin-dotenv'), {
replacedModuleName: 'react-native-dotenv',
configDir: path.resolve(__dirname, "../../")
}],
Expand Down