Skip to content

Commit

Permalink
feat: watchConfig utility (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Apr 19, 2023
1 parent 44d22b4 commit 9cd11ef
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 13 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Smart Configuration Loader.
- Reads config from the nearest `package.json` file
- [Extends configurations](https://github.com/unjs/c12#extending-configuration) from multiple local or git sources
- Overwrite with [environment-specific configuration](#environment-specific-configuration)
- Config watcher with auto reload

## Usage

Expand All @@ -36,10 +37,10 @@ Import:

```js
// ESM
import { loadConfig } from "c12";
import { loadConfig, watchConfig } from "c12";

// CommonJS
const { loadConfig } = require("c12");
const { loadConfig, watchConfig } = require("c12");
```

Load configuration:
Expand Down Expand Up @@ -234,6 +235,29 @@ c12 tries to match [`envName`](#envname) and override environment config if spec
}
```

## Watching Configuration

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,

```ts
import { watchConfig } from "c12";

const config = watchConfig({
cwd: ".",
// chokidarOptions: {}, // Default is { ignoreInitial: true }
// debounce: 200 // Default is 100. You can set to fale to disable debounced watcher
onChange: ({ config, oldConfig, path, type }) => {
console.log("[watcher]", type, path);
},
});

console.log("initial config", config.config, config.layers);
console.log("watching config files:", config.watchingFiles);

// When exiting process
await config.unwatch();
```

## 💻 Development

- Clone this repository
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
"test:types": "tsc --noEmit"
},
"dependencies": {
"chokidar": "^3.5.3",
"defu": "^6.1.2",
"dotenv": "^16.0.3",
"giget": "^1.1.2",
"jiti": "^1.18.2",
"mlly": "^1.2.0",
"pathe": "^1.1.0",
"perfect-debounce": "^0.1.3",
"pkg-types": "^1.0.2",
"rc9": "^2.1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion test/test.ts → playground/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { loadConfig } from "../src";
const r = (path: string) => fileURLToPath(new URL(path, import.meta.url));

async function main() {
const fixtureDir = r("./fixture");
const fixtureDir = r("../test/fixture");
const config = await loadConfig({ cwd: fixtureDir, dotenv: true });
console.log(config);
}
Expand Down
27 changes: 27 additions & 0 deletions playground/watch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { fileURLToPath } from "node:url";
import { watchConfig } from "../src";

const r = (path: string) => fileURLToPath(new URL(path, import.meta.url));

async function main() {
const fixtureDir = r("../test/fixture");
const config = await watchConfig({
cwd: fixtureDir,
dotenv: true,
packageJson: ["c12", "c12-alt"],
globalRc: true,
envName: "test",
extend: {
extendKey: ["theme", "extends"],
},
onChange: ({ config, path, type }) => {
console.log("[watcher]", type, path);
console.log(config.config);
},
});
console.log("initial config", config.config, config.layers);
console.log("watching config files:", config.watchingFiles);
}

// eslint-disable-next-line unicorn/prefer-top-level-await
main().catch(console.error);
66 changes: 57 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./dotenv";
export * from "./loader";
export * from "./types";
export * from "./watch";
Loading

0 comments on commit 9cd11ef

Please sign in to comment.