Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parse-env-file): fix strip comments per PR #333 #393

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions dist/parse-env-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,8 @@ export function parseEnvVars(envString) {
* Strips out comments from env file string
*/
export function stripComments(envString) {
const commentsRegex = /(^#.*$)/gim;
let match = commentsRegex.exec(envString);
let newString = envString;
while (match != null) {
newString = newString.replace(match[1], '');
match = commentsRegex.exec(envString);
}
return newString;
const commentsRegex = /(^\s*#.*$)/gim;
return envString.replace(commentsRegex, '');
}
/**
* Strips out newlines from env file string
Expand Down
10 changes: 2 additions & 8 deletions src/parse-env-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,8 @@ export function parseEnvVars(envString: string): Environment {
* Strips out comments from env file string
*/
export function stripComments(envString: string): string {
const commentsRegex = /(^#.*$)/gim
let match = commentsRegex.exec(envString)
let newString = envString
while (match != null) {
newString = newString.replace(match[1], '')
match = commentsRegex.exec(envString)
}
return newString
const commentsRegex = /(^\s*#.*$)/gim
return envString.replace(commentsRegex, '')
}

/**
Expand Down
5 changes: 5 additions & 0 deletions test/parse-env-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ describe('stripComments', (): void => {
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')
})

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 => {
Expand Down
Loading