From e2372bd5b9ead39054d8c4b002518bc15429eb76 Mon Sep 17 00:00:00 2001 From: Andrii Kirmas Date: Tue, 19 Oct 2021 01:21:58 +0300 Subject: [PATCH] #3 Integrate `ENV_FILE` to `main`. Comment #13 application --- src/cli-utils.test.ts | 2 +- src/cli-utils.ts | 6 +++--- src/index.ts | 23 ++++++++++++++++++++++- src/main.test.ts | 20 ++++++++++++++++---- src/main.ts | 37 ++++++++++++++++++++----------------- src/parse.ts | 1 + 6 files changed, 63 insertions(+), 26 deletions(-) diff --git a/src/cli-utils.test.ts b/src/cli-utils.test.ts index cf7d7c3..c502152 100644 --- a/src/cli-utils.test.ts +++ b/src/cli-utils.test.ts @@ -17,7 +17,7 @@ describe(fromArgs.name, () => { ], false)).toStrictEqual([ ])) - it("1", () => expect(fromArgs([ + it("duplicated", () => expect(fromArgs([ 'bin/node', 'script', '--env-file=xxx yyy', diff --git a/src/cli-utils.ts b/src/cli-utils.ts index 9c68b6f..82f9d34 100644 --- a/src/cli-utils.ts +++ b/src/cli-utils.ts @@ -59,7 +59,7 @@ function fromArgs( /** * @todo Consider calculation as in compose.yml */ -function fromPackageEnv(env: Env) { +function fromPackageEnv(env: Readonly) { return fromArrayAsAssoc( env, packagePrefix, @@ -67,7 +67,7 @@ function fromPackageEnv(env: Env) { ) } -function fromEnv(env: Env) { +function fromEnv(env: Readonly) { return fromArrayAsAssoc( env, envPrefix, @@ -78,7 +78,7 @@ function fromEnv(env: Env) { } // CONSIDER `collected: Set` -function fromArrayAsAssoc(env: Env, prefix: string, collected: string[]) { +function fromArrayAsAssoc(env: Readonly, prefix: string, collected: string[]) { let i = 0 , key = `${prefix}_${i}` diff --git a/src/index.ts b/src/index.ts index 9211e0a..1b4c679 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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>(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, diff --git a/src/main.test.ts b/src/main.test.ts index 4c4bd88..803f5b8 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -2,16 +2,22 @@ import { join } from "../research/utils" import { main } from "./main" describe(main.name, () => { - it("1", () => { + it("demo", () => { const files: Record = { "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"] @@ -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" diff --git a/src/main.ts b/src/main.ts index ed7cc80..5a90893 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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( @@ -20,21 +22,22 @@ function main( reader: Reader, deleteArgs: boolean ) { - const envPatch: Record = {} - - assigner( - env, + // TODO #13 + const patches = [ + fromEnv(env), fromArgs(argv, deleteArgs), - reader, - envPatch - ) + fromPackageEnv(env) + ] + , {length} = patches + , envPatch: Record = {} - assigner( - env, - fromPackageEnv(env), - reader, - envPatch - ) + for (let i = 0; i < length; i++) + assigner( + env, + patches[i], + reader, + envPatch + ) $assign(env, envPatch) @@ -42,8 +45,8 @@ function main( } function assigner( - env: Env, - files: string[], + env: Readonly, + files: readonly string[], reader: Reader, envPatch: Record ) { diff --git a/src/parse.ts b/src/parse.ts index b8931bb..42bc3dc 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -14,6 +14,7 @@ function parse( reserved: undefined | Record ): Record { // TODO Line + // TODO Emit good error for bad `src` const source = typeof src === "string" ? src : src.toString() , $return = {} as Record , replacer = (_: string, variable: string, __: string, $default = "") =>