Skip to content

Commit

Permalink
Merge b619d4d into 46ec97d
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbeatty committed Nov 2, 2017
2 parents 46ec97d + b619d4d commit 1f57de2
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ language: node_js
node_js:
- 4
- 6
- 7
- 8
- 9
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ That's it.
`process.env` now has the keys and values you defined in your `.env` file.

```javascript
var db = require('db')
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
Expand All @@ -48,8 +48,7 @@ db.connect({

### Preload

If you are using iojs-v1.6.0 or later, you can use the `--require` (`-r`) command line option to preload dotenv. By doing this, you do not need to require and load dotenv in your application code.

You can use the `--require` (`-r`) command line option to preload dotenv. By doing this, you do not need to require and load dotenv in your application code. This is the preferred approach when using `import` instead of `require`.

```bash
$ node -r dotenv/config your_script.js
Expand Down Expand Up @@ -85,13 +84,13 @@ You can additionally, pass options to `config`.

#### Path

Default: `.env`
Default: `path.resolve(process.cwd(), '.env')`

You can specify a custom path if your file containing environment variables is
named or located differently.

```js
require('dotenv').config({path: '/custom/path/to/your/env/vars'})
require('dotenv').config({path: '/full/custom/path/to/your/env/vars'})
```

#### Encoding
Expand All @@ -112,9 +111,9 @@ variables is available to use. It accepts a String or Buffer and will return
an Object with the parsed keys and values.

```js
var dotenv = require('dotenv')
var buf = new Buffer('BASIC=basic')
var config = dotenv.parse(buf) // will return an object
const dotenv = require('dotenv')
const buf = new Buffer('BASIC=basic')
const config = dotenv.parse(buf) // will return an object
console.log(typeof config, config) // object { BASIC : 'basic' }
```

Expand Down
2 changes: 0 additions & 2 deletions examples/es6-preload/run_me
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/bin/bash

# TODO: make sure node version >1.6.0

node -r babel/register -r ../../config.js index.js
2 changes: 0 additions & 2 deletions examples/es6/run_me
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/bin/bash

# TODO: make sure node version >1.6.0

node -r babel/register index.js
2 changes: 0 additions & 2 deletions examples/preload/run_me
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/bin/bash

# TODO: make sure node version >1.6.0

node -r ../../config.js index.js
27 changes: 14 additions & 13 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
'use strict'

var fs = require('fs')
const fs = require('fs')
const path = require('path')

/*
* Parses a string or buffer into an object
* @param {(string|Buffer)} src - source to be parsed
* @returns {Object} keys and values from src
*/
function parse (src) {
var obj = {}
const obj = {}

// convert Buffers before splitting into lines and processing
src.toString().split('\n').forEach(function (line) {
// matching "KEY' and 'VAL' in 'KEY=VAL'
var keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
const keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
// matched?
if (keyValueArr != null) {
var key = keyValueArr[1]
const key = keyValueArr[1]

// default undefined or missing values to empty string
var value = keyValueArr[2] || ''
let value = keyValueArr[2] || ''

// expand newlines in quoted values
var len = value ? value.length : 0
const len = value ? value.length : 0
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
value = value.replace(/\\n/gm, '\n')
}
Expand All @@ -45,12 +46,12 @@ function parse (src) {
* @returns {Object} parsed object or error
*/
function config (options) {
var path = '.env'
var encoding = 'utf8'
let dotenvPath = path.resolve(process.cwd(), '.env')
let encoding = 'utf8'

if (options) {
if (options.path) {
path = options.path
dotenvPath = options.path
}
if (options.encoding) {
encoding = options.encoding
Expand All @@ -59,15 +60,15 @@ function config (options) {

try {
// specifying an encoding returns a string instead of a buffer
var parsedObj = parse(fs.readFileSync(path, { encoding: encoding }))
const parsed = parse(fs.readFileSync(dotenvPath, { encoding }))

Object.keys(parsedObj).forEach(function (key) {
Object.keys(parsed).forEach(function (key) {
if (!process.env.hasOwnProperty(key)) {
process.env[key] = parsedObj[key]
process.env[key] = parsed[key]
}
})

return { parsed: parsedObj }
return { parsed }
} catch (e) {
return { error: e }
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"babel": "5.8.23",
"coveralls": "^2.11.9",
"lab": "11.1.0",
"semver": "5.3.0",
"should": "11.1.1",
"sinon": "1.17.6",
"standard": "8.4.0",
Expand Down
10 changes: 0 additions & 10 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,15 @@

require('should')
var cp = require('child_process')
var semver = require('semver')
// var sinon = require('sinon')
var Lab = require('lab')
var lab = exports.lab = Lab.script()
var describe = lab.experiment
// var before = lab.before
// var beforeEach = lab.beforeEach
// var afterEach = lab.afterEach
var it = lab.test
var nodeBinary = process.argv[0]

describe('config', function () {
describe('preload', function () {
it('loads .env', function (done) {
// preloading was introduced in v1.6.0 so skip test for other environments
if (semver.lt(process.env.npm_config_node_version, '1.6.0')) {
return done()
}

cp.exec(
nodeBinary + ' -r ../config -e "console.log(process.env.BASIC)" dotenv_config_path=./test/.env',
function (err, stdout, stderr) {
Expand Down

0 comments on commit 1f57de2

Please sign in to comment.