Skip to content

Commit

Permalink
Merge pull request #36 from vordgi/nic-35
Browse files Browse the repository at this point in the history
nic-35 add targetEnvKey option
  • Loading branch information
vordgi committed Apr 23, 2024
2 parents 955ab1d + 66f4330 commit 99cf26e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 5 additions & 1 deletion package/src/lib/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 14 additions & 1 deletion package/src/with-nimpl-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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;
};
};
Expand Down

0 comments on commit 99cf26e

Please sign in to comment.