Skip to content

Commit

Permalink
Fix removing unbalanced quotes (motdotla#376)
Browse files Browse the repository at this point in the history
* upgrade dependencies

* fix flow warnings

* remove load alias

* simplify links to projects that depend on and extend dotenv

fixes outdated projects and makes future maintaince easier

* retain single leading or trailing quotes, maintain whitespace in quotes

* don't publish examples to npm

* rm node v9 from travis test matrix

EOL 2018-06-30
  • Loading branch information
maxbeatty authored Mar 13, 2019
1 parent 406d0d1 commit 01b859a
Show file tree
Hide file tree
Showing 12 changed files with 1,054 additions and 2,341 deletions.
2 changes: 1 addition & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ untyped-import
untyped-type-import

[version]
^0.84.0
^0.92.1
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.nyc_output/
examples/
flow-typed/
tests/
.editorconfig
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ os:
node_js:
- "6"
- "8"
- "9"
- "10"
- "11"

Expand Down
38 changes: 8 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ DB_USER=root
DB_PASS=s1mpl3
```

That's it.

`process.env` now has the keys and values you defined in your `.env` file.

```javascript
Expand Down Expand Up @@ -77,9 +75,7 @@ $ DOTENV_CONFIG_ENCODING=latin1 node -r dotenv/config your_script.js dotenv_conf

## Config

_Alias: `load`_

`config` will read your .env file, parse the contents, assign it to
`config` will read your `.env` file, parse the contents, assign it to
[`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env),
and return an Object with a `parsed` key containing the loaded content or an `error` key if it failed.

Expand Down Expand Up @@ -164,17 +160,17 @@ The parsing engine currently supports the following rules:
- empty lines are skipped
- lines beginning with `#` are treated as comments
- empty values become empty strings (`EMPTY=` becomes `{EMPTY: ''}`)
- inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`)
- whitespace is removed from both ends of unquoted values (see more on [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)) (`FOO= some value ` becomes `{FOO: 'some value'}`)
- single and double quoted values are escaped (`SINGLE_QUOTE='quoted'` becomes `{SINGLE_QUOTE: "quoted"}`)
- new lines are expanded if in double quotes (`MULTILINE="new\nline"` becomes
- single and double quoted values maintain whitespace from both ends (`FOO=" some value "` becomes `{FOO: ' some value '}`)
- double quoted values expand new lines (`MULTILINE="new\nline"` becomes

```
{MULTILINE: 'new
line'}
```

- inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`)
- whitespace is removed from both ends of the value (see more on [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)) (`FOO=" some value "` becomes `{FOO: 'some value'}`)

## FAQ

### Should I commit my `.env` file?
Expand Down Expand Up @@ -270,26 +266,8 @@ See [CHANGELOG.md](CHANGELOG.md)

See [LICENSE](LICENSE)

## Who's using dotenv

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

* [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)
* [phant](https://www.npmjs.com/package/phant)
* [adafruit-io-node](https://github.com/adafruit/adafruit-io-node)
* [mockbin](https://www.npmjs.com/package/mockbin)
* [and many more...](https://www.npmjs.com/browse/depended/dotenv)

## Go well with dotenv
## Who's using dotenv?

Here's some projects that expand on dotenv. Check them out.
[These npm modules depend on it.](https://www.npmjs.com/browse/depended/dotenv)

* [require-environment-variables](https://github.com/bjoshuanoah/require-environment-variables)
* [dotenv-safe](https://github.com/rolodato/dotenv-safe)
* [envalid](https://github.com/af/envalid)
* [lookenv](https://github.com/RodrigoEspinosa/lookenv)
* [run.env](https://www.npmjs.com/package/run.env)
* [dotenv-webpack](https://github.com/mrsteele/dotenv-webpack)
* [env-path](https://github.com/benrei/env-path)
Projects that expand it often use the [keyword "dotenv" on npm](https://www.npmjs.com/search?q=keywords:dotenv).
60 changes: 60 additions & 0 deletions flow-typed/npm/decache_vx.x.x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// flow-typed signature: abdbe43f01cbcc690c733cdb2033ad4f
// flow-typed version: <<STUB>>/decache_v4.5.0/flow_v0.92.1

/**
* This is an autogenerated libdef stub for:
*
* 'decache'
*
* Fill this stub out by replacing all the `any` types.
*
* Once filled out, we encourage you to share your work with the
* community by sending a pull request to:
* https://github.com/flowtype/flow-typed
*/

declare module 'decache' {
declare module.exports: any;
}

/**
* We include stubs for each file inside this npm package in case you need to
* require those files directly. Feel free to delete any files that aren't
* needed.
*/
declare module 'decache/decache' {
declare module.exports: any;
}

declare module 'decache/lib/mymodule/index' {
declare module.exports: any;
}

declare module 'decache/lib/othermodule' {
declare module.exports: any;
}

declare module 'decache/setup' {
declare module.exports: any;
}

declare module 'decache/test/test' {
declare module.exports: any;
}

// Filename aliases
declare module 'decache/decache.js' {
declare module.exports: $Exports<'decache/decache'>;
}
declare module 'decache/lib/mymodule/index.js' {
declare module.exports: $Exports<'decache/lib/mymodule/index'>;
}
declare module 'decache/lib/othermodule.js' {
declare module.exports: $Exports<'decache/lib/othermodule'>;
}
declare module 'decache/setup.js' {
declare module.exports: $Exports<'decache/setup'>;
}
declare module 'decache/test/test.js' {
declare module.exports: $Exports<'decache/test/test'>;
}
6 changes: 3 additions & 3 deletions lib/env-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// ../config.js accepts options via environment variables
const options = {}

if (process.env.DOTENV_CONFIG_ENCODING) {
if (process.env.DOTENV_CONFIG_ENCODING != null) {
options.encoding = process.env.DOTENV_CONFIG_ENCODING
}

if (process.env.DOTENV_CONFIG_PATH) {
if (process.env.DOTENV_CONFIG_PATH != null) {
options.path = process.env.DOTENV_CONFIG_PATH
}

if (process.env.DOTENV_CONFIG_DEBUG) {
if (process.env.DOTENV_CONFIG_DEBUG != null) {
options.debug = process.env.DOTENV_CONFIG_DEBUG
}

Expand Down
37 changes: 23 additions & 14 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,42 @@ function log (message /*: string */) {
console.log(`[dotenv][DEBUG] ${message}`)
}

const NEWLINE = '\n'
const RE_INI_KEY_VAL = /^\s*([\w.-]+)\s*=\s*(.*)?\s*$/
const RE_NEWLINES = /\\n/g

// Parses src into an Object
function parse (src /*: string | Buffer */, options /*: ?DotenvParseOptions */) /*: DotenvParseOutput */ {
const debug = Boolean(options && options.debug)
const obj = {}

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

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

// expand newlines in quoted values
const len = value ? value.length : 0
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
value = value.replace(/\\n/gm, '\n')
let val = (keyValueArr[2] || '')
const end = val.length - 1
const isDoubleQuoted = val[0] === '"' && val[end] === '"'
const isSingleQuoted = val[0] === "'" && val[end] === "'"

// if single or double quoted, remove quotes
if (isSingleQuoted || isDoubleQuoted) {
val = val.substring(1, end)

// if double quoted, expand newlines
if (isDoubleQuoted) {
val = val.replace(RE_NEWLINES, NEWLINE)
}
} else {
// remove surrounding whitespace
val = val.trim()
}

// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim()

obj[key] = value
obj[key] = val
} else if (debug) {
log(`did not match key and value when parsing line ${idx + 1}: ${line}`)
}
Expand Down Expand Up @@ -99,5 +109,4 @@ function config (options /*: ?DotenvConfigOptions */) /*: DotenvConfigOutput */
}

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

0 comments on commit 01b859a

Please sign in to comment.