Skip to content

Commit

Permalink
feat: parse inline comments
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Dec 11, 2024
1 parent 4790a82 commit 6fc9a41
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
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"}'

0 comments on commit 6fc9a41

Please sign in to comment.