Skip to content

Commit

Permalink
feat: envName config
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Mar 13, 2023
1 parent ec03711 commit 4a0227d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ Custom [unjs/jiti](https://github.com/unjs/jiti) instance used to import configu

Custom [unjs/jiti](https://github.com/unjs/jiti) options to import configuration files.

### `envName`

Environment name used for [environment specific configuration](#environment-specific-configuration).

Default is `process.env.NODE_ENV`. You can set `envName` to `false` or an empty string to disable the feature.

## Extending configuration

If resolved config contains a `extends` key, it will be used to extend configuration.
Expand Down Expand Up @@ -206,7 +212,7 @@ Users can define environment specific configuration using those config keys:
- `$production: {...}`
- `$env: { [env]: {...} }`

c12 matches `$envName` or `NODE_ENV` environment variable to the env config and overrides it.
c12 matches [`envName`](#envname) confi the env config and overrides it.

**Note:** Environment will be applied when extending each configuration layer. This way layers can provide environment specific configuration.

Expand Down
19 changes: 11 additions & 8 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface ConfigLayerMeta {
}

export interface C12InputConfig {
$envName?: string;
$test?: UserInputConfig;
$development?: UserInputConfig;
$production?: UserInputConfig;
Expand Down Expand Up @@ -63,6 +62,8 @@ export interface LoadConfigOptions<T extends InputConfig = InputConfig> {

dotenv?: boolean | DotenvOptions;

envName?: string | false;

packageJson?: boolean | string | string[];

defaults?: T;
Expand Down Expand Up @@ -90,6 +91,7 @@ export async function loadConfig<T extends InputConfig = InputConfig>(
// Normalize options
options.cwd = resolve(process.cwd(), options.cwd || ".");
options.name = options.name || "config";
options.envName = options.envName ?? process.env.NODE_ENV;
options.configFile =
options.configFile ??
(options.name !== "config" ? `${options.name}.config` : "config");
Expand Down Expand Up @@ -332,13 +334,14 @@ async function resolveConfig(
}

// Extend env specific config
const envName = res.config.$envName || process.env.NODE_ENV || "default";
const envConfig = {
...res.config["$" + envName],
...res.config.$env?.[envName],
};
if (Object.keys(envConfig).length > 0) {
res.config = defu(envConfig, res.config);
if (options.envName) {
const envConfig = {
...res.config["$" + options.envName],
...res.config.$env?.[options.envName],
};
if (Object.keys(envConfig).length > 0) {
res.config = defu(envConfig, res.config);
}
}

// Meta
Expand Down
1 change: 1 addition & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("c12", () => {
dotenv: true,
packageJson: ["c12", "c12-alt"],
globalRc: true,
envName: "test",
extend: {
extendKey: ["theme", "extends"],
},
Expand Down

0 comments on commit 4a0227d

Please sign in to comment.