Execute a single command under the current dotenv context. Includes a convenient parent‑level -c, --cmd <command...> alias so npm script flags apply to getdotenv rather than the inner shell command.
// Recommended: plugins barrel (shares type identity with cliHost)
import { cmdPlugin } from '@karmaniverous/get-dotenv/plugins';
Per‑plugin subpaths remain available when needed:
import { cmdPlugin } from '@karmaniverous/get-dotenv/plugins/cmd';
getdotenv cmd [command...]
Positional tokens are used as the command payload. The plugin resolves scripts and shell settings using shared helpers and executes the child with:
{ ...process.env, ...ctx.dotenv }stdio: inherits by default; enable capture with --capture or GETDOTENV_STDIO=pipe{ cmd, shell } require an explicit shell (omission currently results in shell-off rather than inheriting the root shell)Shell-off note: when --shell-off is active and you run node -e/--eval, the runner preserves argv tokens (and normalizes one layer of outer quotes on the code argument) to avoid lossy re-tokenization across platforms.
The alias allows:
getdotenv -c 'echo $APP_SETTING'
Prefer the alias for npm scripts so flags after -- are routed to getdotenv:
{ "scripts": { "print": "getdotenv -c 'echo $APP_SETTING'" } }
Then:
npm run print -- -e dev
The alias is the cmd subcommand by another name: it runs the cmd action's code path, with the same options bag (createCli rootOptionDefaults, config scripts), the same afterResolve scoping and the same exit codes. When no explicit subcommand is in subcommand position, a parse that gives the alias dispatches to cmd (even where cmd is not the default subcommand). The variadic alias takes every token up to the next option, so a subcommand name inside its value is just part of the command: getdotenv -c echo init runs echo init.
Expansion: the alias value is joined with spaces and dotenv-expanded against process.env once the context is loaded, unless expansion is off. Config plugins.cmd.expand beats the factory's optionAlias.expand; both default to true.
Conflict guard: Supplying both the alias and positional cmd arguments in the same invocation (e.g. getdotenv -c echo -e dev cmd x) is a usage error: it prints --cmd option conflicts with cmd subcommand. (named after the alias's option) and exits 1.
Exit codes: cmd, the default command and the alias exit with the child's exit code. Under the default Windows shell, powershell.exe -Command reports any failure as 1, so the original code is lost there; run with --shell-off (or a POSIX shell) to keep it.
$VAR to avoid outer‑shell expansion.-e/--eval as getdotenv’s --env.Examples:
getdotenv --cmd 'node -e "console.log(process.env.APP_SETTING ?? \"\")"'
getdotenv --shell-off cmd node -e "console.log(process.env.APP_SETTING ?? '')"
You can configure the cmd plugin in getdotenv.config.* under plugins.cmd.
expand?: boolean — Whether to expand the parent alias value (--cmd ...) against the process environment. Default: true.Note: The expand option applies only to the parent alias; positional arguments to the cmd subcommand are never expanded by the plugin (the shell governs them).
Use --trace [keys...] to print concise lines to stderr before spawning the child, indicating the origin and value composition for each key. Useful in CI and for debugging overlays.
Commands may resolve via scripts[name]; only the table's own keys are script names, so getdotenv toString runs toString as typed. When scripts[name] is the object form { cmd, shell }, the effective shell is scripts[name].shell (and when omitted, the script currently runs with shell-off rather than inheriting the root shell):
{
"scripts": {
"bash-only": { "cmd": "echo $SHELL && echo OK", "shell": "/bin/bash" },
"plain": { "cmd": "node -v", "shell": false }
}
}
Then:
getdotenv cmd bash-only
getdotenv cmd plain