mirror of
https://github.com/toddbluhm/env-cmd.git
synced 2025-12-08 18:23:33 +00:00
feat: parse inline comments
This commit is contained in:
parent
4790a8251b
commit
6fc9a4197e
@ -66,12 +66,19 @@ export function parseEnvVars(envString: string): Environment {
|
|||||||
while ((match = envParseRegex.exec(envString)) !== null) {
|
while ((match = envParseRegex.exec(envString)) !== null) {
|
||||||
// Note: match[1] is the full env=var line
|
// Note: match[1] is the full env=var line
|
||||||
const key = match[2].trim()
|
const key = match[2].trim()
|
||||||
let value: string | number | boolean = match[3].trim()
|
let value = match[3].trim()
|
||||||
|
|
||||||
// remove any surrounding quotes
|
// if the string is quoted, remove everything after the final
|
||||||
value = value
|
// quote. This implicitly removes inline comments.
|
||||||
.replace(/(^['"]|['"]$)/g, '')
|
if (value.startsWith("'") || value.startsWith('"')) {
|
||||||
.replace(/\\n/g, '\n')
|
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
|
// Convert string to JS type if appropriate
|
||||||
if (value !== '' && !isNaN(+value)) {
|
if (value !== '' && !isNaN(+value)) {
|
||||||
|
|||||||
@ -96,6 +96,36 @@ describe('parseEnvVars', (): void => {
|
|||||||
assert(envVars.NODE_ENV === 'dev')
|
assert(envVars.NODE_ENV === 'dev')
|
||||||
assert(envVars.ANSWER === ' 42 AND COUNTING')
|
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 => {
|
describe('parseEnvString', (): void => {
|
||||||
@ -172,6 +202,14 @@ describe('getEnvFileVars', (): void => {
|
|||||||
ONLY: 'IN=PRODUCTION',
|
ONLY: 'IN=PRODUCTION',
|
||||||
GALAXY: 'hitch\nhiking',
|
GALAXY: 'hitch\nhiking',
|
||||||
BRINGATOWEL: true,
|
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 ",
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -3,3 +3,14 @@ ANSWER=42
|
|||||||
ONLY= "IN=PRODUCTION"
|
ONLY= "IN=PRODUCTION"
|
||||||
GALAXY="hitch\nhiking"
|
GALAXY="hitch\nhiking"
|
||||||
BRINGATOWEL=true
|
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…
x
Reference in New Issue
Block a user