Skip to content

Commit

Permalink
#3 Integrate ENV_FILE to main. Comment #13 application
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Kirmas committed Oct 18, 2021
1 parent e3343ff commit e2372bd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/cli-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe(fromArgs.name, () => {
], false)).toStrictEqual([
]))

it("1", () => expect(fromArgs([
it("duplicated", () => expect(fromArgs([
'bin/node',
'script',
'--env-file=xxx yyy',
Expand Down
6 changes: 3 additions & 3 deletions src/cli-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ function fromArgs<T extends boolean>(
/**
* @todo Consider calculation as in compose.yml
*/
function fromPackageEnv(env: Env) {
function fromPackageEnv(env: Readonly<Env>) {
return fromArrayAsAssoc(
env,
packagePrefix,
env[packagePrefix]?.split(npm7delimiter) ?? []
)
}

function fromEnv(env: Env) {
function fromEnv(env: Readonly<Env>) {
return fromArrayAsAssoc(
env,
envPrefix,
Expand All @@ -78,7 +78,7 @@ function fromEnv(env: Env) {
}

// CONSIDER `collected: Set<string>`
function fromArrayAsAssoc(env: Env, prefix: string, collected: string[]) {
function fromArrayAsAssoc(env: Readonly<Env>, prefix: string, collected: string[]) {
let i = 0
, key = `${prefix}_${i}`

Expand Down
23 changes: 22 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
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
* @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
20 changes: 16 additions & 4 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ import { join } from "../research/utils"
import { main } from "./main"

describe(main.name, () => {
it("1", () => {
it("demo", () => {
const files: Record<string, string> = {
"npm0": "OVERWRITE=true",
"npm1": "NPM1=Loaded",
"arg0": "ARG0=Loaded",
"arg1": "ARG1=Loaded",
"env": "ENV=Loaded",
"env0": "ENV0=Loaded",
"env1": "ENV1=Loaded"
}
, env = {
"npm_package_config_env_file_0": "npm0",
"npm_package_config_env_file_1": "npm1",
"ENV_FILE": "env",
"ENV_FILE_0": "env0",
"ENV_FILE_1": "env1",
"OVERWRITE": "false"
}
, argv = ["node", "script", "--env-file=arg0", "--env-file=arg1"]
Expand All @@ -24,12 +30,18 @@ describe(main.name, () => {
)

expect(env).toStrictEqual({
"npm_package_config_env_file_0": "npm0",
"npm_package_config_env_file_1": "npm1",
"NPM1": "Loaded",
"ARG0": "Loaded",
"ARG1": "Loaded",
"OVERWRITE": "false"
"ENV": "Loaded",
"ENV0": "Loaded",
"ENV1": "Loaded",
"OVERWRITE": "false",
"npm_package_config_env_file_0": "npm0",
"npm_package_config_env_file_1": "npm1",
"ENV_FILE": "env",
"ENV_FILE_0": "env0",
"ENV_FILE_1": "env1"
})
expect(argv).toStrictEqual([
"node", "script"
Expand Down
37 changes: 20 additions & 17 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import {
fromArgs,
fromEnv,
fromPackageEnv
} from "./cli-utils"
import {
parse
} from "./parse"

type Reader = (path: string) => string | Buffer
export type Reader = (path: string) => string | Buffer

const {assign: $assign} = Object

export {
main
main,
assigner
}

function main(
Expand All @@ -20,30 +22,31 @@ function main(
reader: Reader,
deleteArgs: boolean
) {
const envPatch: Record<string, unknown> = {}

assigner(
env,
// TODO #13
const patches = [
fromEnv(env),
fromArgs(argv, deleteArgs),
reader,
envPatch
)
fromPackageEnv(env)
]
, {length} = patches
, envPatch: Record<string, unknown> = {}

assigner(
env,
fromPackageEnv(env),
reader,
envPatch
)
for (let i = 0; i < length; i++)
assigner(
env,
patches[i],
reader,
envPatch
)

$assign(env, envPatch)

return envPatch
}

function assigner(
env: Env,
files: string[],
env: Readonly<Env>,
files: readonly string[],
reader: Reader,
envPatch: Record<string, unknown>
) {
Expand Down
1 change: 1 addition & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function parse<K extends string>(
reserved: undefined | Record<string, unknown>
): Record<K, string> {
// TODO Line
// TODO Emit good error for bad `src`
const source = typeof src === "string" ? src : src.toString()
, $return = {} as Record<string, string>
, replacer = (_: string, variable: string, __: string, $default = "") =>
Expand Down

0 comments on commit e2372bd

Please sign in to comment.