clifn
Reusable CLI primitives for Superfunctions packages and downstream CLIs.
clifn remains a single package at clifn/core, published as @clifn/core. It is the shared parser-agnostic CLI toolkit for generic concerns that repeat across Superfunctions CLIs, while each owning CLI keeps its product-specific command graph and behavior.
Current public subpaths:
@clifn/core/credentials
@clifn/core/config
@clifn/core/client
@clifn/core/ui
@clifn/core/stdio
@clifn/core/prompt
New additive subpaths are exported for the generic CLI-builder surface:
@clifn/core/output
@clifn/core/runner
@clifn/core/config-loader
@clifn/core/env
@clifn/core/diagnostics
@clifn/core/exec
@clifn/core/scaffold
@clifn/core/paths
@clifn/core/terminal
Install
npm install @clifn/core
Conduct CLI-style usage
import { createCredentialStore } from "@clifn/core/credentials";
import { createProjectConfig } from "@clifn/core/config";
import { createApiClient } from "@clifn/core/client";
import { readJsonStdin, writeJsonStdout } from "@clifn/core/stdio";
const credentials = createCredentialStore();
const config = createProjectConfig();
const profile = String(config.get("profile") ?? "default");
const api = createApiClient({
credentials,
profile,
projectId: String(config.get("projectId") ?? ""),
});
const input = await readJsonStdin<{ command: string }>();
process.stderr.write(`running command: ${input.command}\n`);
const result = await api.post("/runs", input);
writeJsonStdout(result.data);
Generic CLI usage
import { prompt } from "@clifn/core/prompt";
import { ui } from "@clifn/core/ui";
const target = await prompt.select("Target environment", [
"local",
"staging",
"production",
]);
const confirmed = await prompt.confirm(`Deploy to ${target}?`);
if (!confirmed) {
ui.warn("deployment cancelled");
} else {
ui.success(`deployment started for ${target}`);
}
Shared CLI-builder quick start
import { runAction } from "@clifn/core/runner";
const exitCode = await runAction(
async ({ name }, ctx) => {
ctx.output.info(`hello ${name}`);
},
{ name: "world" },
{
verbose: false,
}
);
process.exitCode = exitCode;
Parser compatibility patterns
clifn stays parser-agnostic. The owning CLI keeps parser choice and parser dependencies, while clifn supplies the reusable execution and output primitives underneath.
The current repository already uses all three parser styles:
hostfn/cli, plugfn/cli, and extfn/cli use commander
apifn/cli uses cac
datafn/cli uses raw node:util.parseArgs
The parser canaries under clifn/core/tests/fixtures/* prove that runAction() and the shared output service work in each style without adding parser libraries to clifn runtime dependencies.
commander
import { Command } from "commander";
import { runAction } from "@clifn/core/runner";
const program = new Command();
program.option("--json", "Emit machine-readable output");
program
.command("greet")
.option("--name <name>", "Name to greet", "world")
.action(async (options, command) => {
const globals = command.parent?.opts() ?? {};
process.exitCode = await runAction(
async ({ name }, ctx) => {
ctx.output.info(`hello ${name}`);
},
{ name: options.name },
{
mode: globals.json ? "json" : "text",
}
);
});
await program.parseAsync(process.argv);
cac
import { cac } from "cac";
import { runAction } from "@clifn/core/runner";
const cli = cac("demo");
cli.option("--json", "Emit machine-readable output");
cli
.command("greet")
.option("--name <name>", "Name to greet")
.action(async (options) => {
process.exitCode = await runAction(
async ({ name }, ctx) => {
return {
data: {
ok: true,
name,
},
};
},
{ name: options.name ?? "world" },
{
mode: options.json ? "json" : "text",
}
);
});
cli.parse(process.argv, { run: false });
await cli.runMatchedCommand();
node:util.parseArgs
import { parseArgs } from "node:util";
import { runAction } from "@clifn/core/runner";
const { values, positionals } = parseArgs({
allowPositionals: true,
options: {
name: { type: "string" },
json: { type: "boolean" },
},
});
if (positionals[0] === "greet") {
process.exitCode = await runAction(
async ({ name }, ctx) => {
ctx.output.info(`hello ${name}`);
},
{ name: values.name ?? "world" },
{
mode: values.json ? "json" : "text",
}
);
}
Consumer ownership stays explicit:
clifn owns the parser-agnostic runner/output/diagnostics/config/env/exec/scaffold helpers
- the consuming CLI owns command registration, parser wiring, and parser package installation
- parser libraries such as
commander and cac remain consumer dependencies or test-only canary dependencies, not clifn runtime dependencies
API Overview
Compatibility promise
- Existing imports for
@clifn/core/credentials, @clifn/core/config, @clifn/core/client, @clifn/core/ui, @clifn/core/stdio, and @clifn/core/prompt remain valid.
- The new subpaths are additive and do not replace any existing import path.
@clifn/core/ui remains the compatibility-facing terminal helper path even as newer shared output primitives land in @clifn/core/output.
@clifn/core/credentials
createCredentialStore(path?) -> INI profile store.
createCredentialStore({ appName }) -> the same store at <config>/credentials from resolveAppPaths(appName); { path } equals the string form. Without an argument the Conduct default ~/.conduct/credentials stays.
MissingProfileError -> typed error when profile is not found.
@clifn/core/config
createProjectConfig(path?) -> JSON project config store.
createProjectConfig({ appName, fileName? }) -> <config>/<fileName> (default config.json); { cwd?, fileName } -> a project-local file; { path } equals the string form. Without an argument the Conduct default conduct.config.json stays.
{ appName } stores resolve only the config directory for the current platform (env and homedir may be passed through, a different platform throws); fileName must stay inside its directory. Only own properties of the options object are read (enumerable or not; inherited keys are ignored). The selector of the form (appName, path, or fileName for a project-local file) must be a string, and an optional key set to undefined (fileName beside appName, cwd, env, homedir) takes its default. A selector set to undefined or a non-string, a key from another form, or an unknown key throws InvalidStoreOptionsError (CLIFN_INVALID_STORE_OPTIONS) instead of falling back to another form or the Conduct default. Every own property descriptor is captured before any getter runs, and each getter is called once. An object-form target must name a file: { path: "" }, a path ending in a separator or a root (C:\, \\server\share), . or .., and a NUL character in path, fileName or cwd throw InvalidStoreOptionsError before any filesystem call; the string form is used as given. fileName cannot start with a drive (C:) before or after normalization (./C:x). On Windows no fileName segment, and not the base name of an object-form target, may contain :, be a device name (nul, CON.json) or end with a dot or space, since Windows would open a device or another name. Loading the module does not fail when the home or working directory is unavailable; the no-argument defaults are still taken at import when they can be.
InvalidConfigError -> typed error for invalid JSON/object shapes.
@clifn/core/client
createApiClient(config) -> authenticated HTTP client with retries.
HttpFailureError -> typed error for non-2xx responses.
HttpRequestError -> typed error for network/transport failures.
@clifn/core/ui
ui.success, ui.error, ui.warn, ui.info
ui.spinner(message) -> start/stop/succeed/fail
ui.table(rows) -> deterministic table output
@clifn/core/stdio
readJsonStdin<T>() -> parse one JSON document from stdin.
writeJsonStdout(value) -> serialize one JSON document + trailing newline.
InvalidJsonStdinError -> typed deterministic parse failure.
@clifn/core/prompt
prompt.select
prompt.multiSelect
prompt.text
prompt.confirm
createPrompt({ ask?, output?, nonInteractive? }): with nonInteractive: true every prompt throws PromptInputError with code CLIFN_PROMPT_NON_INTERACTIVE; an options object that names output or nonInteractive writes questions to output, else stderr. createPrompt(), createPrompt({ ask }) and prompt keep writing to stdout.
New shared CLI-builder modules
These module entrypoints are the generic surface that repository CLIs can share: