Skip to content

Commit

Permalink
allow importing config method directly in es6 closes #163
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbeatty committed Dec 9, 2016
1 parent 233db60 commit 8117fc1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 69 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ language: node_js
node_js:
- 4
- 6
- 7
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ for (var k in envConfig) {
}
```


### Can I customize/write plugins for dotenv?

For `[email protected]`: Yes. `dotenv.config()` now returns an object representing
Expand All @@ -185,7 +184,6 @@ For `[email protected]`: Use [dotenv-expand](https://github.com/motdotla/dotenv-expan

For `[email protected]`: We haven't been presented with a compelling use case for expanding variables and believe it leads to env vars that are not "fully orthogonal" as [The Twelve-Factor App](http://12factor.net/config) outlines.<sup>[[1](https://github.com/motdotla/dotenv/issues/39)][[2](https://github.com/motdotla/dotenv/pull/97)]</sup> Please open an issue if you have a compelling use case.


## Contributing Guide

See [CONTRIBUTING.md](CONTRIBUTING.md)
Expand All @@ -202,7 +200,6 @@ See [LICENSE](LICENSE)

Here's just a few of many repositories using dotenv:

* [npm](https://github.com/npm/newww)
* [jaws](https://github.com/jaws-framework/jaws-core-js)
* [node-lambda](https://github.com/motdotla/node-lambda)
* [resume-cli](https://www.npmjs.com/package/resume-cli)
Expand Down
2 changes: 1 addition & 1 deletion examples/es6-preload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// e.g. node -r babel/register -r dotenv/config index.js
// run this example by executing the "run_me" script: ./run_me

import {uptime} from 'os'
import { uptime } from 'os'

console.log(process.env.ES6PRELOAD)
console.log(`Your computer has been up for ${Math.floor(uptime() / 60 / 60 / 24)} days`)
5 changes: 3 additions & 2 deletions examples/es6/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// therefore we'll preload babel's register function
// run this example by executing the "run_me" script: ./run_me

import dotenv from '../../lib/main'
dotenv.config()
import { config } from '../../lib/main'

config()

console.log(process.env.ES6)
125 changes: 62 additions & 63 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,80 @@

var fs = require('fs')

module.exports = {
/*
* Main entry point into dotenv. Allows configuration before loading .env
* @param {Object} options - valid options: path ('.env'), encoding ('utf8')
* @returns {Boolean}
*/
config: function (options) {
var path = '.env'
var encoding = 'utf8'
var verbose = false
/*
* Parses a string or buffer into an object
* @param {String|Buffer} src - source to be parsed
* @returns {Object}
*/
function parse (src) {
var obj = {}

if (options) {
if (options.verbose) {
verbose = options.verbose
}
if (options.path) {
path = options.path
}
if (options.encoding) {
encoding = options.encoding
}
}
// 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*$/)
// matched?
if (keyValueArr != null) {
var key = keyValueArr[1]

try {
// specifying an encoding returns a string instead of a buffer
var parsedObj = this.parse(fs.readFileSync(path, { encoding: encoding }))
// default undefined or missing values to empty string
var value = keyValueArr[2] ? keyValueArr[2] : ''

Object.keys(parsedObj).forEach(function (key) {
process.env[key] = process.env[key] || parsedObj[key]
})

return parsedObj
} catch (e) {
if (verbose) {
console.error('dotenv failed to parse and/or populate:' + e.message)
// expand newlines in quoted values
var len = value ? value.length : 0
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
value = value.replace(/\\n/gm, '\n')
}
return false
}
},

/*
* Parses a string or buffer into an object
* @param {String|Buffer} src - source to be parsed
* @returns {Object}
*/
parse: function (src) {
var obj = {}
// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim()

obj[key] = value
}
})

// 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*$/)
// matched?
if (keyValueArr != null) {
var key = keyValueArr[1]
return obj
}

// default undefined or missing values to empty string
var value = keyValueArr[2] ? keyValueArr[2] : ''
/*
* Main entry point into dotenv. Allows configuration before loading .env
* @param {Object} options - valid options: path ('.env'), encoding ('utf8')
* @returns {Boolean}
*/
function config (options) {
var path = '.env'
var encoding = 'utf8'
var verbose = false

// expand newlines in quoted values
var len = value ? value.length : 0
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
value = value.replace(/\\n/gm, '\n')
}
if (options) {
if (options.verbose) {
verbose = options.verbose
}
if (options.path) {
path = options.path
}
if (options.encoding) {
encoding = options.encoding
}
}

// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim()
try {
// specifying an encoding returns a string instead of a buffer
var parsedObj = parse(fs.readFileSync(path, { encoding: encoding }))

obj[key] = value
}
Object.keys(parsedObj).forEach(function (key) {
process.env[key] = process.env[key] || parsedObj[key]
})

return obj
return parsedObj
} catch (e) {
if (verbose) {
console.error('dotenv failed to parse and/or populate:' + e.message)
}
return false
}

}

module.exports.load = module.exports.config
module.exports.config = config
module.exports.load = config
module.exports.parse = parse

0 comments on commit 8117fc1

Please sign in to comment.