From 66f4330bb461eaaa393d8c59236211b43aede170 Mon Sep 17 00:00:00 2001 From: Vordgi Date: Tue, 23 Apr 2024 21:44:39 +0400 Subject: [PATCH] nic-35 add targetEnvKey option --- package/src/lib/get-config.ts | 6 +++++- package/src/with-nimpl-config.ts | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/package/src/lib/get-config.ts b/package/src/lib/get-config.ts index 0c41334..974c096 100644 --- a/package/src/lib/get-config.ts +++ b/package/src/lib/get-config.ts @@ -10,7 +10,11 @@ type GetConfigParam = { const getConfig = async ({ variant, env, configFolder }: GetConfigParam) => { const variantFolder = getConfigVariantFolder(variant, configFolder); - const configs = await findConfigs(variantFolder, env || process.env.NIMPL_CONFIG_ENV || process.env.NODE_ENV); + const configEnvKey = process.env.NIMPL_CONFIG_ENV_KEY; + const configs = await findConfigs( + variantFolder, + env || (configEnvKey && process.env[configEnvKey]) || process.env.NIMPL_CONFIG_ENV || process.env.NODE_ENV, + ); if (!configs) return null; diff --git a/package/src/with-nimpl-config.ts b/package/src/with-nimpl-config.ts index d551eb8..73b3416 100644 --- a/package/src/with-nimpl-config.ts +++ b/package/src/with-nimpl-config.ts @@ -3,17 +3,27 @@ import getBuildConfig from "./lib/get-build-config"; import getPostbuildConfig from "./lib/get-postbuild-config"; type NimplConfigParam = { + /** List of possible environment variables */ envs?: string[]; + /** The current environment variable that will be used for [Environment-dependent config](https://nimpl.tech/config/configuration#environment-dependent-config) */ targetEnv?: string; + /** The key of the environment variable that needs to be used for [Environment-dependent config](https://nimpl.tech/config/configuration#environment-dependent-config) */ + targetEnvKey?: string; + /** Path to the directory with configs */ folder?: string; }; -const nimplConfig = ({ envs = [], targetEnv, folder }: NimplConfigParam) => { +const nimplConfig = ({ envs = [], targetEnv, targetEnvKey, folder }: NimplConfigParam) => { if (targetEnv && !envs.includes(targetEnv)) { console.log( `@nimpl/config: an unknown env was passed (${targetEnv}), the allowed ones were: [${envs.join(", ")}]`, ); } + if (targetEnvKey && (!process.env[targetEnvKey] || !envs.includes(process.env[targetEnvKey]))) { + console.log( + `@nimpl/config: Failed to get the allowed env by the targetEnvKey (process.env["${targetEnvKey}"]=${process.env[targetEnvKey]}), the allowed ones were: [${envs.join(", ")}]`, + ); + } configurePackageTypes(folder || "config"); @@ -32,6 +42,9 @@ const nimplConfig = ({ envs = [], targetEnv, folder }: NimplConfigParam) => { if (targetEnv) { nextConfig.env.NIMPL_CONFIG_ENV = targetEnv; } + if (targetEnvKey) { + nextConfig.env.NIMPL_CONFIG_ENV_KEY = targetEnvKey; + } return nextConfig; }; };