Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nic-35 add targetEnvKey option #36

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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