c12 0.2.6 → 0.2.9

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
@@ -129,12 +129,19 @@ async function loadConfig(opts) {
129
129
  }
130
130
  Object.assign(configRC, rc9__namespace.read({ name: opts.rcFile, dir: opts.cwd }));
131
131
  }
132
- r.config = defu__default(opts.overrides, config, configRC);
132
+ r.config = defu__default(
133
+ opts.overrides,
134
+ config,
135
+ configRC
136
+ );
133
137
  if (opts.extend) {
134
138
  await extendConfig(r.config, opts);
135
139
  r.layers = r.config._layers;
136
140
  delete r.config._layers;
137
- r.config = defu__default(r.config, ...r.layers.map((e) => e.config));
141
+ r.config = defu__default(
142
+ r.config,
143
+ ...r.layers.map((e) => e.config)
144
+ );
138
145
  }
139
146
  const baseLayers = [
140
147
  opts.overrides && { config: opts.overrides, configFile: void 0, cwd: void 0 },
@@ -161,6 +168,7 @@ async function extendConfig(config, opts) {
161
168
  for (const extendSource of extendSources) {
162
169
  const _config = await resolveConfig(extendSource, opts);
163
170
  if (!_config.config) {
171
+ console.warn(`Cannot extend config from ${extendSource} in ${opts.cwd}`);
164
172
  continue;
165
173
  }
166
174
  await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
@@ -172,7 +180,8 @@ async function extendConfig(config, opts) {
172
180
  }
173
181
  }
174
182
  const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
175
- const jiti = createJiti__default(null, { cache: false, interopDefault: true, requireCache: false });
183
+ const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
184
+ const jiti = createJiti__default(null, { cache: false, interopDefault: true, requireCache: false, esmResolve: true });
176
185
  async function resolveConfig(source, opts) {
177
186
  if (opts.resolve) {
178
187
  const res2 = await opts.resolve(source, opts);
@@ -192,23 +201,28 @@ async function resolveConfig(source, opts) {
192
201
  await gittar.extract(tarFile, tmpdir);
193
202
  source = pathe.resolve(tmpdir, subPath);
194
203
  }
204
+ if (NPM_PACKAGE_RE.test(source)) {
205
+ try {
206
+ source = jiti.resolve(source, { paths: [opts.cwd] });
207
+ } catch (_err) {
208
+ }
209
+ }
195
210
  const isDir = !pathe.extname(source);
196
211
  const cwd = pathe.resolve(opts.cwd, isDir ? source : pathe.dirname(source));
197
212
  if (isDir) {
198
213
  source = opts.configFile;
199
214
  }
200
- const res = { config: {}, cwd };
215
+ const res = { config: null, cwd };
201
216
  try {
202
217
  res.configFile = jiti.resolve(pathe.resolve(cwd, source), { paths: [cwd] });
203
- res.config = jiti(res.configFile);
204
- if (typeof res.config === "function") {
205
- res.config = await res.config();
206
- }
207
- } catch (err) {
208
- if (err.code === "MODULE_NOT_FOUND" && String(err).includes(opts.configFile)) {
209
- return res;
210
- }
211
- throw err;
218
+ } catch (_err) {
219
+ }
220
+ if (!fs.existsSync(res.configFile)) {
221
+ return res;
222
+ }
223
+ res.config = jiti(res.configFile);
224
+ if (typeof res.config === "function") {
225
+ res.config = await res.config();
212
226
  }
213
227
  return res;
214
228
  }
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
@@ -105,12 +105,19 @@ async function loadConfig(opts) {
105
105
  }
106
106
  Object.assign(configRC, rc9.read({ name: opts.rcFile, dir: opts.cwd }));
107
107
  }
108
- r.config = defu(opts.overrides, config, configRC);
108
+ r.config = defu(
109
+ opts.overrides,
110
+ config,
111
+ configRC
112
+ );
109
113
  if (opts.extend) {
110
114
  await extendConfig(r.config, opts);
111
115
  r.layers = r.config._layers;
112
116
  delete r.config._layers;
113
- r.config = defu(r.config, ...r.layers.map((e) => e.config));
117
+ r.config = defu(
118
+ r.config,
119
+ ...r.layers.map((e) => e.config)
120
+ );
114
121
  }
115
122
  const baseLayers = [
116
123
  opts.overrides && { config: opts.overrides, configFile: void 0, cwd: void 0 },
@@ -137,6 +144,7 @@ async function extendConfig(config, opts) {
137
144
  for (const extendSource of extendSources) {
138
145
  const _config = await resolveConfig(extendSource, opts);
139
146
  if (!_config.config) {
147
+ console.warn(`Cannot extend config from ${extendSource} in ${opts.cwd}`);
140
148
  continue;
141
149
  }
142
150
  await extendConfig(_config.config, { ...opts, cwd: _config.cwd });
@@ -148,7 +156,8 @@ async function extendConfig(config, opts) {
148
156
  }
149
157
  }
150
158
  const GIT_PREFIXES = ["github:", "gitlab:", "bitbucket:", "https://"];
151
- const jiti = createJiti(null, { cache: false, interopDefault: true, requireCache: false });
159
+ const NPM_PACKAGE_RE = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/;
160
+ const jiti = createJiti(null, { cache: false, interopDefault: true, requireCache: false, esmResolve: true });
152
161
  async function resolveConfig(source, opts) {
153
162
  if (opts.resolve) {
154
163
  const res2 = await opts.resolve(source, opts);
@@ -168,23 +177,28 @@ async function resolveConfig(source, opts) {
168
177
  await gittar.extract(tarFile, tmpdir);
169
178
  source = resolve(tmpdir, subPath);
170
179
  }
180
+ if (NPM_PACKAGE_RE.test(source)) {
181
+ try {
182
+ source = jiti.resolve(source, { paths: [opts.cwd] });
183
+ } catch (_err) {
184
+ }
185
+ }
171
186
  const isDir = !extname(source);
172
187
  const cwd = resolve(opts.cwd, isDir ? source : dirname(source));
173
188
  if (isDir) {
174
189
  source = opts.configFile;
175
190
  }
176
- const res = { config: {}, cwd };
191
+ const res = { config: null, cwd };
177
192
  try {
178
193
  res.configFile = jiti.resolve(resolve(cwd, source), { paths: [cwd] });
179
- res.config = jiti(res.configFile);
180
- if (typeof res.config === "function") {
181
- res.config = await res.config();
182
- }
183
- } catch (err) {
184
- if (err.code === "MODULE_NOT_FOUND" && String(err).includes(opts.configFile)) {
185
- return res;
186
- }
187
- throw err;
194
+ } catch (_err) {
195
+ }
196
+ if (!existsSync(res.configFile)) {
197
+ return res;
198
+ }
199
+ res.config = jiti(res.configFile);
200
+ if (typeof res.config === "function") {
201
+ res.config = await res.config();
188
202
  }
189
203
  return res;
190
204
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c12",
3
- "version": "0.2.6",
3
+ "version": "0.2.9",
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.7",
27
+ "pathe": "^0.3.3",
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.8.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
  }