Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose dotenv API #1032

Merged
merged 16 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"name": "zx/index",
"path": "build/*.{js,cjs}",
"limit": "805 kB",
"limit": "806 kB",
"brotli": false,
"gzip": false
},
Expand All @@ -23,14 +23,14 @@
{
"name": "vendor",
"path": "build/vendor-*",
"limit": "761 kB",
"limit": "762 kB",
"brotli": false,
"gzip": false
},
{
"name": "all",
"path": "build/*",
"limit": "842 kB",
"limit": "843 kB",
"brotli": false,
"gzip": false
}
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"create-require": "^1.1.1",
"depseek": "^0.4.1",
"dts-bundle-generator": "^9.5.1",
"envapi": "^0.0.0",
"esbuild": "^0.24.2",
"esbuild-node-externals": "^1.16.0",
"esbuild-plugin-entry-chunks": "^0.1.15",
Expand Down
1 change: 1 addition & 0 deletions scripts/build-dts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const entries = [
'@webpod/ingrid',
'chalk',
'zurk',
'envapi',
], // args['external-inlines'],
},
output,
Expand Down
19 changes: 7 additions & 12 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
import os from 'node:os'
import path from 'node:path'
import fs from 'node:fs'
import { chalk, type RequestInfo, type RequestInit } from './vendor-core.js'
import {
chalk,
type RequestInfo,
type RequestInit,
parseDotenv,
} from './vendor-core.js'
import { inspect } from 'node:util'

export { isStringLiteral } from './vendor-core.js'
export { isStringLiteral, parseDotenv } from './vendor-core.js'

export function tempdir(prefix: string = `zx-${randomId()}`): string {
const dirpath = path.join(os.tmpdir(), prefix)
Expand Down Expand Up @@ -358,16 +363,6 @@ export const toCamelCase = (str: string) =>
export const parseBool = (v: string): boolean | string =>
({ true: true, false: false })[v] ?? v

export const parseDotenv = (content: string): NodeJS.ProcessEnv =>
content.split(/\r?\n/).reduce<NodeJS.ProcessEnv>((r, line) => {
if (line.startsWith('export ')) line = line.slice(7)
const i = line.indexOf('=')
const k = line.slice(0, i).trim()
const v = line.slice(i + 1).trim()
if (k && v) r[k] = v
return r
}, {})

export const readEnvFromFile = (
filepath: string,
env: NodeJS.ProcessEnv = process.env
Expand Down
1 change: 1 addition & 0 deletions src/vendor-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export type RequestInit = Parameters<typeof globalThis.fetch>[1]
export { default as chalk, type ChalkInstance } from 'chalk'
export { default as which } from 'which'
export { default as ps } from '@webpod/ps'
export { parse as parseDotenv } from 'envapi'
2 changes: 1 addition & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('cli', () => {
assert.ok(p.stderr.endsWith(cwd + '\n'))
})

test('supports `--env` options with file', async () => {
test('supports `--env` option', async () => {
const env = tmpfile(
'.env',
`FOO=BAR
Expand Down
71 changes: 40 additions & 31 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,44 +140,53 @@ describe('util', () => {
assert.equal(toCamelCase('SOME_MORE_BIG_STR'), 'someMoreBigStr')
assert.equal(toCamelCase('kebab-input-str'), 'kebabInputStr')
})
})

test('parseDotenv()', () => {
assert.deepEqual(
parseDotenv('ENV=v1\nENV2=v2\n\n\n ENV3 = v3 \nexport ENV4=v4'),
{
test.only('parseDotenv()', () => {
const multiline = `SIMPLE=xyz123
# comment ###
NON_INTERPOLATED='raw text without variable interpolation'
MULTILINE = """
long text here, # not-comment
e.g. a private SSH key
"""
ENV=v1\nENV2=v2\n\n\n\t\t ENV3 = v3 \n export ENV4=v4
ENV5=v5 # comment
`

assert.deepEqual(parseDotenv(multiline), {
SIMPLE: 'xyz123',
NON_INTERPOLATED: 'raw text without variable interpolation',
MULTILINE: 'long text here, # not-comment\ne.g. a private SSH key',
ENV: 'v1',
ENV2: 'v2',
ENV3: 'v3',
ENV4: 'v4',
}
)
assert.deepEqual(parseDotenv(''), {})
ENV5: 'v5',
})

// TBD: multiline
const multiline = `SIMPLE=xyz123
NON_INTERPOLATED='raw text without variable interpolation'
MULTILINE = """
long text here,
e.g. a private SSH key
"""`
})

describe('readEnvFromFile()', () => {
test('handles correct proccess.env', () => {
const file = tempfile('.env', 'ENV=value1\nENV2=value24')
const env = readEnvFromFile(file)
assert.equal(env.ENV, 'value1')
assert.equal(env.ENV2, 'value24')
assert.ok(env.NODE_VERSION !== '')
assert.deepEqual(
parseDotenv(`FOO=BAR
BAR=FOO+`),
{ FOO: 'BAR', BAR: 'FOO+' }
)
})

test('handles correct some env', () => {
const file = tempfile('.env', 'ENV=value1\nENV2=value24')
const env = readEnvFromFile(file, { version: '1.0.0', name: 'zx' })
assert.equal(env.ENV, 'value1')
assert.equal(env.ENV2, 'value24')
assert.equal(env.version, '1.0.0')
assert.equal(env.name, 'zx')
describe('readEnvFromFile()', () => {
test('handles correct proccess.env', () => {
const file = tempfile('.env', 'ENV=value1\nENV2=value24')
const env = readEnvFromFile(file)
assert.equal(env.ENV, 'value1')
assert.equal(env.ENV2, 'value24')
assert.ok(env.NODE_VERSION !== '')
})

test('handles correct some env', () => {
const file = tempfile('.env', 'ENV=value1\nENV2=value24')
const env = readEnvFromFile(file, { version: '1.0.0', name: 'zx' })
assert.equal(env.ENV, 'value1')
assert.equal(env.ENV2, 'value24')
assert.equal(env.version, '1.0.0')
assert.equal(env.name, 'zx')
})
})
})
Loading