diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b3893d9..a7045ce6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +## [Unreleased, to be released as 11.0.0] + +### Added + +- Support inline comments + ## [10.0.0](https://github.com/motdotla/dotenv/compare/v9.0.2...v10.0.0) (2021-05-20) ### Added diff --git a/lib/main.js b/lib/main.js index 43711f39..68889ec6 100644 --- a/lib/main.js +++ b/lib/main.js @@ -30,7 +30,7 @@ function log (message /*: string */) { } const NEWLINE = '\n' -const RE_INI_KEY_VAL = /^\s*([\w.-]+)\s*=\s*(.*)?\s*$/ +const RE_INI_KEY_VAL = /^\s*([\w.-]+)\s*=\s*("[^"]*"|'[^']*'|[^#]*)?(\s*|\s*#.*)?$/ const RE_NEWLINES = /\\n/g const NEWLINES_MATCH = /\r\n|\n|\r/ diff --git a/package-lock.json b/package-lock.json index 75de5eca..0d16863d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "dotenv", - "version": "10.0.0", + "version": "11.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 85ff8d71..d08875af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dotenv", - "version": "10.0.0", + "version": "11.0.0", "description": "Loads environment variables from .env file", "main": "lib/main.js", "exports": { diff --git a/tests/.env b/tests/.env index d1e9301e..0fdabc97 100644 --- a/tests/.env +++ b/tests/.env @@ -7,10 +7,15 @@ SINGLE_QUOTES='single_quotes' SINGLE_QUOTES_SPACED=' single quotes ' DOUBLE_QUOTES="double_quotes" DOUBLE_QUOTES_SPACED=" double quotes " +DOUBLE_QUOTES_INSIDE_SINGLE='double "quotes" work inside single quotes' +SINGLE_QUOTES_INSIDE_DOUBLE="single 'quotes' work inside double quotes" EXPAND_NEWLINES="expand\nnew\nlines" DONT_EXPAND_UNQUOTED=dontexpand\nnewlines DONT_EXPAND_SQUOTED='dontexpand\nnewlines' # COMMENTS=work +INLINE_COMMENTS=inline comments # work #very #well +INLINE_COMMENTS_SINGLE_QUOTES='inline comments outside of #singlequotes' # work +INLINE_COMMENTS_DOUBLE_QUOTES="inline comments outside of #doublequotes" # work EQUAL_SIGNS=equals== RETAIN_INNER_QUOTES={"foo": "bar"} RETAIN_LEADING_DQUOTE="retained diff --git a/tests/test-parse.js b/tests/test-parse.js index d042fc97..8642ad0e 100644 --- a/tests/test-parse.js +++ b/tests/test-parse.js @@ -9,7 +9,7 @@ const dotenv = require('../lib/main') const parsed = dotenv.parse(fs.readFileSync('tests/.env', { encoding: 'utf8' })) -t.plan(28) +t.plan(33) t.type(parsed, Object, 'should return an object') @@ -27,6 +27,10 @@ t.equal(parsed.DOUBLE_QUOTES, 'double_quotes', 'escapes double quoted values') t.equal(parsed.DOUBLE_QUOTES_SPACED, ' double quotes ', 'respects surrounding spaces in double quotes') +t.equal(parsed.DOUBLE_QUOTES_INSIDE_SINGLE, 'double "quotes" work inside single quotes', 'respects double quotes inside single quotes') + +t.equal(parsed.SINGLE_QUOTES_INSIDE_DOUBLE, "single 'quotes' work inside double quotes", 'respects single quotes inside double quotes') + t.equal(parsed.EXPAND_NEWLINES, 'expand\nnew\nlines', 'expands newlines but only if double quoted') t.equal(parsed.DONT_EXPAND_UNQUOTED, 'dontexpand\\nnewlines', 'expands newlines but only if double quoted') @@ -35,6 +39,12 @@ t.equal(parsed.DONT_EXPAND_SQUOTED, 'dontexpand\\nnewlines', 'expands newlines b t.notOk(parsed.COMMENTS, 'ignores commented lines') +t.equal(parsed.INLINE_COMMENTS, 'inline comments', 'ignores inline comments') + +t.equal(parsed.INLINE_COMMENTS_SINGLE_QUOTES, 'inline comments outside of #singlequotes', 'ignores inline comments, but respects # character inside of single quotes') + +t.equal(parsed.INLINE_COMMENTS_DOUBLE_QUOTES, 'inline comments outside of #doublequotes', 'ignores inline comments, but respects # character inside of double quotes') + t.equal(parsed.EQUAL_SIGNS, 'equals==', 'respects equals signs in values') t.equal(parsed.RETAIN_INNER_QUOTES, '{"foo": "bar"}', 'retains inner quotes')