├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── MIT-LICENSE ├── Makefile ├── README.md ├── TODO ├── examples ├── .env ├── .env2 └── basic.js ├── index.js ├── lib └── index.js ├── package.json └── test ├── fixtures ├── .env.0 ├── .env.1 ├── .env.2 ├── .env.3 ├── .env.4 ├── .env.5 ├── .env.exports.0 ├── .env.exports.1 ├── .env.exports.2 ├── .env.exports.3 └── .env.exports.4 ├── helper.js ├── index.js └── mocha.opts /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | 7 | indent_style = space 8 | indent_size = 4 9 | 10 | end_of_line = lf 11 | charset = utf-8 12 | trim_trailing_whitespace = true 13 | insert_final_newline = true 14 | 15 | [*.md] 16 | trim_trailing_whitespace = false 17 | 18 | [Makefile] 19 | indent_style = tab 20 | indent_size = 4 21 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .coverage_data 3 | .*~ 4 | *~ 5 | tmp/* 6 | log/* 7 | node_modules 8 | npm-debug.log 9 | .idea/ 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "4.1" 4 | - "4.0" 5 | - "0.12" 6 | - "0.11" 7 | - "0.10" 8 | script: 9 | - "make test-ci" 10 | notifications: 11 | email: false 12 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Jonas Grimfelt. https://github.com/grimen 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | 3 | MODULE_BIN := ./node_modules/.bin 4 | 5 | all: test-watch 6 | 7 | example: 8 | node ./examples/basic.js 9 | 10 | test: 11 | @ $(MODULE_BIN)/mocha ./test/index.js 12 | 13 | test-watch: 14 | @ $(MODULE_BIN)/mocha ./test/index.js --watch 15 | 16 | test-ci: 17 | @ $(MODULE_BIN)/mocha ./test/index.js --reporter dot --ignore-leaks 18 | 19 | install: 20 | npm install 21 | 22 | release: 23 | npm publish 24 | 25 | .PHONY: example test test-ci install 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NODE-ENV-FILE [![Build Status](https://secure.travis-ci.org/grimen/node-env-file.svg)](http://travis-ci.org/grimen/node-env-file) 2 | 3 | Parse and load environment files (containing ENV variable exports) into Node.js environment, i.e. `process.env`. 4 | 5 | 6 | ## Example 7 | 8 | **`.env`** 9 | 10 | ```bash 11 | # some env variables 12 | 13 | FOO=foo1 14 | BAR=bar1 15 | BAZ=1 16 | QUX= 17 | # QUUX= 18 | ``` 19 | 20 | **`.env2`** 21 | 22 | ```bash 23 | # some env variables using exports syntax 24 | 25 | exports FOO=foo2 26 | exports BAR=bar2 27 | exports BAZ=2 28 | exports QUX= 29 | # exports QUUX= 30 | 31 | ``` 32 | 33 | **`index.js`** 34 | 35 | ```javascript 36 | var assert = require('assert'); 37 | var env = require('node-env-file'); 38 | 39 | process.env.FOO = "defaultfoo"; 40 | 41 | // Load any undefined ENV variables from a specified file. 42 | env(__dirname + '/.env'); 43 | assert.equal(process.env.FOO, "defaultfoo"); 44 | assert.equal(process.env.BAR, "bar1"); 45 | assert.equal(process.env.BAZ, "1"); 46 | assert.equal(process.env.QUX, ""); 47 | assert.equal(process.env.QUUX, undefined); 48 | 49 | // Load another ENV file - and overwrite any defined ENV variables. 50 | env(__dirname + '/.env2', {overwrite: true}); 51 | assert.equal(process.env.FOO, "foo2"); 52 | assert.equal(process.env.BAR, "bar2"); 53 | assert.equal(process.env.BAZ, "2"); 54 | assert.equal(process.env.QUX, ""); 55 | assert.equal(process.env.QUUX, undefined); 56 | 57 | // Load any undefined ENV variables from a specified file, but don't crash if the file doesn't exist 58 | // Usefull for testing env vars in development, but using "real" env vars in production 59 | envfile(__dirname + '/.env', {raise: false}); 60 | 61 | ``` 62 | 63 | 64 | ## API 65 | 66 | * `(filepath)` 67 | 68 | ```javascript 69 | env('./path/to/.env'); 70 | ``` 71 | 72 | * `(filepath, options)` 73 | 74 | ```javascript 75 | env('./path/to/.env', {verbose: true, overwrite: true, raise: false, logger: console}); 76 | ``` 77 | 78 | 79 | ## Installation 80 | 81 | ```shell 82 | $ npm install node-env-file 83 | ``` 84 | 85 | 86 | ## Test 87 | 88 | **Local tests:** 89 | 90 | ```shell 91 | $ make test 92 | ``` 93 | 94 | 95 | ## Examples 96 | 97 | **Local examples:** 98 | 99 | ```shell 100 | $ make example 101 | ``` 102 | 103 | 104 | ## Related Libraries 105 | 106 | * **[node-env-flag](http://github.com/grimen/node-env-flag)** 107 | 108 | 109 | ## License 110 | 111 | Released under the MIT license. 112 | 113 | Copyright (c) [Jonas Grimfelt](http://github.com/grimen) 114 | 115 | [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/grimen/node-env-file/trend.png)](https://bitdeli.com/free "Bitdeli Badge") 116 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | 2 | :: NEXT 3 | 4 | - [FEATURE]: Optionally load environment async. 5 | 6 | Examples: 7 | 8 | env.loadFile(path, options, callback) 9 | env.loadFileSync(path, options) 10 | 11 | env.sync(path, options) 12 | env.async(path, options, callback) 13 | 14 | - [FEATURE]: Pretty and colorized JSON. 15 | -------------------------------------------------------------------------------- /examples/.env: -------------------------------------------------------------------------------- 1 | # some env variables 2 | 3 | FOO=foo1 4 | BAR=bar1 5 | BAZ=1 6 | QUX= 7 | 8 | # IGNORE 9 | # IGNORE= 10 | # IGNORE=1 11 | # IGNORE="1" 12 | -------------------------------------------------------------------------------- /examples/.env2: -------------------------------------------------------------------------------- 1 | # some env variables using exports syntax 2 | 3 | exports FOO=foo2 4 | exports BAR=bar2 5 | exports BAZ=2 6 | exports QUX= 7 | 8 | # exports IGNORE 9 | # exports IGNORE= 10 | # exports IGNORE=1 11 | # exports IGNORE="1" 12 | -------------------------------------------------------------------------------- /examples/basic.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var assert = require('assert') 4 | var env = require('../') 5 | 6 | process.env.FOO = "defaultfoo" 7 | 8 | // Load any undefined ENV variables form a specified file. 9 | env(__dirname + '/.env') 10 | assert.equal(process.env.FOO, "defaultfoo") 11 | assert.equal(process.env.BAR, "bar1") 12 | assert.equal(process.env.BAZ, "1") 13 | assert.equal(process.env.QUX, "") 14 | 15 | // Load another ENV file - and overwrite any defined ENV variables. 16 | env(__dirname + '/.env2', {overwrite: true}) 17 | assert.equal(process.env.FOO, "foo2") 18 | assert.equal(process.env.BAR, "bar2") 19 | assert.equal(process.env.BAZ, "2") 20 | assert.equal(process.env.QUX, "") 21 | 22 | console.log('=============') 23 | console.log(' ENV') 24 | console.log('----------') 25 | console.log(env.data) 26 | console.log('----------') 27 | console.log('DONE') 28 | 29 | process.exit() 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | // ----------------------- 4 | // Export 5 | // -------------------- 6 | 7 | module.exports = require('./lib') 8 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var fs = require('fs') 4 | var path = require('path') 5 | 6 | //---------------------- 7 | // Export 8 | //------------------- 9 | 10 | module.exports = function (env_file, options) { 11 | options = options || {} 12 | 13 | if (typeof options.verbose === 'undefined') { 14 | options.verbose = module.exports.verbose 15 | } 16 | 17 | if (typeof options.overwrite === 'undefined') { 18 | options.overwrite = module.exports.overwrite 19 | } 20 | 21 | if (typeof options.raise === 'undefined') { 22 | options.raise = module.exports.raise 23 | } 24 | 25 | module.exports.logger = options.logger || module.exports.logger 26 | 27 | if (typeof env_file !== 'string') { 28 | if (options.raise) { 29 | throw new TypeError("Environment file argument is not a valid `String`: " + env_file) 30 | } else { 31 | if (options.verbose && module.exports.logger) { 32 | module.exports.logger.error('[ENV]: ERROR Environment file argument is not a valid `String`:', process.env.ENV_FILE) 33 | } 34 | return {} 35 | } 36 | } 37 | 38 | try { 39 | env_file = process.env.ENV_FILE = path.resolve(env_file) || process.env.ENV_FILE 40 | } catch (err) { 41 | if (options.raise) { 42 | throw new TypeError("Environment file path could not be resolved: " + err) 43 | } else { 44 | if (options.verbose && module.exports.logger) { 45 | module.exports.logger.error('[ENV]: ERROR Environment file path could not be resolved:', process.env.ENV_FILE) 46 | } 47 | return {} 48 | } 49 | } 50 | 51 | module.exports.data = module.exports.data || {} 52 | module.exports.data[env_file] = {} 53 | 54 | if (options.verbose && module.exports.logger) { 55 | module.exports.logger.info('[ENV]: Loading environment:', env_file) 56 | } 57 | 58 | if (fs.existsSync(env_file)) { 59 | var lines 60 | 61 | try { 62 | lines = (fs.readFileSync(env_file, 'utf8') || '') 63 | .split(/\r?\n|\r/) 64 | .filter(function (line) { 65 | return /\s*=\s*/i.test(line) 66 | }) 67 | .map(function (line) { 68 | return line.replace('exports ', '') 69 | }) 70 | 71 | } catch (err) { 72 | if (options.raise) { 73 | throw new TypeError("Environment file could not be read: " + err) 74 | } else { 75 | if (options.verbose && module.exports.logger) { 76 | module.exports.logger.error('[ENV]: ERROR Environment file could not be read:', env_file) 77 | } 78 | return {} 79 | } 80 | } 81 | 82 | module.exports.lines = module.exports 83 | module.exports.lines.comments = [] 84 | module.exports.lines.variables = [] 85 | 86 | var is_comment 87 | 88 | lines.forEach(function(line) { 89 | is_comment = /^\s*\#/i.test(line) // ignore comment lines (starting with #). 90 | 91 | if (is_comment) { 92 | module.exports.lines.comments.push(line) 93 | 94 | if (options.verbose && module.exports.logger) { 95 | module.exports.logger.info('[ENV]: Ignored line:', line) 96 | } 97 | 98 | } else { 99 | module.exports.lines.variables.push(line) 100 | 101 | var key_value = line.match(/^([^=]+)\s*=\s*(.*)$/) 102 | 103 | var env_key = key_value[1] 104 | 105 | // remove ' and " characters if right side of = is quoted 106 | var env_value = key_value[2].match(/^(['"]?)([^\n]*)\1$/m)[2] 107 | 108 | // overwrite already defined `process.env.*` values? 109 | if (!!options.overwrite) { 110 | module.exports.data[env_file][env_key] = env_value 111 | 112 | if (options.verbose && module.exports.logger && module.exports.data[env_file][env_key] !== env_value) { 113 | module.exports.logger.info('[ENV]: Overwritten ', module.exports.data[env_file][env_key], ' => ', env_value) 114 | } 115 | 116 | } else { 117 | module.exports.data[env_file][env_key] = process.env[env_key] || env_value 118 | } 119 | 120 | process.env[env_key] = module.exports.data[env_file][env_key] 121 | 122 | if (options.verbose && module.exports.logger) { 123 | module.exports.logger.info('[ENV]:', module.exports.data[env_file]) 124 | } 125 | } 126 | }) 127 | } 128 | else { 129 | if (options.raise) { 130 | throw new TypeError("Environment file doesn't exist: " + env_file) 131 | 132 | } else { 133 | if (options.verbose && module.exports.logger) { 134 | module.exports.logger.error('[ENV]: ERROR Environment file path could not be resolved:', env_file) 135 | } 136 | return {} 137 | } 138 | } 139 | 140 | return module.exports.data[env_file] 141 | } 142 | 143 | module.exports.log = false 144 | module.exports.logger = console 145 | module.exports.overwrite = false 146 | module.exports.raise = true 147 | module.exports.data = {} 148 | 149 | module.exports.sync = module.exports 150 | 151 | // TODO: module.exports.async = module.exports.async 152 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-env-file", 3 | "description": "Parse and load environment files (containing ENV variable exports) into Node.js environment, i.e. `process.env`.", 4 | "keywords": [ 5 | "process", 6 | "env", 7 | "file", 8 | "files", 9 | ".env", 10 | "ENV", 11 | "process.env", 12 | "parse", 13 | "load", 14 | "export", 15 | "exports" 16 | ], 17 | "version": "0.1.8", 18 | "homepage": "https://github.com/grimen/node-env-file", 19 | "repository": { 20 | "type": "git", 21 | "url": "https://github.com/grimen/node-env-file.git" 22 | }, 23 | "bugs": { 24 | "url": "https://github.com/grimen/node-env-file/issues" 25 | }, 26 | "licenses": [ 27 | { 28 | "type": "MIT", 29 | "url": "https://github.com/grimen/node-env-file/blob/MIT-LICENSE" 30 | } 31 | ], 32 | "author": "Jonas Grimfelt ", 33 | "maintainers": [ 34 | { 35 | "name": "Jonas Grimfelt", 36 | "email": "grimen@gmail.com", 37 | "web": "https://github.com/grimen" 38 | } 39 | ], 40 | "contributors": [], 41 | "scripts": { 42 | "test": "make test", 43 | "test-ci": "make test-ci" 44 | }, 45 | "bin": {}, 46 | "engines": { 47 | "node": ">= 0.8.0" 48 | }, 49 | "dependencies": { 50 | }, 51 | "devDependencies": { 52 | "mocha": "latest", 53 | "chai": "latest", 54 | "glob": "latest", 55 | "temp": "latest" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /test/fixtures/.env.0: -------------------------------------------------------------------------------- 1 | # ignore this and blank lines 2 | 3 | # IGNORE 4 | # IGNORE= 5 | # IGNORE=1 6 | # IGNORE="1" 7 | -------------------------------------------------------------------------------- /test/fixtures/.env.1: -------------------------------------------------------------------------------- 1 | # ignore this and blank lines 2 | 3 | FOO=1 4 | 5 | # IGNORE 6 | # IGNORE= 7 | # IGNORE=1 8 | # IGNORE="1" 9 | -------------------------------------------------------------------------------- /test/fixtures/.env.2: -------------------------------------------------------------------------------- 1 | # ignore this and blank lines 2 | 3 | FOO=1 4 | BAR=bar 5 | QUX= 6 | 7 | # IGNORE 8 | # IGNORE= 9 | # IGNORE=1 10 | # IGNORE="1" 11 | -------------------------------------------------------------------------------- /test/fixtures/.env.3: -------------------------------------------------------------------------------- 1 | # ignore this and blank lines 2 | 3 | FOO=http://foo.com?bar=baz 4 | 5 | # IGNORE 6 | # IGNORE= 7 | # IGNORE=1 8 | # IGNORE="1" 9 | -------------------------------------------------------------------------------- /test/fixtures/.env.4: -------------------------------------------------------------------------------- 1 | # ignore this and blank lines 2 | 3 | FOO=http://foo.com#hash?bar=baz 4 | 5 | # IGNORE 6 | # IGNORE= 7 | # IGNORE=1 8 | # IGNORE="1" 9 | -------------------------------------------------------------------------------- /test/fixtures/.env.5: -------------------------------------------------------------------------------- 1 | FOO= 2 | BAR=1 3 | BAZ= 4 | QUX=sample 5 | NORF= 6 | 7 | # IGNORE 8 | # IGNORE= 9 | # IGNORE=1 10 | # IGNORE="1" 11 | -------------------------------------------------------------------------------- /test/fixtures/.env.exports.0: -------------------------------------------------------------------------------- 1 | # ignore this and blank lines 2 | 3 | # exports IGNORE 4 | # exports IGNORE= 5 | # exports IGNORE=1 6 | # exports IGNORE="1" 7 | -------------------------------------------------------------------------------- /test/fixtures/.env.exports.1: -------------------------------------------------------------------------------- 1 | # ignore this and blank lines 2 | 3 | exports FOO=1 4 | 5 | # exports IGNORE 6 | # exports IGNORE= 7 | # exports IGNORE=1 8 | # exports IGNORE="1" 9 | -------------------------------------------------------------------------------- /test/fixtures/.env.exports.2: -------------------------------------------------------------------------------- 1 | # ignore this and blank lines 2 | 3 | exports FOO=1 4 | exports BAR=bar 5 | exports QUX= 6 | 7 | # exports IGNORE 8 | # exports IGNORE= 9 | # exports IGNORE=1 10 | # exports IGNORE="1" 11 | -------------------------------------------------------------------------------- /test/fixtures/.env.exports.3: -------------------------------------------------------------------------------- 1 | # ignore this and blank lines 2 | 3 | exports FOO=http://foo.com?bar=baz 4 | 5 | # exports IGNORE 6 | # exports IGNORE= 7 | # exports IGNORE=1 8 | # exports IGNORE="1" 9 | -------------------------------------------------------------------------------- /test/fixtures/.env.exports.4: -------------------------------------------------------------------------------- 1 | # ignore this and blank lines 2 | 3 | exports FOO=http://foo.com#hash?bar=baz 4 | 5 | # exports IGNORE 6 | # exports IGNORE= 7 | # exports IGNORE=1 8 | # exports IGNORE="1" 9 | -------------------------------------------------------------------------------- /test/helper.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | process.env.NODE_ENV = 'test' 4 | 5 | module.exports.chai = require('chai') 6 | module.exports.chai.config.includeStack = true 7 | 8 | module.exports.assert = module.exports.chai.assert 9 | module.exports.expect = module.exports.chai.expect 10 | 11 | module.exports.debug = console.log 12 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var helper = require('./helper') 4 | var assert = helper.assert 5 | var expect = helper.expect 6 | var debug = helper.debug 7 | 8 | var temp = require('temp') 9 | var path = require('path') 10 | var fs = require('fs') 11 | var glob = require('glob') 12 | 13 | var env = require('../') 14 | 15 | var defaultTestPath = path.join(__dirname, 'fixtures') 16 | var windowsCRTestPath = temp.mkdirSync('node-env-file.windows-cr') 17 | 18 | function _fixture(filename, type) { 19 | var _fixturesPath; 20 | 21 | var types = ['windows-cr', 'default'] 22 | 23 | if (!type) { 24 | type = types[Math.floor(Math.random() * types.length)] 25 | } 26 | 27 | switch (type) { 28 | case 'windows-cr': 29 | _fixturesPath = windowsCRTestPath 30 | break 31 | default: 32 | _fixturesPath = defaultTestPath 33 | } 34 | return path.join(_fixturesPath, filename) 35 | } 36 | 37 | function _prepareFixtures() { 38 | // Prepare: Windows CR/newline tests 39 | glob.sync(path.join(defaultTestPath, '*'), {dot: true}) 40 | .forEach(function(fixtureFile) { 41 | var tmpEnvFile = path.join(windowsCRTestPath, path.basename(fixtureFile)) 42 | var fixtureData = fs.readFileSync(fixtureFile, 'utf-8') 43 | var fixtureDataWithCR = fixtureData.replace(/\n/gmi, "\r\n") 44 | 45 | // debug: console.log(tmpEnvFile) 46 | 47 | fs.writeFileSync(tmpEnvFile, fixtureDataWithCR, 'utf-8') 48 | }) 49 | } 50 | 51 | temp.track() 52 | 53 | // ----------------------- 54 | // Test 55 | // -------------------- 56 | 57 | module.exports = { 58 | 59 | before: function() { 60 | _prepareFixtures() 61 | 62 | expect(env).to.be.a('function') 63 | }, 64 | 65 | 'beforeEach': function() { 66 | delete process.env.FOO 67 | delete process.env.BAR 68 | delete process.env.BAZ 69 | delete process.env.QUX 70 | delete process.env.NORF 71 | delete process.env.IGNORE 72 | }, 73 | 74 | '()': function() { 75 | expect(function() { 76 | env() 77 | }).to.throw(TypeError) 78 | 79 | expect(function() { 80 | env(_fixture('.env.100'), {raise: false}) 81 | }).to.not.throw(Error) 82 | }, 83 | 84 | // non-existing 85 | 86 | '(, [])': { 87 | '("./fixtures/.env.100")': function () { 88 | expect(function() { 89 | env(_fixture('.env.100')) 90 | }).to.throw(Error) 91 | 92 | expect(process.env.FOO).to.be.equal(undefined) 93 | expect(process.env.BAR).to.be.equal(undefined) 94 | expect(process.env.BAZ).to.be.equal(undefined) 95 | expect(process.env.QUX).to.be.equal(undefined) 96 | expect(process.env.IGNORE).to.be.equal(undefined) 97 | 98 | process.env.FOO = 'foo2' 99 | 100 | expect(function() { 101 | env(_fixture('.env.100'), {}) 102 | }).to.throw(Error) 103 | 104 | expect(process.env.FOO).to.be.equal('foo2') 105 | expect(process.env.BAR).to.be.equal(undefined) 106 | expect(process.env.BAZ).to.be.equal(undefined) 107 | expect(process.env.QUX).to.be.equal(undefined) 108 | expect(process.env.IGNORE).to.be.equal(undefined) 109 | 110 | process.env.FOO = 'foo2' 111 | 112 | expect(function() { 113 | env(_fixture('.env.100'), {overwrite: true}) 114 | }).to.throw(Error) 115 | 116 | expect(process.env.FOO).to.be.equal('foo2') 117 | expect(process.env.BAR).to.be.equal(undefined) 118 | expect(process.env.BAZ).to.be.equal(undefined) 119 | expect(process.env.QUX).to.be.equal(undefined) 120 | expect(process.env.IGNORE).to.be.equal(undefined) 121 | 122 | expect(function() { 123 | env(_fixture('.env.100'), {raise: false}) 124 | }).to.not.throw(Error) 125 | } 126 | }, 127 | 128 | '(, [])': { 129 | '("./fixtures/.env.0")': function () { 130 | expect(function() { 131 | env(_fixture('.env.0')) 132 | }).to.not.throw(Error) 133 | 134 | expect(process.env.FOO).to.be.equal(undefined) 135 | expect(process.env.BAR).to.be.equal(undefined) 136 | expect(process.env.BAZ).to.be.equal(undefined) 137 | expect(process.env.QUX).to.be.equal(undefined) 138 | expect(process.env.IGNORE).to.be.equal(undefined) 139 | 140 | process.env.FOO = 'foo2' 141 | 142 | expect(function() { 143 | env(_fixture('.env.0'), {}) 144 | }).to.not.throw(Error) 145 | 146 | expect(process.env.FOO).to.be.equal('foo2') 147 | expect(process.env.BAR).to.be.equal(undefined) 148 | expect(process.env.BAZ).to.be.equal(undefined) 149 | expect(process.env.QUX).to.be.equal(undefined) 150 | expect(process.env.IGNORE).to.be.equal(undefined) 151 | 152 | process.env.FOO = 'foo2' 153 | 154 | expect(function() { 155 | env(_fixture('.env.0'), {overwrite: true}) 156 | }).to.not.throw(Error) 157 | 158 | expect(process.env.FOO).to.be.equal('foo2') 159 | expect(process.env.BAR).to.be.equal(undefined) 160 | expect(process.env.BAZ).to.be.equal(undefined) 161 | expect(process.env.QUX).to.be.equal(undefined) 162 | expect(process.env.IGNORE).to.be.equal(undefined) 163 | }, 164 | 165 | '("./fixtures/.env.1")': function () { 166 | expect(function() { 167 | env(_fixture('.env.1')) 168 | }).to.not.throw(Error) 169 | 170 | expect(process.env.FOO).to.be.equal('1') 171 | expect(process.env.BAR).to.be.equal(undefined) 172 | expect(process.env.BAZ).to.be.equal(undefined) 173 | expect(process.env.QUX).to.be.equal(undefined) 174 | expect(process.env.IGNORE).to.be.equal(undefined) 175 | 176 | process.env.FOO = 'foo2' 177 | 178 | expect(function() { 179 | env(_fixture('.env.1'), {}) 180 | }).to.not.throw(Error) 181 | 182 | expect(process.env.FOO).to.be.equal('foo2') 183 | expect(process.env.BAR).to.be.equal(undefined) 184 | expect(process.env.BAZ).to.be.equal(undefined) 185 | expect(process.env.QUX).to.be.equal(undefined) 186 | expect(process.env.IGNORE).to.be.equal(undefined) 187 | 188 | process.env.FOO = 'foo2' 189 | 190 | expect(function() { 191 | env(_fixture('.env.1'), {overwrite: true}) 192 | }).to.not.throw(Error) 193 | 194 | expect(process.env.FOO).to.be.equal('1') 195 | expect(process.env.BAR).to.be.equal(undefined) 196 | expect(process.env.BAZ).to.be.equal(undefined) 197 | expect(process.env.QUX).to.be.equal(undefined) 198 | expect(process.env.IGNORE).to.be.equal(undefined) 199 | }, 200 | 201 | '("./fixtures/.env.2")': function () { 202 | expect(function() { 203 | env(_fixture('.env.2')) 204 | }).to.not.throw(Error) 205 | 206 | expect(process.env.FOO).to.be.equal('1') 207 | expect(process.env.BAR).to.be.equal('bar') 208 | expect(process.env.BAZ).to.be.equal(undefined) 209 | expect(process.env.QUX).to.be.equal('') 210 | expect(process.env.IGNORE).to.be.equal(undefined) 211 | 212 | process.env.FOO = 'foo2' 213 | 214 | expect(function() { 215 | env(_fixture('.env.2'), {}) 216 | }).to.not.throw(Error) 217 | 218 | expect(process.env.FOO).to.be.equal('foo2') 219 | expect(process.env.BAR).to.be.equal('bar') 220 | expect(process.env.BAZ).to.be.equal(undefined) 221 | expect(process.env.QUX).to.be.equal('') 222 | expect(process.env.IGNORE).to.be.equal(undefined) 223 | 224 | process.env.FOO = 'foo2' 225 | 226 | expect(function() { 227 | env(_fixture('.env.2'), {overwrite: true}) 228 | }).to.not.throw(Error) 229 | 230 | expect(process.env.FOO).to.be.equal('1') 231 | expect(process.env.BAR).to.be.equal('bar') 232 | expect(process.env.BAZ).to.be.equal(undefined) 233 | expect(process.env.QUX).to.be.equal('') 234 | expect(process.env.IGNORE).to.be.equal(undefined) 235 | }, 236 | 237 | '("./fixtures/.env.3")': function () { 238 | expect(function() { 239 | env(_fixture('.env.3')) 240 | }).to.not.throw(Error) 241 | 242 | expect(process.env.FOO).to.be.equal('http://foo.com?bar=baz') 243 | expect(process.env.BAR).to.be.equal(undefined) 244 | expect(process.env.BAZ).to.be.equal(undefined) 245 | expect(process.env.QUX).to.be.equal(undefined) 246 | expect(process.env.IGNORE).to.be.equal(undefined) 247 | }, 248 | 249 | '("./fixtures/.env.4")': function () { 250 | expect(function() { 251 | env(_fixture('.env.4')) 252 | }).to.not.throw(Error) 253 | 254 | expect(process.env.FOO).to.be.equal('http://foo.com#hash?bar=baz') 255 | expect(process.env.BAR).to.be.equal(undefined) 256 | expect(process.env.BAZ).to.be.equal(undefined) 257 | expect(process.env.QUX).to.be.equal(undefined) 258 | expect(process.env.IGNORE).to.be.equal(undefined) 259 | }, 260 | 261 | 262 | '("./fixtures/.env.5")': function () { 263 | expect(function() { 264 | env(_fixture('.env.5')) 265 | }).to.not.throw(Error) 266 | 267 | expect(process.env.FOO).to.be.equal('') 268 | expect(process.env.BAR).to.be.equal('1') 269 | expect(process.env.BAZ).to.be.equal('') 270 | expect(process.env.QUX).to.be.equal('sample') 271 | expect(process.env.NORF).to.be.equal('') 272 | expect(process.env.IGNORE).to.be.equal(undefined) 273 | }, 274 | 275 | '("./fixtures/.env.exports.0")': function () { 276 | expect(function() { 277 | env(_fixture('.env.exports.0')) 278 | }).to.not.throw(Error) 279 | 280 | expect(process.env.FOO).to.be.equal(undefined) 281 | expect(process.env.BAR).to.be.equal(undefined) 282 | expect(process.env.BAZ).to.be.equal(undefined) 283 | expect(process.env.QUX).to.be.equal(undefined) 284 | expect(process.env.IGNORE).to.be.equal(undefined) 285 | 286 | process.env.FOO = 'foo2' 287 | 288 | expect(function() { 289 | env(_fixture('.env.exports.0'), {}) 290 | }).to.not.throw(Error) 291 | 292 | expect(process.env.FOO).to.be.equal('foo2') 293 | expect(process.env.BAR).to.be.equal(undefined) 294 | expect(process.env.BAZ).to.be.equal(undefined) 295 | expect(process.env.QUX).to.be.equal(undefined) 296 | expect(process.env.IGNORE).to.be.equal(undefined) 297 | 298 | process.env.FOO = 'foo2' 299 | 300 | expect(function() { 301 | env(_fixture('.env.exports.0'), {overwrite: true}) 302 | }).to.not.throw(Error) 303 | 304 | expect(process.env.FOO).to.be.equal('foo2') 305 | expect(process.env.BAR).to.be.equal(undefined) 306 | expect(process.env.BAZ).to.be.equal(undefined) 307 | expect(process.env.QUX).to.be.equal(undefined) 308 | expect(process.env.IGNORE).to.be.equal(undefined) 309 | }, 310 | 311 | '("./fixtures/.env.exports.1")': function () { 312 | expect(function() { 313 | env(_fixture('.env.exports.1')) 314 | }).to.not.throw(Error) 315 | 316 | expect(process.env.FOO).to.be.equal('1') 317 | expect(process.env.BAR).to.be.equal(undefined) 318 | expect(process.env.BAZ).to.be.equal(undefined) 319 | expect(process.env.QUX).to.be.equal(undefined) 320 | expect(process.env.IGNORE).to.be.equal(undefined) 321 | 322 | process.env.FOO = 'foo2' 323 | 324 | expect(function() { 325 | env(_fixture('.env.exports.1'), {}) 326 | }).to.not.throw(Error) 327 | 328 | expect(process.env.FOO).to.be.equal('foo2') 329 | expect(process.env.BAR).to.be.equal(undefined) 330 | expect(process.env.BAZ).to.be.equal(undefined) 331 | expect(process.env.QUX).to.be.equal(undefined) 332 | expect(process.env.IGNORE).to.be.equal(undefined) 333 | 334 | process.env.FOO = 'foo2' 335 | 336 | expect(function() { 337 | env(_fixture('.env.exports.1'), {overwrite: true}) 338 | }).to.not.throw(Error) 339 | 340 | expect(process.env.FOO).to.be.equal('1') 341 | expect(process.env.BAR).to.be.equal(undefined) 342 | expect(process.env.BAZ).to.be.equal(undefined) 343 | expect(process.env.QUX).to.be.equal(undefined) 344 | expect(process.env.IGNORE).to.be.equal(undefined) 345 | }, 346 | 347 | '("./fixtures/.env.exports.2")': function () { 348 | expect(function() { 349 | env(_fixture('.env.exports.2')) 350 | }).to.not.throw(Error) 351 | 352 | expect(process.env.FOO).to.be.equal('1') 353 | expect(process.env.BAR).to.be.equal('bar') 354 | expect(process.env.BAZ).to.be.equal(undefined) 355 | expect(process.env.QUX).to.be.equal('') 356 | expect(process.env.IGNORE).to.be.equal(undefined) 357 | 358 | process.env.FOO = 'foo2' 359 | 360 | expect(function() { 361 | env(_fixture('.env.exports.2'), {}) 362 | }).to.not.throw(Error) 363 | 364 | expect(process.env.FOO).to.be.equal('foo2') 365 | expect(process.env.BAR).to.be.equal('bar') 366 | expect(process.env.BAZ).to.be.equal(undefined) 367 | expect(process.env.QUX).to.be.equal('') 368 | expect(process.env.IGNORE).to.be.equal(undefined) 369 | 370 | process.env.FOO = 'foo2' 371 | 372 | expect(function() { 373 | env(_fixture('.env.exports.2'), {overwrite: true}) 374 | }).to.not.throw(Error) 375 | 376 | expect(process.env.FOO).to.be.equal('1') 377 | expect(process.env.BAR).to.be.equal('bar') 378 | expect(process.env.BAZ).to.be.equal(undefined) 379 | expect(process.env.QUX).to.be.equal('') 380 | expect(process.env.IGNORE).to.be.equal(undefined) 381 | }, 382 | 383 | '("./fixtures/.env.exports.3")': function () { 384 | expect(function() { 385 | env(_fixture('.env.exports.3')) 386 | }).to.not.throw(Error) 387 | 388 | expect(process.env.FOO).to.be.equal('http://foo.com?bar=baz') 389 | expect(process.env.BAR).to.be.equal(undefined) 390 | expect(process.env.BAZ).to.be.equal(undefined) 391 | expect(process.env.QUX).to.be.equal(undefined) 392 | expect(process.env.IGNORE).to.be.equal(undefined) 393 | }, 394 | 395 | '("./fixtures/.env.exports.4")': function () { 396 | expect(function() { 397 | env(_fixture('.env.exports.4')) 398 | }).to.not.throw(Error) 399 | 400 | expect(process.env.FOO).to.be.equal('http://foo.com#hash?bar=baz') 401 | expect(process.env.BAR).to.be.equal(undefined) 402 | expect(process.env.BAZ).to.be.equal(undefined) 403 | expect(process.env.QUX).to.be.equal(undefined) 404 | expect(process.env.IGNORE).to.be.equal(undefined) 405 | } 406 | } 407 | 408 | } 409 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --ui exports 2 | --reporter spec 3 | --bail --------------------------------------------------------------------------------