Skip to content

Commit

Permalink
#5 Add unproven function for programmatic injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Kirmas committed Oct 18, 2021
1 parent daff296 commit 243f9ba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import { readFileSync } from "fs"
import { main } from "./main"
import { assigner, main, Reader } from "./main"

const {assign: $assign} = Object

export { parse } from "./parse"
export { argEnv }
export default argEnv

/**
* **NB!** args, package and ENV are already picked
* Revert precedence
* @param {string[]} paths
* @param {*} [env] `= process.env`
* @param {*} [target] `= {}`
* @param {*} [reader] `= readFileSync`
*/
// TODO #4 Like `path.unshift(".env")`
// TODO Move to a separate module
function argEnv<T extends Record<string, unknown>>(paths: string[], env = process.env, target = {} as T, reader = readFileSync as Reader) {
assigner(env, paths, reader, target)
$assign(env, target)

return target
}

main(
process.env,
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function assigner(
const {length} = files

for (let i = length; i--; )
// NB commutative
$assign(
envPatch,
parse(
Expand Down

0 comments on commit 243f9ba

Please sign in to comment.