Skip to content

Commit 759dcf6

Browse files
authored
fix: parseDotenv tweak ups (#1030)
1 parent 36b42d8 commit 759dcf6

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

docs/cli.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,14 @@ zx --cwd=/foo/bar script.mjs
119119
```
120120

121121
## --env
122-
Specify a env file.
122+
Specify an env file.
123123

124124
```bash
125125
zx --env=/path/to/some.env script.mjs
126126
```
127127

128-
When cwd option is specified, it will used as base path: `--cwd='/foo/bar' --env='../.env'``/foo/.env`
129-
130-
```bash
131-
zx --cwd=/foo/bar --env=/path/to/some.env script.mjs
132-
```
128+
When `cwd` option is specified, it will be used as base path:
129+
`--cwd='/foo/bar' --env='../.env'``/foo/.env`
133130

134131
## --ext
135132

src/util.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,15 @@ 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-
return content.split(/\r?\n/).reduce<NodeJS.ProcessEnv>((r, line) => {
363-
const [k] = line.trim().split('=', 1)
364-
const v = line.trim().slice(k.length + 1)
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()
365367
if (k && v) r[k] = v
366368
return r
367369
}, {})
368-
}
369370

370371
export const readEnvFromFile = (
371372
filepath: string,

test/util.test.js

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

145145
test('parseDotenv()', () => {
146-
assert.deepEqual(parseDotenv('ENV=value1\nENV2=value24'), {
147-
ENV: 'value1',
148-
ENV2: 'value24',
149-
})
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+
)
150155
assert.deepEqual(parseDotenv(''), {})
156+
157+
// TBD: multiline
158+
const multiline = `SIMPLE=xyz123
159+
NON_INTERPOLATED='raw text without variable interpolation'
160+
MULTILINE = """
161+
long text here,
162+
e.g. a private SSH key
163+
"""`
151164
})
152165

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

0 commit comments

Comments
 (0)