Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2503fbb

Browse files
committed
feat(Env): add support to load .env from different location
According to #116, add support for .env from different location via process.env.ENV_PATH #116
1 parent 4beb8a2 commit 2503fbb

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"pem": "^1.8.1",
3131
"standard": "^6.0.5",
3232
"supertest": "^1.1.0",
33+
"test-console": "^1.0.0",
3334
"zombie": "^4.2.1"
3435
},
3536
"peerDependencies": {

src/Env/index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,29 @@ const util = require('../../lib/util')
2121
class Env {
2222

2323
constructor (Helpers) {
24-
dotenv.load({path: path.join(Helpers.basePath(), '.env')})
24+
const envLocation = this.envPath()
25+
const options = {
26+
path: path.join(Helpers.basePath(), envLocation),
27+
silent: process.env.ENV_SILENT || false,
28+
encoding: process.env.ENV_ENCODING || 'utf8'
29+
}
30+
dotenv.load(options)
31+
}
32+
33+
/**
34+
* returns envPath by checking the environment variables
35+
*
36+
* @method envPath
37+
*
38+
* @return {String}
39+
*
40+
* @public
41+
*/
42+
envPath () {
43+
if (!process.env.ENV_PATH || process.env.ENV_PATH.length === 0) {
44+
return '.env'
45+
}
46+
return process.env.ENV_PATH
2547
}
2648

2749
/**

test/unit/env.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
const Env = require('../../src/Env')
10+
const stderr = require("test-console").stderr
1011
const chai = require('chai')
1112
const expect = chai.expect
1213

@@ -21,6 +22,15 @@ describe('Env', function() {
2122
new Env(Helpers)
2223
})
2324

25+
it('should load .env file from the location defined as ENV_PATH flag', function () {
26+
const inspect = stderr.inspect()
27+
process.env.ENV_PATH = '/users/.env'
28+
new Env(Helpers)
29+
inspect.restore()
30+
expect(inspect.output[0]).to.match(/\/users\/\.env/)
31+
process.env.ENV_PATH = ''
32+
})
33+
2434
it('should get values defined in .env file', function () {
2535
const env = new Env(Helpers)
2636
expect(env.get('APP_PORT')).to.equal('3000')

0 commit comments

Comments
 (0)