Skip to content

Commit 109205b

Browse files
authored
feat: expose dotenv API (#1032)
* feat: handle multilines in env files continues #974 * fix: check donenv names * fix: handle dotenv comments * fix: handle tabs in dotenvs * fix: handle backtick in dotenv * chore: parseDotenv tweak ups * chore: shrink a few bytes * chore: dotenv parse imprs * chore: move custom dotenv parser to external pkg * chore: linting * chore: rebase * feat: reexport `envapi`
1 parent f1ca807 commit 109205b

13 files changed

+48
-57
lines changed

.size-limit.json

+3-3
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": "806 kB",
1313
"brotli": false,
1414
"gzip": false
1515
},
@@ -23,14 +23,14 @@
2323
{
2424
"name": "vendor",
2525
"path": "build/vendor-*",
26-
"limit": "761 kB",
26+
"limit": "763 kB",
2727
"brotli": false,
2828
"gzip": false
2929
},
3030
{
3131
"name": "all",
3232
"path": "build/*",
33-
"limit": "842 kB",
33+
"limit": "844 kB",
3434
"brotli": false,
3535
"gzip": false
3636
}

docs/api.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ console.log(YAML.parse('foo: bar').foo)
366366
```
367367

368368
## dotenv
369-
[dotenv](https://www.npmjs.com/package/dotenv)-like environment variables loading API
369+
The [envapi](https://www.npmjs.com/package/envapi) package.
370+
An API to interact with environment vars in [dotenv](https://www.npmjs.com/package/dotenv) format.
370371

371372
```js
372373
// parse

package-lock.json

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"create-require": "^1.1.1",
109109
"depseek": "^0.4.1",
110110
"dts-bundle-generator": "^9.5.1",
111+
"envapi": "^0.1.0",
111112
"esbuild": "^0.24.2",
112113
"esbuild-node-externals": "^1.16.0",
113114
"esbuild-plugin-entry-chunks": "^0.1.15",

scripts/build-dts.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const entries = [
4545
// '@webpod/ps',
4646
'@webpod/ingrid',
4747
'depseek',
48+
'envapi',
4849
], // args['external-inlines'],
4950
},
5051
output,

scripts/build-tests.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const modules = [
2626
['core', core],
2727
['cli', cli],
2828
['index', index],
29-
['vendor', vendor, ['chalk', 'depseek', 'fs', 'glob', 'minimist', 'ps', 'which', 'YAML',]],
29+
['vendor', vendor, ['chalk', 'depseek', 'dotenv', 'fs', 'glob', 'minimist', 'ps', 'which', 'YAML',]],
3030
]
3131
const root = path.resolve(new URL(import.meta.url).pathname, '../..')
3232
const filePath = path.resolve(root, `test/export.test.js`)

src/goods.ts

