Comparing version 1.1.2 to 1.2.0
@@ -40,6 +40,26 @@ import { JITI } from 'jiti'; | ||
interface InputConfig extends Record<string, any> { | ||
type UserInputConfig = Record<string, any>; | ||
interface ConfigLayerMeta { | ||
name?: string; | ||
[key: string]: any; | ||
} | ||
interface C12InputConfig { | ||
$test?: UserInputConfig; | ||
$development?: UserInputConfig; | ||
$production?: UserInputConfig; | ||
$env?: Record<string, UserInputConfig>; | ||
$meta?: ConfigLayerMeta; | ||
} | ||
interface InputConfig extends C12InputConfig, UserInputConfig { | ||
} | ||
interface SourceOptions { | ||
meta?: ConfigLayerMeta; | ||
overrides?: UserInputConfig; | ||
[key: string]: any; | ||
} | ||
interface ConfigLayer<T extends InputConfig = InputConfig> { | ||
config: T | null; | ||
source?: string; | ||
sourceOptions?: SourceOptions; | ||
meta?: ConfigLayerMeta; | ||
cwd?: string; | ||
@@ -62,2 +82,4 @@ configFile?: string; | ||
dotenv?: boolean | DotenvOptions; | ||
envName?: string | false; | ||
packageJson?: boolean | string | string[]; | ||
defaults?: T; | ||
@@ -75,2 +97,2 @@ defaultConfig?: T; | ||
export { ConfigLayer, DotenvOptions, Env, InputConfig, LoadConfigOptions, ResolveConfigOptions, ResolvedConfig, loadConfig, loadDotenv, setupDotenv }; | ||
export { C12InputConfig, ConfigLayer, ConfigLayerMeta, DotenvOptions, Env, InputConfig, LoadConfigOptions, ResolveConfigOptions, ResolvedConfig, SourceOptions, UserInputConfig, loadConfig, loadDotenv, setupDotenv }; |
{ | ||
"name": "c12", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Smart Config Loader", | ||
@@ -34,5 +34,5 @@ "repository": "unjs/c12", | ||
"dotenv": "^16.0.3", | ||
"giget": "^1.1.0", | ||
"jiti": "^1.17.1", | ||
"mlly": "^1.1.1", | ||
"giget": "^1.1.2", | ||
"jiti": "^1.17.2", | ||
"mlly": "^1.2.0", | ||
"pathe": "^1.1.0", | ||
@@ -43,12 +43,12 @@ "pkg-types": "^1.0.2", | ||
"devDependencies": { | ||
"@vitest/coverage-c8": "^0.28.5", | ||
"changelogen": "^0.4.1", | ||
"eslint": "^8.34.0", | ||
"@vitest/coverage-c8": "^0.29.2", | ||
"changelogen": "^0.5.1", | ||
"eslint": "^8.36.0", | ||
"eslint-config-unjs": "^0.1.0", | ||
"prettier": "^2.8.4", | ||
"typescript": "^4.9.5", | ||
"unbuild": "^1.1.1", | ||
"vitest": "^0.28.5" | ||
"unbuild": "^1.1.2", | ||
"vitest": "^0.29.2" | ||
}, | ||
"packageManager": "pnpm@7.27.0" | ||
"packageManager": "pnpm@7.29.1" | ||
} |
117
README.md
@@ -12,6 +12,7 @@ # c12 | ||
- JSON, CJS, Typescript and ESM config loader with [unjs/jiti](https://github.com/unjs/jiti) | ||
- JSON, CJS, Typescript, and ESM config loader with [unjs/jiti](https://github.com/unjs/jiti) | ||
- RC config support with [unjs/rc9](https://github.com/unjs/rc9) | ||
- Multiple sources merged with [unjs/defu](https://github.com/unjs/defu) | ||
- `.env` support with [dotenv](https://www.npmjs.com/package/dotenv) | ||
- Support reading config from the nearest `package.json` file | ||
- Support extending nested configurations from multiple local or git sources | ||
@@ -38,6 +39,6 @@ | ||
// ESM | ||
import { loadConfig } from 'c12' | ||
import { loadConfig } from "c12"; | ||
// CommonJS | ||
const { loadConfig } = require('c12') | ||
const { loadConfig } = require("c12"); | ||
``` | ||
@@ -49,6 +50,6 @@ | ||
// Get loaded config | ||
const { config } = await loadConfig({}) | ||
const { config } = await loadConfig({}); | ||
// Get resolved config and extended layers | ||
const { config, configFile, layers } = await loadConfig({}) | ||
const { config, configFile, layers } = await loadConfig({}); | ||
``` | ||
@@ -60,8 +61,9 @@ | ||
1. config overrides passed by options | ||
2. config file in CWD | ||
1. Config overrides passed by options | ||
2. Config file in CWD | ||
3. RC file in CWD | ||
4. global RC file in user's home directory | ||
5. default config passed by options | ||
6. Extended config layers | ||
4. Global RC file in user's home directory | ||
5. Config from `package.json` | ||
6. Default config passed by options | ||
7. Extended config layers | ||
@@ -72,13 +74,13 @@ ## Options | ||
Resolve configuration from this working directory. Default is `process.cwd()` | ||
Resolve configuration from this working directory. The default is `process.cwd()` | ||
### `name` | ||
Configuration base name. Default is `config`. | ||
Configuration base name. The default is `config`. | ||
### `configName` | ||
Configuration file name without extension . Default is generated from `name` (name=foo => `foo.config`). | ||
Configuration file name without extension. Default is generated from `name` (name=foo => `foo.config`). | ||
Set to `false` to avoid loading config file. | ||
Set to `false` to avoid loading the config file. | ||
@@ -93,3 +95,3 @@ ### `rcFile` | ||
Load RC config from the workspace directory and user's home directory. Only enabled when `rcFile` is provided. Set to `false` to disable this functionality. | ||
Load RC config from the workspace directory and the user's home directory. Only enabled when `rcFile` is provided. Set to `false` to disable this functionality. | ||
@@ -100,2 +102,10 @@ ### `dotenv` | ||
### `packageJson` | ||
Loads config from nearest `package.json` file. It is disabled by default. | ||
If `true` value is passed, c12 uses `name` field from `package.json`. | ||
You can also pass either a string or an array of strings as a value to use those fields. | ||
### `defaults` | ||
@@ -109,3 +119,3 @@ | ||
### `overides` | ||
### `overrides` | ||
@@ -122,12 +132,18 @@ Specify override configuration. It has the **highest** priority and is applied **before extending** config. | ||
### `envName` | ||
Environment name used for [environment specific configuration](#environment-specific-configuration). | ||
The 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. | ||
If resolved config contains a `extends` key, it will be used to extend the configuration. | ||
Extending can be nested and each layer can extend from one base or more. | ||
Final config is merged result of extended options and user options with [unjs/defu](https://github.com/unjs/defu). | ||
The final config is merged result of extended options and user options with [unjs/defu](https://github.com/unjs/defu). | ||
Each item in extends, is a string that can be either an absolute or relative path to current config file pointing to a config file for extending or directory containing config file. | ||
If it starts with either of `github:`, `gitlab:`, `bitbucket:` or `https:`, c12 autmatically clones it. | ||
Each item in extends is a string that can be either an absolute or relative path to the current config file pointing to a config file for extending or the directory containing the config file. | ||
If it starts with either `github:`, `gitlab:`, `bitbucket:`, or `https:`, c12 automatically clones it. | ||
@@ -142,9 +158,6 @@ For custom merging strategies, you can directly access each layer with `layers` property. | ||
colors: { | ||
primary: 'user_primary' | ||
primary: "user_primary", | ||
}, | ||
extends: [ | ||
'./theme', | ||
'./config.dev.ts' | ||
] | ||
} | ||
extends: ["./theme"], | ||
}; | ||
``` | ||
@@ -155,4 +168,4 @@ | ||
export default { | ||
dev: true | ||
} | ||
dev: true, | ||
}; | ||
``` | ||
@@ -163,8 +176,8 @@ | ||
export default { | ||
extends: '../base', | ||
extends: "../base", | ||
colors: { | ||
primary: 'theme_primary', | ||
secondary: 'theme_secondary' | ||
} | ||
} | ||
primary: "theme_primary", | ||
secondary: "theme_secondary", | ||
}, | ||
}; | ||
``` | ||
@@ -182,3 +195,3 @@ | ||
Loaded configuration would look like this: | ||
The loaded configuration would look like this: | ||
@@ -206,2 +219,32 @@ ```js | ||
## Environment-specific configuration | ||
Users can define environment-specific configuration using these config keys: | ||
- `$test: {...}` | ||
- `$development: {...}` | ||
- `$production: {...}` | ||
- `$env: { [env]: {...} }` | ||
c12 tries to match [`envName`](#envname) and override environment config if specified. | ||
**Note:** Environment will be applied when extending each configuration layer. This way layers can provide environment-specific configuration. | ||
**Example:** | ||
```js | ||
{ | ||
// Default configuration | ||
logLevel: 'info', | ||
// Environment overrides | ||
$test: { logLevel: 'silent' }, | ||
$development: { logLevel: 'warning' }, | ||
$production: { logLevel: 'error' }, | ||
$env: { | ||
staging: { logLevel: 'debug' } | ||
} | ||
} | ||
``` | ||
## 💻 Development | ||
@@ -219,12 +262,10 @@ | ||
<!-- Badges --> | ||
[npm-version-src]: https://img.shields.io/npm/v/c12?style=flat-square | ||
[npm-version-href]: https://npmjs.com/package/c12 | ||
[npm-downloads-src]: https://img.shields.io/npm/dm/c12?style=flat-square | ||
[npm-downloads-href]: https://npmjs.com/package/c12 | ||
[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/c12/ci/main?style=flat-square | ||
[github-actions-src]: https://img.shields.io/github/actions/workflow/status/unjs/c12/ci.yml?branch=main&style=flat-square | ||
[github-actions-href]: https://github.com/unjs/c12/actions?query=workflow%3Aci | ||
[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/c12/main?style=flat-square | ||
[codecov-href]: https://codecov.io/gh/unjs/c12 |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30782
685
257
9
Updatedgiget@^1.1.2
Updatedjiti@^1.17.2
Updatedmlly@^1.2.0