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 populate API #3

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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: 4 additions & 2 deletions src/main/ts/envapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export const loadSafe = (...files: string[]): NodeJS.ProcessEnv =>
...files
)

export const config = (def = DOTENV, ...files: string[]): NodeJS.ProcessEnv =>
Object.assign(process.env, loadSafe(def, ...files))
export const populate = (env: NodeJS.ProcessEnv, extra?: NodeJS.ProcessEnv): NodeJS.ProcessEnv =>
Object.assign(env, extra)

export const config = (def = DOTENV, ...files: string[]): NodeJS.ProcessEnv =>
populate(process.env, loadSafe(def, ...files))
8 changes: 7 additions & 1 deletion src/test/ts/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { describe, test, afterAll } from 'vitest'
import { parse, stringify, load, loadSafe, config } from '../../main/ts/index.js'
import { parse, stringify, load, loadSafe, populate, config } from '../../main/ts/index.js'

const randomId= () => Math.random().toString(36).slice(2)
const tempdir = (prefix: string = `temp-${randomId()}`): string => {
Expand Down Expand Up @@ -126,4 +126,10 @@ JSONSTR='{"foo": "b a r"}'`
delete process.env.ENV1
})
})

describe('populate()', () => {
test('populates env', () => {
assert.deepEqual(populate({FOO: 'BAR'}, { FOO: 'BAZ' }), { FOO: 'BAZ' })
})
})
})
Loading