-44
Original file line numberDiff line numberDiff line change
@@ -219,47 +219,3 @@ export async function spinner<T>(
219219
}
220220
})
221221
}
222-
223-
/**
224-
* Read env files and collects it into environment variables
225-
*/
226-
export const dotenv = (() => {
227-
const parse = (content: string | Buffer): NodeJS.ProcessEnv =>
228-
content
229-
.toString()
230-
.split(/\r?\n/)
231-
.reduce<NodeJS.ProcessEnv>((r, line) => {
232-
if (line.startsWith('export ')) line = line.slice(7)
233-
const i = line.indexOf('=')
234-
const k = line.slice(0, i).trim()
235-
const v = line.slice(i + 1).trim()
236-
if (k && v) r[k] = v
237-
return r
238-
}, {})
239-
240-
const _load = (
241-
read: (file: string) => string,
242-
...files: string[]
243-
): NodeJS.ProcessEnv =>
244-
files
245-
.reverse()
246-
.reduce((m, f) => Object.assign(m, parse(read(path.resolve(f)))), {})
247-
const load = (...files: string[]): NodeJS.ProcessEnv =>
248-
_load((file) => fs.readFileSync(file, 'utf8'), ...files)
249-
const loadSafe = (...files: string[]): NodeJS.ProcessEnv =>
250-
_load(
251-
(file: string): string =>
252-
fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : '',
253-
...files
254-
)
255-
256-
const config = (def = '.env', ...files: string[]): NodeJS.ProcessEnv =>
257-
Object.assign(process.env, loadSafe(def, ...files))
258-
259-
return {
260-
parse,
261-
load,
262-
loadSafe,
263-
config,
264-
}
265-
})()

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export * from './goods.js'
2020
export {
2121
minimist,
2222
chalk,
23+
dotenv,
2324
fs,
2425
which,
2526
YAML,

src/vendor-extra.ts

+1
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,4 @@ export const fs: typeof import('fs-extra') = _fs
116116

117117
export { depseekSync as depseek } from 'depseek'
118118
export { default as minimist } from 'minimist'
119+
export { default as dotenv } from 'envapi'

test/cli.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('cli', () => {
137137
assert.ok(p.stderr.endsWith(cwd + '\n'))
138138
})
139139

140-
test('supports `--env` options with file', async () => {
140+
test('supports `--env` option', async () => {
141141
const env = tmpfile(
142142
'.env',
143143
`FOO=BAR

test/export.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ describe('index', () => {
159159
assert.equal(typeof index.dotenv.load, 'function', 'index.dotenv.load')
160160
assert.equal(typeof index.dotenv.loadSafe, 'function', 'index.dotenv.loadSafe')
161161
assert.equal(typeof index.dotenv.parse, 'function', 'index.dotenv.parse')
162+
assert.equal(typeof index.dotenv.stringify, 'function', 'index.dotenv.stringify')
162163
assert.equal(typeof index.echo, 'function', 'index.echo')
163164
assert.equal(typeof index.expBackoff, 'function', 'index.expBackoff')
164165
assert.equal(typeof index.fetch, 'function', 'index.fetch')
@@ -420,6 +421,12 @@ describe('vendor', () => {
420421
assert.equal(typeof vendor.chalk, 'function', 'vendor.chalk')
421422
assert.equal(typeof vendor.chalk.level, 'number', 'vendor.chalk.level')
422423
assert.equal(typeof vendor.depseek, 'function', 'vendor.depseek')
424+
assert.equal(typeof vendor.dotenv, 'object', 'vendor.dotenv')
425+
assert.equal(typeof vendor.dotenv.config, 'function', 'vendor.dotenv.config')
426+
assert.equal(typeof vendor.dotenv.load, 'function', 'vendor.dotenv.load')
427+
assert.equal(typeof vendor.dotenv.loadSafe, 'function', 'vendor.dotenv.loadSafe')
428+
assert.equal(typeof vendor.dotenv.parse, 'function', 'vendor.dotenv.parse')
429+
assert.equal(typeof vendor.dotenv.stringify, 'function', 'vendor.dotenv.stringify')
423430
assert.equal(typeof vendor.fs, 'object', 'vendor.fs')
424431
assert.equal(typeof vendor.fs.Dir, 'function', 'vendor.fs.Dir')
425432
assert.equal(typeof vendor.fs.Dirent, 'function', 'vendor.fs.Dirent')

test/goods.test.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
import assert from 'node:assert'
1616
import { test, describe, after } from 'node:test'
17-
import { $, chalk, fs, tempfile } from '../build/index.js'
18-
import { echo, sleep, parseArgv, dotenv } from '../build/goods.js'
17+
import { $, chalk, fs, tempfile, dotenv } from '../build/index.js'
18+
import { echo, sleep, parseArgv } from '../build/goods.js'
1919

2020
describe('goods', () => {
2121
function zx(script) {
@@ -176,6 +176,7 @@ describe('goods', () => {
176176

177177
describe('dotenv', () => {
178178
test('parse()', () => {
179+
assert.deepEqual(dotenv.parse(''), {})
179180
assert.deepEqual(
180181
dotenv.parse('ENV=v1\nENV2=v2\n\n\n ENV3 = v3 \nexport ENV4=v4'),
181182
{
@@ -185,15 +186,27 @@ describe('goods', () => {
185186
ENV4: 'v4',
186187
}
187188
)
188-
assert.deepEqual(dotenv.parse(''), {})
189189

190-
// TBD: multiline
191190
const multiline = `SIMPLE=xyz123
192-
NON_INTERPOLATED='raw text without variable interpolation'
191+
# comment ###
192+
NON_INTERPOLATED='raw text without variable interpolation'
193193
MULTILINE = """
194-
long text here,
194+
long text here, # not-comment
195195
e.g. a private SSH key
196-
"""`
196+
"""
197+
ENV=v1\nENV2=v2\n\n\n\t\t ENV3 = v3 \n export ENV4=v4
198+
ENV5=v5 # comment
199+
`
200+
assert.deepEqual(dotenv.parse(multiline), {
201+
SIMPLE: 'xyz123',
202+
NON_INTERPOLATED: 'raw text without variable interpolation',
203+
MULTILINE: 'long text here, # not-comment\ne.g. a private SSH key',
204+
ENV: 'v1',
205+
ENV2: 'v2',
206+
ENV3: 'v3',
207+
ENV4: 'v4',
208+
ENV5: 'v5',
209+
})
197210
})
198211

199212
describe('load()', () => {

test/index.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
ProcessOutput,
3131
ProcessPromise,
3232
defaults,
33+
dotenv,
3334
minimist,
3435
chalk,
3536
fs,
@@ -106,6 +107,7 @@ describe('index', () => {
106107
assert(which)
107108
assert(YAML)
108109
assert(ps)
110+
assert(dotenv)
109111

110112
// utils
111113
assert(quote)

0 commit comments

Comments
 (0)