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

feat: parse inline comments #397

Merged
merged 1 commit into from
Dec 16, 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
17 changes: 12 additions & 5 deletions src/parse-env-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,19 @@ export function parseEnvVars(envString: string): Environment {
while ((match = envParseRegex.exec(envString)) !== null) {
// Note: match[1] is the full env=var line
const key = match[2].trim()
let value: string | number | boolean = match[3].trim()
let value = match[3].trim()

// remove any surrounding quotes
value = value
.replace(/(^['"]|['"]$)/g, '')
.replace(/\\n/g, '\n')
// if the string is quoted, remove everything after the final
// quote. This implicitly removes inline comments.
if (value.startsWith("'") || value.startsWith('"')) {
value = value.slice(1, value.lastIndexOf(value[0]));
} else {
// if the string is not quoted, we need to explicitly remove
// inline comments.
value = value.split('#')[0].trim();
}

value = value.replace(/\\n/g, '\n');

// Convert string to JS type if appropriate
if (value !== '' && !isNaN(+value)) {
Expand Down
38 changes: 38 additions & 0 deletions test/parse-env-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,36 @@ describe('parseEnvVars', (): void => {
assert(envVars.NODE_ENV === 'dev')
assert(envVars.ANSWER === ' 42 AND COUNTING')
})

describe('value', () => {
const testCases = [
[`a`, `a`],
[`'a'`, `a`],
[`"a"`, `a`],
[`"a"`, `a`],
[`"single 'quotes' inside double"`, `single 'quotes' inside double`],
[`'double "quotes" inside single'`, `double "quotes" inside single`],
[`a # comment without quotes`, `a`],
[`'a' # comment with single quotes`, `a`],
[`"a" # comment with double quotes`, `a`],
[`"a"garbage after final quote`, `a`],
[
`"double quotes " within double quotes"`,
`double quotes " within double quotes`,
],
[
`'single quotes ' within single quotes'`,
`single quotes ' within single quotes`,
],
[`line\\nbreaks`, `line\nbreaks`],
[`"line\\n\\nbreaks in quotes"`, `line\n\nbreaks in quotes`],
];
for (const [input, output] of testCases) {
it(`should parse ${input}`, () => {
assert.equal(parseEnvVars(`KEY=${input}`).KEY, output)
})
}
})
})

describe('parseEnvString', (): void => {
Expand Down Expand Up @@ -172,6 +202,14 @@ describe('getEnvFileVars', (): void => {
ONLY: 'IN=PRODUCTION',
GALAXY: 'hitch\nhiking',
BRINGATOWEL: true,
a: 1,
b: 2,
c: 3,
d: "=",
e: "equals symbol = = ",
json_no_quotes: "{\"foo\": \"bar\"}",
json_with_quotes: "{\"foo\": \"bar\"}",
quotes: "hello \n\n \" 'escaped \\tsingle quote' \" #cool #fun ",
})
})

Expand Down
11 changes: 11 additions & 0 deletions test/test-files/test
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ ANSWER=42
ONLY= "IN=PRODUCTION"
GALAXY="hitch\nhiking"
BRINGATOWEL=true
# full-line comment
#
quotes = 'hello \n\n " 'escaped \tsingle quote' " #cool #fun ' # inline comment
a=1#inline comment
b='2'#inline comment
c="3"#inline comment
d==
e='equals symbol = = '
json_no_quotes={"foo": "bar"}
json_with_quotes='{"foo": "bar"}'
json_with_quotes='{"foo": "bar"}'
Loading