c12 1.9.0 → 1.11.0

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
@@ -22,6 +22,7 @@ c12 (pronounced as /siːtwelv/, like c-twelve) is a smart configuration loader.
22
22
  - [Extends configurations](https://github.com/unjs/c12#extending-configuration) from multiple local or git sources
23
23
  - Overwrite with [environment-specific configuration](#environment-specific-configuration)
24
24
  - Config watcher with auto-reload and HMR support
25
+ - Create or update configuration files with [magicast](https://github.com/unjs/magicast)
25
26
 
26
27
  ## 🦴 Used by
27
28
 
@@ -40,33 +41,41 @@ Install package:
40
41
 
41
42
  ```sh
42
43
  # ✨ Auto-detect
43
- npx nypm i c12@^1.7.0
44
+ npx nypm install c12
44
45
 
45
46
  # npm
46
- npm install c12@^1.7.0
47
+ npm install c12
47
48
 
48
49
  # yarn
49
- yarn add c12@^1.7.0
50
+ yarn add c12
50
51
 
51
52
  # pnpm
52
- pnpm install c12@^1.7.0
53
+ pnpm install c12
53
54
 
54
55
  # bun
55
- bun install c12@^1.7.0
56
+ bun install c12
56
57
  ```
57
58
 
58
59
  <!-- /automd -->
59
60
 
60
61
  Import:
61
62
 
63
+ <!-- automd:jsimport cjs imports="loadConfig,watchConfig" -->
64
+
65
+ **ESM** (Node.js, Bun)
66
+
62
67
  ```js
63
- // ESM
64
68
  import { loadConfig, watchConfig } from "c12";
69
+ ```
70
+
71
+ **CommonJS** (Legacy Node.js)
65
72
 
66
- // CommonJS
73
+ ```js
67
74
  const { loadConfig, watchConfig } = require("c12");
68
75
  ```
69
76
 
77
+ <!-- /automd -->
78
+
70
79
  Load configuration:
71
80
 
72
81
  ```js
@@ -155,6 +164,12 @@ Custom [unjs/jiti](https://github.com/unjs/jiti) options to import configuration
155
164
 
156
165
  Options passed to [unjs/giget](https://github.com/unjs/giget) when extending layer from git source.
157
166
 
167
+ ### `merger`
168
+
169
+ Custom options merger function. Default is [defu](https://github.com/unjs/defu).
170
+
171
+ **Note:** Custom merge function should deeply merge options with arguments high -> low priority.
172
+
158
173
  ### `envName`
159
174
 
160
175
  Environment name used for [environment specific configuration](#environment-specific-configuration).
@@ -208,36 +223,54 @@ export default {
208
223
  // base/config.ts
209
224
  export default {
210
225
  colors: {
211
- primary: 'base_primary'
212
- text: 'base_text'
213
- }
214
- }
226
+ primary: "base_primary",
227
+ text: "base_text",
228
+ },
229
+ };
215
230
  ```
216
231
 
217
232
  The loaded configuration would look like this:
218
233
 
219
234
  ```js
220
- {
235
+ const config = {
221
236
  dev: true,
222
237
  colors: {
223
- primary: 'user_primary',
224
- secondary: 'theme_secondary',
225
- text: 'base_text'
226
- }
227
- }
238
+ primary: "user_primary",
239
+ secondary: "theme_secondary",
240
+ text: "base_text",
241
+ },
242
+ };
228
243
  ```
229
244
 
230
245
  Layers:
231
246
 
232
247
  ```js
233
248
  [
234
- { config: /* theme config */, configFile: /* path/to/theme/config.ts */, cwd: /* path/to/theme */ },
235
- { config: /* base config */, configFile: /* path/to/base/config.ts */, cwd: /* path/to/base */ },
236
- { config: /* dev config */, configFile: /* path/to/config.dev.ts */, cwd: /* path/ */ },
237
- ]
249
+ {
250
+ config: {
251
+ /* theme config */
252
+ },
253
+ configFile: "/path/to/theme/config.ts",
254
+ cwd: "/path/to/theme ",
255
+ },
256
+ {
257
+ config: {
258
+ /* base config */
259
+ },
260
+ configFile: "/path/to/base/config.ts",
261
+ cwd: "/path/to/base",
262
+ },
263
+ {
264
+ config: {
265
+ /* dev config */
266
+ },
267
+ configFile: "/path/to/config.dev.ts",
268
+ cwd: "/path/",
269
+ },
270
+ ];
238
271
  ```
239
272
 
240
- ## Extending Config Layer from Remote Sources
273
+ ## Extending config layer from remote sources
241
274
 
242
275
  You can also extend configuration from remote sources such as npm or github.
243
276
 
@@ -261,15 +294,17 @@ export default {
261
294
  };
262
295
  ```
263
296
 
264
- **Example:** Extend with clone configuration
297
+ **Example:** Extend a private repository and install dependencies:
265
298
 
266
299
  ```js
267
300
  // config.ts
268
301
  export default {
269
- extends: ["gh:user/repo", { giget: { auth: process.env.GITHUB_TOKEN } }],
302
+ extends: ["gh:user/repo", { auth: process.env.GITHUB_TOKEN, install: true }],
270
303
  };
271
304
  ```
272
305
 
306
+ You can pass more options to `giget: {}` in layer config.
307
+
273
308
  Refer to [unjs/giget](https://giget.unjs.io) for more information.
274
309
 
275
310
  ## Environment-specific configuration
@@ -288,21 +323,21 @@ c12 tries to match [`envName`](#envname) and override environment config if spec
288
323
  **Example:**
289
324
 
290
325
  ```js
291
- {
326
+ export default {
292
327
  // Default configuration
293
- logLevel: 'info',
328
+ logLevel: "info",
294
329
 
295
330
  // Environment overrides
296
- $test: { logLevel: 'silent' },
297
- $development: { logLevel: 'warning' },
298
- $production: { logLevel: 'error' },
331
+ $test: { logLevel: "silent" },
332
+ $development: { logLevel: "warning" },
333
+ $production: { logLevel: "error" },
299
334
  $env: {
300
- staging: { logLevel: 'debug' }
301
- }
302
- }
335
+ staging: { logLevel: "debug" },
336
+ },
337
+ };
303
338
  ```
304
339
 
305
- ## Watching Configuration
340
+ ## Watching configuration
306
341
 
307
342
  you can use `watchConfig` instead of `loadConfig` to load config and watch for changes, add and removals in all expected configuration paths and auto reload with new config.
308
343
 
@@ -342,24 +377,88 @@ console.log("initial config", config.config);
342
377
  // await config.unwatch();
343
378
  ```
344
379
 
345
- ## 💻 Development
380
+ ## Updating config
381
+
382
+ > [!NOTE]
383
+ > This feature is experimental
384
+
385
+ Update or create a new configuration files.
386
+
387
+ Add `magicast` peer dependency:
388
+
389
+ <!-- automd:pm-install name="magicast" dev -->
390
+
391
+ ```sh
392
+ # ✨ Auto-detect
393
+ npx nypm install -D magicast
394
+
395
+ # npm
396
+ npm install -D magicast
397
+
398
+ # yarn
399
+ yarn add -D magicast
400
+
401
+ # pnpm
402
+ pnpm install -D magicast
403
+
404
+ # bun
405
+ bun install -D magicast
406
+ ```
407
+
408
+ <!-- /automd -->
409
+
410
+ Import util from `c12/update`
411
+
412
+ ```js
413
+ const { configFile, created } = await updateConfig({
414
+ cwd: ".",
415
+ configFile: "foo.config",
416
+ onCreate: ({ configFile }) => {
417
+ // You can prompt user if wants to create a new config file and return false to cancel
418
+ console.log(`Creating new config file in ${configFile}...`);
419
+ return "export default { test: true }";
420
+ },
421
+ onUpdate: (config) => {
422
+ // You can update the config contents just like an object
423
+ config.test2 = false;
424
+ },
425
+ });
426
+
427
+ console.log(`Config file ${created ? "created" : "updated"} in ${configFile}`);
428
+ ```
429
+
430
+ ## Contribution
431
+
432
+ <details>
433
+ <summary>Local development</summary>
346
434
 
347
435
  - Clone this repository
348
- - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)
436
+ - Install the latest LTS version of [Node.js](https://nodejs.org/en/)
437
+ - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
349
438
  - Install dependencies using `pnpm install`
350
- - Run interactive tests using `pnpm dev`
439
+ - Run tests using `pnpm dev` or `pnpm test`
440
+
441
+ </details>
442
+
443
+ <!-- /automd -->
351
444
 
352
445
  ## License
353
446
 
354
- Made with 💛 Published under [MIT License](./LICENSE).
447
+ <!-- automd:contributors license=MIT author="pi0" -->
448
+
449
+ Published under the [MIT](https://github.com/unjs/c12/blob/main/LICENSE) license.
450
+ Made by [@pi0](https://github.com/pi0) and [community](https://github.com/unjs/c12/graphs/contributors) 💛
451
+ <br><br>
452
+ <a href="https://github.com/unjs/c12/graphs/contributors">
453
+ <img src="https://contrib.rocks/image?repo=unjs/c12" />
454
+ </a>
455
+
456
+ <!-- /automd -->
355
457
 
356
- <!-- Badges -->
458
+ <!-- automd:with-automd -->
357
459
 
358
- [npm-version-src]: https://img.shields.io/npm/v/c12?style=flat&colorA=18181B&colorB=F0DB4F
359
- [npm-version-href]: https://npmjs.com/package/c12
360
- [npm-downloads-src]: https://img.shields.io/npm/dm/c12?style=flat&colorA=18181B&colorB=F0DB4F
361
- [npm-downloads-href]: https://npmjs.com/package/c12
362
- [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/c12/main?style=flat&colorA=18181B&colorB=F0DB4F
363
- [codecov-href]: https://codecov.io/gh/unjs/c12
364
- [license-src]: https://img.shields.io/github/license/unjs/c12.svg?style=flat&colorA=18181B&colorB=F0DB4F
365
- [license-href]: https://github.com/unjs/c12/blob/main/LICENSE
460
+ ---
461
+
462
+ _🤖 auto updated with [automd](https://automd.unjs.io)_
463
+
464
+ <!-- /automd -->