Skip to content

Commit 93691d6

Browse files
committed
feat: handle multilines in env files
continues #974
1 parent 759dcf6 commit 93691d6

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

.size-limit.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{
1010
"name": "zx/index",
1111
"path": "build/*.{js,cjs}",
12-
"limit": "805 kB",
12+
"limit": "805.5 kB",
1313
"brotli": false,
1414
"gzip": false
1515
},
@@ -30,7 +30,7 @@
3030
{
3131
"name": "all",
3232
"path": "build/*",
33-
"limit": "842 kB",
33+
"limit": "842.5 kB",
3434
"brotli": false,
3535
"gzip": false
3636
}

src/util.ts

+34-9
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,40 @@ export const toCamelCase = (str: string) =>
358358
export const parseBool = (v: string): boolean | string =>
359359
({ true: true, false: false })[v] ?? v
360360

361-
export const parseDotenv = (content: string): NodeJS.ProcessEnv =>
362-
content.split(/\r?\n/).reduce<NodeJS.ProcessEnv>((r, line) => {
363-
if (line.startsWith('export ')) line = line.slice(7)
364-
const i = line.indexOf('=')
365-
const k = line.slice(0, i).trim()
366-
const v = line.slice(i + 1).trim()
367-
if (k && v) r[k] = v
368-
return r
369-
}, {})
361+
// prettier-ignore
362+
export const parseDotenv = (content: string): NodeJS.ProcessEnv => {
363+
const e: Record<string, string> = {}
364+
let k = ''
365+
let c = ''
366+
let q = ''
367+
const cap = () => {if (c && k) { e[k] = c; c = ''; k = '' }}
368+
369+
for (const s of content) {
370+
if (s === ' ' && !q) {
371+
if (!k && c === 'export') c = ''
372+
continue
373+
}
374+
if (s === '=' && !q) {
375+
if (!k) { k = c; c = ''; continue }
376+
}
377+
if (s === '\n' && !q) {
378+
cap()
379+
continue
380+
}
381+
if (s === '"' || s === "'") {
382+
if (q === s) {
383+
q = ''
384+
cap()
385+
continue
386+
}
387+
q = s
388+
continue
389+
}
390+
c += s
391+
}
392+
393+
return e
394+
}
370395

371396
export const readEnvFromFile = (
372397
filepath: string,

test/util.test.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,24 @@ describe('util', () => {
143143
})
144144

145145
test('parseDotenv()', () => {
146-
assert.deepEqual(
147-
parseDotenv('ENV=v1\nENV2=v2\n\n\n ENV3 = v3 \nexport ENV4=v4'),
148-
{
149-
ENV: 'v1',
150-
ENV2: 'v2',
151-
ENV3: 'v3',
152-
ENV4: 'v4',
153-
}
154-
)
155-
assert.deepEqual(parseDotenv(''), {})
156-
157-
// TBD: multiline
158146
const multiline = `SIMPLE=xyz123
159147
NON_INTERPOLATED='raw text without variable interpolation'
160148
MULTILINE = """
161149
long text here,
162150
e.g. a private SSH key
163-
"""`
151+
"""
152+
ENV=v1\nENV2=v2\n\n\n ENV3 = v3 \n export ENV4=v4
153+
`
154+
155+
assert.deepEqual(parseDotenv(multiline), {
156+
SIMPLE: 'xyz123',
157+
NON_INTERPOLATED: 'raw text without variable interpolation',
158+
MULTILINE: '\nlong text here,\ne.g. a private SSH key\n',
159+
ENV: 'v1',
160+
ENV2: 'v2',
161+
ENV3: 'v3',
162+
ENV4: 'v4',
163+
})
164164
})
165165

166166
describe('readEnvFromFile()', () => {

0 commit comments

Comments
 (0)