Skip to content

Commit

Permalink
feat: expose populate API
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jan 6, 2025
1 parent a75bd98 commit 2e54df3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
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' })
})
})
})

0 comments on commit 2e54df3

Please sign in to comment.