Comparing version 14.2.0 to 14.3.0
@@ -5,6 +5,8 @@ # Changelog | ||
## [Unreleased](https://github.com/motdotla/dotenv/compare/v14.1.1...master) | ||
## [Unreleased](https://github.com/motdotla/dotenv/compare/v14.3.0...master) | ||
(place unreleased comments here) | ||
## [14.3.0](https://github.com/motdotla/dotenv/compare/v14.2.0...v14.3.0) | ||
- Add `multiline` option 🎉 ([#486](https://github.com/motdotla/dotenv/pull/486)) | ||
## [14.2.0](https://github.com/motdotla/dotenv/compare/v14.1.1...v14.2.0) (2022-01-17) | ||
@@ -11,0 +13,0 @@ |
@@ -1,2 +0,2 @@ | ||
const re = /^dotenv_config_(encoding|path|debug|override)=(.+)$/ | ||
const re = /^dotenv_config_(encoding|path|debug|override|multiline)=(.+)$/ | ||
@@ -3,0 +3,0 @@ module.exports = function optionMatcher (args) { |
@@ -20,2 +20,6 @@ // ../config.js accepts options via environment variables | ||
if (process.env.DOTENV_CONFIG_MULTILINE != null) { | ||
options.multiline = process.env.DOTENV_CONFIG_MULTILINE | ||
} | ||
module.exports = options |
@@ -13,2 +13,17 @@ // TypeScript Version: 3.0 | ||
debug?: boolean; | ||
/** | ||
* Default: `false` | ||
* | ||
* Turn on multiline line break parsing. | ||
* | ||
* example: | ||
* | ||
* MY_VAR="this | ||
* is | ||
* a | ||
* multiline | ||
* string" | ||
*/ | ||
multiline?: boolean; | ||
} | ||
@@ -70,2 +85,17 @@ | ||
override?: boolean; | ||
/** | ||
* Default: `false` | ||
* | ||
* Turn on multiline line break parsing. | ||
* | ||
* example: | ||
* | ||
* MY_VAR="this | ||
* is | ||
* a | ||
* multiline | ||
* string" | ||
*/ | ||
multiline?: boolean; | ||
} | ||
@@ -83,3 +113,3 @@ | ||
* | ||
* @param options - additional options. example: `{ path: './custom/path', encoding: 'latin1', debug: true, override: false }` | ||
* @param options - additional options. example: `{ path: './custom/path', encoding: 'latin1', debug: true, override: false, multiline: false }` | ||
* @returns an object with a `parsed` key if successful or `error` key if an error occurred. example: { parsed: { KEY: 'value' } } | ||
@@ -86,0 +116,0 @@ * |
@@ -17,6 +17,11 @@ const fs = require('fs') | ||
const debug = Boolean(options && options.debug) | ||
const multiline = Boolean(options && options.multiline) | ||
const obj = {} | ||
// convert Buffers before splitting into lines and processing | ||
src.toString().split(NEWLINES_MATCH).forEach(function (line, idx) { | ||
const lines = src.toString().split(NEWLINES_MATCH) | ||
for (let idx = 0; idx < lines.length; idx++) { | ||
let line = lines[idx] | ||
// matching "KEY' and 'VAL' in 'KEY=VAL' | ||
@@ -29,8 +34,26 @@ const keyValueArr = line.match(RE_INI_KEY_VAL) | ||
let val = (keyValueArr[2] || '') | ||
const end = val.length - 1 | ||
let end = val.length - 1 | ||
const isDoubleQuoted = val[0] === '"' && val[end] === '"' | ||
const isSingleQuoted = val[0] === "'" && val[end] === "'" | ||
const isMultilineDoubleQuoted = val[0] === '"' && val[end] !== '"' | ||
const isMultilineSingleQuoted = val[0] === "'" && val[end] !== "'" | ||
// if parsing line breaks and the value starts with a quote | ||
if (multiline && (isMultilineDoubleQuoted || isMultilineSingleQuoted)) { | ||
const quoteChar = isMultilineDoubleQuoted ? '"' : "'" | ||
val = val.substring(1) | ||
while (idx++ < lines.length - 1) { | ||
line = lines[idx] | ||
end = line.length - 1 | ||
if (line[end] === quoteChar) { | ||
val += NEWLINE + line.substring(0, end) | ||
break | ||
} | ||
val += NEWLINE + line | ||
} | ||
// if single or double quoted, remove quotes | ||
if (isSingleQuoted || isDoubleQuoted) { | ||
} else if (isSingleQuoted || isDoubleQuoted) { | ||
val = val.substring(1, end) | ||
@@ -56,3 +79,3 @@ | ||
} | ||
}) | ||
} | ||
@@ -72,2 +95,3 @@ return obj | ||
const override = Boolean(options && options.override) | ||
const multiline = Boolean(options && options.multiline) | ||
@@ -85,3 +109,3 @@ if (options) { | ||
// specifying an encoding returns a string instead of a buffer | ||
const parsed = parse(fs.readFileSync(dotenvPath, { encoding }), { debug }) | ||
const parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding }), { debug, multiline }) | ||
@@ -116,3 +140,7 @@ Object.keys(parsed).forEach(function (key) { | ||
module.exports.config = config | ||
module.exports.parse = parse | ||
const DotenvModule = { | ||
config, | ||
parse | ||
} | ||
module.exports = DotenvModule |
{ | ||
"name": "dotenv", | ||
"version": "14.2.0", | ||
"version": "14.3.0", | ||
"description": "Loads environment variables from .env file", | ||
@@ -5,0 +5,0 @@ "main": "lib/main.js", |
@@ -86,2 +86,3 @@ <p align="center"> | ||
* [nodejs (override on)](https://github.com/dotenv-org/examples/tree/master/dotenv-nodejs-override) | ||
* [nodejs (multiline on)](https://github.com/dotenv-org/examples/tree/master/dotenv-nodejs-multiline) | ||
* [esm](https://github.com/dotenv-org/examples/tree/master/dotenv-esm) | ||
@@ -165,2 +166,23 @@ * [esm (preload)](https://github.com/dotenv-org/examples/tree/master/dotenv-esm-preload) | ||
##### Multiline | ||
Default: `false` | ||
Turn on multiline line break parsing. | ||
```js | ||
require('dotenv').config({ multiline: true }) | ||
``` | ||
This allows specifying multiline values in this format: | ||
``` | ||
PRIVATE_KEY="-----BEGIN PRIVATE KEY----- | ||
MIGT... | ||
7ure... | ||
-----END PRIVATE KEY-----" | ||
``` | ||
Ensure that the value begins with a single or double quote character, and it ends with the same character. | ||
### Parse | ||
@@ -195,2 +217,21 @@ | ||
##### Multiline | ||
Default: `false` | ||
Turn on multiline line break parsing. | ||
```js | ||
require('dotenv').config({ multiline: true }) | ||
``` | ||
This allows specifying multiline values in this format: | ||
``` | ||
PRIVATE_KEY="-----BEGIN PRIVATE KEY----- | ||
MIGT... | ||
7ure... | ||
-----END PRIVATE KEY-----" | ||
``` | ||
## Other Usage | ||
@@ -197,0 +238,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32965
258
410