Skip to content

Commit

Permalink
Fix regex when parsing and preserving newline characters in env values
Browse files Browse the repository at this point in the history
  • Loading branch information
toddbluhm committed Jun 5, 2019
1 parent bec00aa commit 5eaff2c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## 9.0.3

- **Fix**: Use global flag on regex when parsing and preserving newline characters in env values (thanks to MaximTovstashev)

## 9.0.2

- **Fix**: CLI will now exit with non-zero error code when an error is encountered (thanks to blagh)
Expand Down
2 changes: 1 addition & 1 deletion dist/parse-env-file.js
Expand Up @@ -62,7 +62,7 @@ function parseEnvVars(envString) {
// remove any surrounding quotes
matches[key] = value
.replace(/(^['"]|['"]$)/g, '')
.replace(`\\n`, `\n`);
.replace(/\\n/g, `\n`);
}
return matches;
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "env-cmd",
"version": "9.0.2",
"version": "9.0.3",
"description": "Executes a command using the environment variables in an env file",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/parse-env-file.ts
Expand Up @@ -55,7 +55,7 @@ export function parseEnvVars (envString: string): { [key: string]: string } {
// remove any surrounding quotes
matches[key] = value
.replace(/(^['"]|['"]$)/g, '')
.replace(`\\n`, `\n`)
.replace(/\\n/g, `\n`)
}
return matches
}
Expand Down
17 changes: 17 additions & 0 deletions test/parse-env-file.spec.ts
Expand Up @@ -62,6 +62,13 @@ describe('parseEnvVars', (): void => {
assert(envVars.DOUBLE_TWO === '"double_two"')
})

it('should preserve newlines when surrounded in quotes', (): void => {
const envVars = parseEnvVars(`ONE_NEWLINE="ONE\\n"\nTWO_NEWLINES="HELLO\\nWORLD\\n"\nTHREE_NEWLINES="HELLO\\n\\nWOR\\nLD"\n`)
assert(envVars.ONE_NEWLINE === 'ONE\n')
assert(envVars.TWO_NEWLINES === 'HELLO\nWORLD\n')
assert(envVars.THREE_NEWLINES === 'HELLO\n\nWOR\nLD')
})

it('should preserve embedded single quotes', (): void => {
const envVars = parseEnvVars('SINGLE=\'\'\'\'\nSINGLE_ONE=\'\'single_one\'\'\nSINGLE_TWO="\'single_two\'"\n')
assert(envVars.SINGLE === '\'\'')
Expand Down Expand Up @@ -104,6 +111,16 @@ describe('getEnvFileVars', (): void => {
})
})

it('should parse a json file keeping all newlines intact', async (): Promise<void> => {
const env = await getEnvFileVars('./test/test-files/test-newlines.json')
assert.deepEqual(env, {
'THANKS': 'FOR WHAT?!',
'ANSWER': 42,
'ONLY': 'IN\n PRODUCTION',
'GALAXY': 'hitch\nhiking\n\n'
})
})

it('should parse a js file', async (): Promise<void> => {
const env = await getEnvFileVars('./test/test-files/test.js')
assert.deepEqual(env, {
Expand Down
6 changes: 6 additions & 0 deletions test/test-files/test-newlines.json
@@ -0,0 +1,6 @@
{
"THANKS": "FOR WHAT?!",
"ANSWER": 42,
"ONLY": "IN\n PRODUCTION",
"GALAXY": "hitch\nhiking\n\n"
}

0 comments on commit 5eaff2c

Please sign in to comment.