From 95aa929a1dae749c204edfb3ce3df99490a77a24 Mon Sep 17 00:00:00 2001 From: Max Beatty Date: Mon, 29 Jan 2018 17:50:47 -0500 Subject: [PATCH] use explicit full path to form more informative error messages (#237) * use explicit full path to form more informative error messages * add appveyor for windows testing --- .travis.yml | 3 ++- README.md | 16 ++++++++-------- appveyor.yml | 3 +++ examples/es6-preload/run_me | 2 -- examples/es6/run_me | 2 -- examples/preload/run_me | 2 -- lib/main.js | 27 ++++++++++++++------------- package.json | 1 - test/config.js | 10 ---------- 9 files changed, 27 insertions(+), 39 deletions(-) create mode 100644 appveyor.yml diff --git a/.travis.yml b/.travis.yml index c7b60640..3ebfa5fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,4 +3,5 @@ language: node_js node_js: - 4 - 6 - - 7 + - 8 + - 9 diff --git a/README.md b/README.md index 99b8fb17..f8df1102 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Dotenv is a zero-dependency module that loads environment variables from a `.env` file into [`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env). Storing configuration in the environment separate from code is based on [The Twelve-Factor App](http://12factor.net/config) methodology. [![BuildStatus](https://img.shields.io/travis/motdotla/dotenv/master.svg?style=flat-square)](https://travis-ci.org/motdotla/dotenv) +[![Build status](https://ci.appveyor.com/api/projects/status/rnba2pyi87hgc8xw/branch/master?svg=true)](https://ci.appveyor.com/project/maxbeatty/dotenv/branch/master) [![NPM version](https://img.shields.io/npm/v/dotenv.svg?style=flat-square)](https://www.npmjs.com/package/dotenv) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) [![Coverage Status](https://img.shields.io/coveralls/motdotla/dotenv/master.svg?style=flat-square)](https://coveralls.io/github/motdotla/dotenv?branch=coverall-intergration) @@ -38,7 +39,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, @@ -48,8 +49,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 @@ -85,13 +85,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 @@ -112,9 +112,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' } ``` diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 00000000..aa0cee3e --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,3 @@ +version: 4.0.{build} +build: + verbosity: minimal diff --git a/examples/es6-preload/run_me b/examples/es6-preload/run_me index c1188a50..aa9d830a 100755 --- a/examples/es6-preload/run_me +++ b/examples/es6-preload/run_me @@ -1,5 +1,3 @@ #!/bin/bash -# TODO: make sure node version >1.6.0 - node -r babel/register -r ../../config.js index.js diff --git a/examples/es6/run_me b/examples/es6/run_me index f76d9ca5..7d2cef86 100755 --- a/examples/es6/run_me +++ b/examples/es6/run_me @@ -1,5 +1,3 @@ #!/bin/bash -# TODO: make sure node version >1.6.0 - node -r babel/register index.js diff --git a/examples/preload/run_me b/examples/preload/run_me index ca136d8c..bfeee6ea 100755 --- a/examples/preload/run_me +++ b/examples/preload/run_me @@ -1,5 +1,3 @@ #!/bin/bash -# TODO: make sure node version >1.6.0 - node -r ../../config.js index.js diff --git a/lib/main.js b/lib/main.js index ef74a7bc..1f0df927 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,6 +1,7 @@ 'use strict' -var fs = require('fs') +const fs = require('fs') +const path = require('path') /* * Parses a string or buffer into an object @@ -8,21 +9,21 @@ var fs = require('fs') * @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') } @@ -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 @@ -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 } } diff --git a/package.json b/package.json index 38558492..af77a033 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/config.js b/test/config.js index 609b5a22..ab5b6fc2 100644 --- a/test/config.js +++ b/test/config.js @@ -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) {