Skip to content

Commit 7bd1b5b

Browse files
authored
feat(config): cosmiconfig (#939)
1 parent 38710ef commit 7bd1b5b

8 files changed

Lines changed: 252 additions & 53 deletions

File tree

.changeset/curvy-items-smile.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@node-core/doc-kit': minor
3+
---
4+
5+
Discover and load configuration files with `cosmiconfig`.

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ Commands:
5454

5555
### `generate`
5656

57-
You must provide either `--target` (one or more generators to run) or
58-
`--config-file` (which supplies the targets). Running `generate` without either
59-
exits with an error pointing you to the help output.
57+
You must provide an input and at least one target through command-line options
58+
or a configuration file. Configuration is discovered automatically using
59+
`cosmiconfig`, or you can select a file explicitly with `--config-file`.
60+
Running `generate` without the required values exits with an error pointing you
61+
to the help output.
6062

6163
```
6264
Usage: @node-core/doc-kit generate [options]

bin/commands/generate.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const { runGenerators } = createGenerator();
1212

1313
/**
1414
* @typedef {Object} CLIOptions
15+
* @property {string} configFile
1516
* @property {string[]} input
1617
* @property {string[]} target
1718
* @property {string[]} ignore

docs/configuration.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
# Configuration
22

3-
`doc-kit`'s CLI supports a `--config-file` option, allowing for custom configuration files to be passed.
4-
These configuration files must be loadable via a `import()` call, so usually JSON or JavaScript files with default exports.
3+
`doc-kit` uses [`cosmiconfig`](https://github.com/cosmiconfig/cosmiconfig) to
4+
discover and load configuration. Run the CLI from your project directory and it
5+
will automatically look for a `doc-kit` property in `package.json`, rc files
6+
such as `.doc-kitrc.yml`, and module files such as `doc-kit.config.mjs`.
57

6-
By convention, this file is usually named `doc-kit.config.mjs` or `doc-kit.config.json`.
8+
Use `--config-file <path>` to load a specific file instead of searching.
79

810
## Configuration File Format
911

1012
Configuration files can be either:
1113

12-
- **JavaScript/ESM** (`.mjs`, `.js` with `"type": "module"`)
14+
- **JavaScript** (`.js`, `.mjs`, `.cjs`)
15+
- **TypeScript** (`.ts`, when `typescript` is installed in the project)
1316
- **JSON** (`.json`)
17+
- **YAML** (`.yaml`, `.yml`, or an extensionless rc file)
18+
19+
JavaScript and TypeScript configuration files export the configuration object.
20+
JSON and YAML files contain the object directly. A `package.json` configuration
21+
uses the `doc-kit` property:
22+
23+
```json
24+
{
25+
"doc-kit": {
26+
"target": ["json"],
27+
"global": {
28+
"input": "doc/api/*.md",
29+
"output": "out"
30+
}
31+
}
32+
}
33+
```
1434

1535
### Basic Example
1636

@@ -94,10 +114,11 @@ export default {
94114

95115
## Configuration Merging
96116

97-
Configurations are merged in the following order (earlier sources take precedence):
117+
Configurations are merged in the following order (higher sources take
118+
precedence):
98119

99-
1. **Config file** (`--config-file`)
100-
2. **CLI options** (command-line arguments)
120+
1. **CLI options** (command-line arguments)
121+
2. **Configuration file** (discovered or selected with `--config-file`)
101122
3. **Default values** (built-in defaults)
102123

103124
## CLI Options Mapping

package-lock.json

Lines changed: 156 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"@swc/wasm": "^1.15.46",
7373
"acorn": "^8.17.0",
7474
"commander": "^15.0.0",
75+
"cosmiconfig": "^9.0.2",
7576
"dedent": "^1.7.2",
7677
"estree-util-to-js": "^2.0.0",
7778
"estree-util-visit": "^2.0.0",

0 commit comments

Comments
 (0)