mirror of
https://github.com/toddbluhm/env-cmd.git
synced 2025-12-08 18:23:33 +00:00
Merge pull request #393 from toddbluhm/fix-strip-comments
fix(parse-env-file): fix strip comments per PR #333
This commit is contained in:
commit
4790a8251b
10
dist/parse-env-file.js
vendored
10
dist/parse-env-file.js
vendored
@ -85,14 +85,8 @@ export function parseEnvVars(envString) {
|
|||||||
* Strips out comments from env file string
|
* Strips out comments from env file string
|
||||||
*/
|
*/
|
||||||
export function stripComments(envString) {
|
export function stripComments(envString) {
|
||||||
const commentsRegex = /(^#.*$)/gim;
|
const commentsRegex = /(^\s*#.*$)/gim;
|
||||||
let match = commentsRegex.exec(envString);
|
return envString.replace(commentsRegex, '');
|
||||||
let newString = envString;
|
|
||||||
while (match != null) {
|
|
||||||
newString = newString.replace(match[1], '');
|
|
||||||
match = commentsRegex.exec(envString);
|
|
||||||
}
|
|
||||||
return newString;
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Strips out newlines from env file string
|
* Strips out newlines from env file string
|
||||||
|
|||||||
@ -94,14 +94,8 @@ export function parseEnvVars(envString: string): Environment {
|
|||||||
* Strips out comments from env file string
|
* Strips out comments from env file string
|
||||||
*/
|
*/
|
||||||
export function stripComments(envString: string): string {
|
export function stripComments(envString: string): string {
|
||||||
const commentsRegex = /(^#.*$)/gim
|
const commentsRegex = /(^\s*#.*$)/gim
|
||||||
let match = commentsRegex.exec(envString)
|
return envString.replace(commentsRegex, '')
|
||||||
let newString = envString
|
|
||||||
while (match != null) {
|
|
||||||
newString = newString.replace(match[1], '')
|
|
||||||
match = commentsRegex.exec(envString)
|
|
||||||
}
|
|
||||||
return newString
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -16,6 +16,11 @@ describe('stripComments', (): void => {
|
|||||||
const envString = stripComments('#BOB=COOL\nNODE_ENV=dev\nANSWER=42 AND COUNTING\n#AnotherComment\n')
|
const envString = stripComments('#BOB=COOL\nNODE_ENV=dev\nANSWER=42 AND COUNTING\n#AnotherComment\n')
|
||||||
assert(envString === '\nNODE_ENV=dev\nANSWER=42 AND COUNTING\n\n')
|
assert(envString === '\nNODE_ENV=dev\nANSWER=42 AND COUNTING\n\n')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should not strip out #s from values', (): void => {
|
||||||
|
const envString = stripComments('#\nBOB=COMMENT#ELL\n#\nNODE_ENV=dev\nANSWER=42 AND COUNTING\n#AnotherComment\n')
|
||||||
|
assert(envString === '\nBOB=COMMENT#ELL\n\nNODE_ENV=dev\nANSWER=42 AND COUNTING\n\n', envString)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('parseEnvVars', (): void => {
|
describe('parseEnvVars', (): void => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user