gunshi-c12
v1.0.0
Published
Gunshi plugin that provides c12 configuration loading capabilities
Maintainers
Readme
gunshi-c12
gunshi-c12 does not re-implement config loading. It wires Gunshi global options into direct c12 calls (loadConfig/watchConfig) and exposes them on ctx.extensions.c12.
Install
pnpm add gunshi-c12Quick Start
import { cli, define } from "gunshi";
import { c12 } from "gunshi-c12";
const command = define({
name: "build",
run: async (ctx) => {
const configApi = ctx.extensions.c12;
const { config } = await configApi.loadConfig();
console.log(config);
},
});
await cli(process.argv.slice(2), command, {
name: "my-cli",
version: "1.0.0",
plugins: [c12({ name: "myapp" })],
});What This Plugin Adds
- Registers Gunshi global options:
- default mode:
--config,--env - scoped mode (
configName):--config-<name>,--env-<name>
- default mode:
- Resolves APPNAME overrides in scoped mode:
configName: "app"readsAPP_APPNAME
- Merges options and calls c12 directly.
Everything else (file formats, layering, extends, dotenv, watch behavior, etc.) is c12 behavior.
Option Merge Rules
The extension merges options in this order:
- Plugin options passed to
c12(...) - CLI-derived overrides from global flags (
config*/env*) - Runtime options passed to
loadConfig(...)orwatchConfig(...)
For nested overrides, merge is shallow by key (last write wins per key).
Resolved c12 name priority:
options.namepassed toc12(...)- In scoped mode,
APP_APPNAME-style override (<CONFIGNAME>_APPNAME) thenconfigName - Gunshi program name (
ctx.env.name, usually CLIname) - Fallback:
"config"
Scoped Configuration (configName)
Use configName when you want config flags scoped to a domain.
import { c12 } from "gunshi-c12";
// Adds --config-app and --env-app
const plugin = c12({ configName: "app", name: "myapp" });configName also enables APPNAME override lookup:
# Uses APP_APPNAME to override resolved c12 name
APP_APPNAME=project-a my-cli build --config-app ./app.config.tsAPI
c12(options?)
Creates the Gunshi plugin.
optionsisc12LoadConfigOptionsplus:configName?: string
Extension Methods
From ctx.extensions.c12:
loadConfig(options?)-> forwards toc12.loadConfigwatchConfig(options?)-> forwards toc12.watchConfigload(options?)-> alias ofloadConfig
Types
This package re-exports core c12 types (for example LoadConfigOptions, WatchConfigOptions, ResolvedConfig, ConfigWatcher) and plugin types (C12Extension, ConfigLoader).
License
MIT
