c12 0.2.7 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,7 +24,7 @@ Install package:
24
24
  npm install c12
25
25
 
26
26
  # yarn
27
- yarn install c12
27
+ yarn add c12
28
28
 
29
29
  # pnpm
30
30
  pnpm install c12
package/dist/index.cjs CHANGED
@@ -161,6 +161,7 @@ async function extendConfig(config, opts) {
161
161
  for (const extendSource of extendSources) {
162
162
  const _config = await resolveConfig(extendSource, opts);
163
163
  if (!_config.config) {
164
+ console.warn(`Cannot extend config from ${extendSource} in ${opts.cwd}`);
164
165
  continue;
165
166
  }
166
167
  await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
@@ -172,6 +173,7 @@ async function extendConfig(config, opts) {
172
173
  }
173
174
  }
174
175
  const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
176
+ const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
175
177
  const jiti = createJiti__default(null, { cache: false, interopDefault: true, requireCache: false });
176
178
  async function resolveConfig(source, opts) {
177
179
  if (opts.resolve) {
@@ -192,12 +194,18 @@ async function resolveConfig(source, opts) {
192
194
  await gittar.extract(tarFile, tmpdir);
193
195
  source = pathe.resolve(tmpdir, subPath);
194
196
  }
197
+ if (NPM_PACKAGE_RE.test(source)) {
198
+ try {
199
+ source = jiti.resolve(source, { paths: [opts.cwd] });
200
+ } catch (_err) {
201
+ }
202
+ }
195
203
  const isDir = !pathe.extname(source);
196
204
  const cwd = pathe.resolve(opts.cwd, isDir ? source : pathe.dirname(source));
197
205
  if (isDir) {
198
206
  source = opts.configFile;
199
207
  }
200
- const res = { config: {}, cwd };
208
+ const res = { config: null, cwd };
201
209
  try {
202
210
  res.configFile = jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
203
211
  } catch (_err) {
package/dist/index.d.ts CHANGED
@@ -37,7 +37,7 @@ declare function loadDotenv(opts: DotenvOptions): Promise<Env>;
37
37
  interface InputConfig extends Record<string, any> {
38
38
  }
39
39
  interface ConfigLayer<T extends InputConfig = InputConfig> {
40
- config: T;
40
+ config: T | null;
41
41
  cwd?: string;
42
42
  configFile?: string;
43
43
  }
package/dist/index.mjs CHANGED
@@ -137,6 +137,7 @@ async function extendConfig(config, opts) {
137
137
  for (const extendSource of extendSources) {
138
138
  const _config = await resolveConfig(extendSource, opts);
139
139
  if (!_config.config) {
140
+ console.warn(`Cannot extend config from ${extendSource} in ${opts.cwd}`);
140
141
  continue;
141
142
  }
142
143
  await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
@@ -148,6 +149,7 @@ async function extendConfig(config, opts) {
148
149
  }
149
150
  }
150
151
  const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
152
+ const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
151
153
  const jiti = createJiti(null, { cache: false, interopDefault: true, requireCache: false });
152
154
  async function resolveConfig(source, opts) {
153
155
  if (opts.resolve) {
@@ -168,12 +170,18 @@ async function resolveConfig(source, opts) {
168
170
  await gittar.extract(tarFile, tmpdir);
169
171
  source = resolve(tmpdir, subPath);
170
172
  }
173
+ if (NPM_PACKAGE_RE.test(source)) {
174
+ try {
175
+ source = jiti.resolve(source, { paths: [opts.cwd] });
176
+ } catch (_err) {
177
+ }
178
+ }
171
179
  const isDir = !extname(source);
172
180
  const cwd = resolve(opts.cwd, isDir ? source : dirname(source));
173
181
  if (isDir) {
174
182
  source = opts.configFile;
175
183
  }
176
- const res = { config: {}, cwd };
184
+ const res = { config: null, cwd };
177
185
  try {
178
186
  res.configFile = jiti.resolve(resolve(cwd, source), { paths: [cwd] });
179
187
  } catch (_err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c12",
3
- "version": "0.2.7",
3
+ "version": "0.2.8",
4
4
  "description": "Smart Config Loader",
5
5
  "repository": "unjs/c12",
6
6
  "license": "MIT",
@@ -20,12 +20,12 @@
20
20
  ],
21
21
  "dependencies": {
22
22
  "defu": "^6.0.0",
23
- "dotenv": "^16.0.0",
23
+ "dotenv": "^16.0.1",
24
24
  "gittar": "^0.1.1",
25
- "jiti": "^1.12.14",
26
- "mlly": "^0.5.1",
27
- "pathe": "^0.2.0",
28
- "rc9": "^1.2.1"
25
+ "jiti": "^1.14.0",
26
+ "mlly": "^0.5.4",
27
+ "pathe": "^0.3.2",
28
+ "rc9": "^1.2.2"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@nuxtjs/eslint-config-typescript": "latest",
@@ -36,13 +36,12 @@
36
36
  "unbuild": "latest",
37
37
  "vitest": "latest"
38
38
  },
39
- "packageManager": "pnpm@6.32.3",
39
+ "packageManager": "pnpm@7.4.0",
40
40
  "scripts": {
41
41
  "build": "unbuild",
42
42
  "dev": "vitest dev",
43
43
  "lint": "eslint --ext .ts,.js,.mjs,.cjs .",
44
44
  "release": "pnpm test && standard-version && git push --follow-tags && pnpm publish",
45
45
  "test": "vitest run --coverage"
46
- },
47
- "readme": "# c12\n\n[![npm version][npm-version-src]][npm-version-href]\n[![npm downloads][npm-downloads-src]][npm-downloads-href]\n[![Github Actions][github-actions-src]][github-actions-href]\n[![Codecov][codecov-src]][codecov-href]\n\n> Smart Configuration Loader\n\n## Features\n\n- JSON, CJS, Typescript and ESM config loader with [unjs/jiti](https://github.com/unjs/jiti)\n- RC config support with [unjs/rc9](https://github.com/unjs/rc9)\n- Multiple sources merged with [unjs/defu](https://github.com/unjs/defu)\n- `.env` support with [dotenv](https://www.npmjs.com/package/dotenv)\n- Support extending nested configurations from multiple local or git sources\n\n## Usage\n\nInstall package:\n\n```sh\n# npm\nnpm install c12\n\n# yarn\nyarn install c12\n\n# pnpm\npnpm install c12\n```\n\nImport:\n\n```js\n// ESM\nimport { loadConfig } from 'c12'\n\n// CommonJS\nconst { loadConfig } = require('c12')\n```\n\nLoad configuration:\n\n```js\n// Get loaded config\nconst { config } = await loadConfig({})\n\n// Get resolved config and extended layers\nconst { config, configFile, layers } = await loadConfig({})\n```\n\n## Loading priority\n\nc12 merged config sources with [unjs/defu](https://github.com/unjs/defu) by below order:\n\n1. config overrides passed by options\n2. config file in CWD\n3. RC file in CWD\n4. global RC file in user's home directory\n5. default config passed by options\n6. Extended config layers\n\n## Options\n\n### `cwd`\n\nResolve configuration from this working directory. Default is `process.cwd()`\n\n### `name`\n\nConfiguration base name. Default is `config`.\n\n### `configName`\n\nConfiguration file name without extension . Default is generated from `name` (name=foo => `foo.config`).\n\nSet to `false` to avoid loading config file.\n\n### `rcFile`\n\nRC Config file name. Default is generated from `name` (name=foo => `.foorc`).\n\nSet to `false` to disable loading RC config.\n\n### `globalRC`\n\nLoad RC config from the user's home directory. Only enabled when `rcFile` is provided. Set to `false` to disable this functionality.\n\n### `dotenv`\n\nLoads `.env` file if enabled. It is disabled by default.\n\n### `defaults`\n\nSpecify default configuration. It has the **lowest** priority.\n\n### `overides`\n\nSpecify override configuration. It has the **highest** priority.\n\n## Extending configuration\n\nIf resolved config contains a `extends` key, it will be used to extend configuration.\n\nExtending can be nested and each layer can extend from one base or more.\n\nFinal config is merged result of extended options and user options with [unjs/defu](https://github.com/unjs/defu).\n\nEach 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.\nIf it starts with either of `github:`, `gitlab:`, `bitbucket:` or `https:`, c12 autmatically clones it.\n\nFor custom merging strategies, you can directly access each layer with `layers` property.\n\n**Example:**\n\n```js\n// config.ts\nexport default {\n colors: {\n primary: 'user_primary'\n },\n extends: [\n './theme',\n './config.dev.ts'\n ]\n}\n```\n\n```js\n// config.dev.ts\nexport default {\n dev: true\n}\n```\n\n```js\n// theme/config.ts\nexport default {\n extends: '../base',\n colors: {\n primary: 'theme_primary',\n secondary: 'theme_secondary'\n }\n}\n```\n\n```js\n// base/config.ts\nexport default {\n colors: {\n primary: 'base_primary'\n text: 'base_text'\n }\n}\n```\n\nLoaded configuration would look like this:\n\n```js\n{\n dev: true,\n colors: {\n primary: 'user_primary',\n secondary: 'theme_secondary',\n text: 'base_text'\n }\n}\n```\n\nLayers:\n\n```js\n[\n { config: /* theme config */, configFile: /* path/to/theme/config.ts */, cwd: /* path/to/theme */ },\n { config: /* base config */, configFile: /* path/to/base/config.ts */, cwd: /* path/to/base */ },\n { config: /* dev config */, configFile: /* path/to/config.dev.ts */, cwd: /* path/ */ },\n]\n```\n\n## 💻 Development\n\n- Clone this repository\n- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)\n- Install dependencies using `pnpm install`\n- Run interactive tests using `pnpm dev`\n\n## License\n\nMade with 💛 Published under [MIT License](./LICENSE).\n\n<!-- Badges -->\n[npm-version-src]: https://img.shields.io/npm/v/c12?style=flat-square\n[npm-version-href]: https://npmjs.com/package/c12\n\n[npm-downloads-src]: https://img.shields.io/npm/dm/c12?style=flat-square\n[npm-downloads-href]: https://npmjs.com/package/c12\n\n[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/c12/ci/main?style=flat-square\n[github-actions-href]: https://github.com/unjs/c12/actions?query=workflow%3Aci\n\n[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/c12/main?style=flat-square\n[codecov-href]: https://codecov.io/gh/unjs/c12\n"
46
+ }
48
47
  }