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

File tree

13 files changed

+48
-57
lines changed

13 files changed

+48
-57
lines changed

.size-limit.json

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 44 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)