CLI building blocks
Each product ships an xtandard-<product> binary with its own command table. @xtandard/kit/cli
holds the parts every CLI re-implemented, and drifted on: env reading, argv parsing, auth and
storage from env, the startup banner, and a server that runs under Bun or Node.
env and isTruthy
Section titled “env and isTruthy”import { env, isTruthy } from "@xtandard/kit/cli";
env("PORT", "3000"); // empty counts as unset: `PORT=` → "3000"isTruthy(env("READONLY")); // "1" | "true" (any case)webhooks, flags, audit and links used ??, so an empty variable in a .env file became "". The
kit uses ||, like entitlements.
parseArgs
Section titled “parseArgs”parseArgs(["serve", "--port=4000", "--readonly", "-t", "acme"], { short: { t: "tenant", h: "help", v: "version" },});// { _: ["serve"], flags: { port: "4000", readonly: true, tenant: "acme" } }- Supports
--k v,--k=v(the value may contain=) and--flag. - Short flags come from
short(links);-h/-vare the default. booleanflags never take a value, so--help cmdkeepscmdpositional.--ends the flags, and a malformed--=xstays positional (entitlements’ guard).- Only
--…and a configured short flag (-t) start a new flag, so--limit -1,--value -5and--reason -keep their values. helpandversionare boolean by default, so--version 1would not take a value. A CLI whose commands use--version <n>passesboolean: []and handles-h/-vitself, or names them inshortonly.
buildAuthFromEnv
Section titled “buildAuthFromEnv”const { auth, authorization, mode } = await buildAuthFromEnv({ realm: "xtandard-webhooks", roles: { policy: DEFAULT_ROLE_POLICY, actions }, // used with AUTH_MODE=basic});AUTH_MODE |
Result |
|---|---|
none, empty, unset |
noAuth() + noAuthorization() |
basic |
one user: AUTH_USERNAME (admin), AUTH_PASSWORD_HASH or AUTH_PASSWORD, role AUTH_ROLE (admin), the product’s roles provider |
| anything else | throws Unknown AUTH_MODE "…" |
A typo such as basci used to open the panel in four products. It was hot-fixed in each of them,
and is now enforced here.
storageFromEnv
Section titled “storageFromEnv”Storage drivers are configured per role:
| Product | Roles | Driver variable(s) |
|---|---|---|
| webhooks | STORAGE, QUEUE |
STORAGE_DRIVER, QUEUE_STORAGE_DRIVER |
| flags | SOURCE, RUNTIME |
SOURCE_STORAGE_DRIVER, RUNTIME_STORAGE_DRIVER |
| audit, links | STORAGE |
STORAGE_DRIVER |
| entitlements | STORAGE, COUNTERS |
STORAGE_DRIVER, COUNTERS_DRIVER (driverEnv) |
const storage = await storageFromEnv<WebhooksStorage>({ role: { name: "STORAGE", defaults: { // persisted defaults are the product's (ADR 0005) prefix: "xtandard:webhooks:storage", pgTable: "xtandard_webhooks_storage", mongoDb: "xtandard_webhooks", mongoCollection: "webhooks_storage", sqlitePath: "./.webhooks/storage.sqlite", sqliteTable: "xtandard_webhooks", // sqlite and libsql libsqlUrl: "file:./.webhooks/storage.db", fileDir: "./.webhooks/storage", }, }, allowed: ["memory", "file", "redis", "postgres", "mongodb", "sqlite", "libsql"], // optional driver: flags.storage as string | undefined, // optional: overrides the env var (still validated)});The variable names are the products’ own:
REDIS_URLand<ROLE>_PREFIX;DATABASE_URL(falling back toPOSTGRES_URL) and<ROLE>_PG_TABLE;MONGO_URL,MONGO_DBand<ROLE>_MONGO_COLLECTION;<ROLE>_SQLITE_PATH, and<ROLE>_SQLITE_TABLEfor thesqliteandlibsqltable;<ROLE>_LIBSQL_URL(falling back toLIBSQL_URL) andLIBSQL_AUTH_TOKEN;<ROLE>_FILE_DIR.
Behaviour:
- An unknown driver throws. webhooks, flags and links used to fall back to
file. - The default registry imports the kit’s storage entries lazily, so a peer such as
redisloads only when its driver is chosen. A driver missing from the build says so; other import errors propagate. - The registry’s drivers are
memory,file,sqlite,libsql,postgres,mongodb,redis,redis-jsonandunstorage. They use the products’ factory names and options (createPostgresStorage({ connectionString, table })…). - Pass
driversto add drivers or replace them, e.g. a product’s counters. - Redis stores strings with
redisand RedisJSON documents withredis-json. The value format is chosen by the driver name.
Pass every persisted name
Section titled “Pass every persisted name”The kit’s drivers default to neutral names: the xtandard_kv table and channel, and no Redis
prefix. A product CLI that leaves a name out would silently write to the kit’s default and
miss the data its earlier releases stored (ADR 0005). Use one of two approaches:
-
Role defaults. Set every field a driver persists under:
prefix,pgTable,mongoDb,mongoCollection,sqlitePath,sqliteTable,libsqlUrlandfileDir. Use the exact values the product’s previous CLI used. The env vars still override them per deployment. -
The product’s own registry (required when its driver wrappers add capabilities). Build
driversfrom the product’s own entries withstorageRegistryFrom. It applies the kit’s env → options mapping to the product’s wrapped factories, so persisted defaults and capability layers (webhooks’claimDue) survive.defaultStorageRegistryimports the kit’s bare drivers and bypasses both:import { storageFromEnv, storageRegistryFrom } from "@xtandard/kit/cli";const drivers = storageRegistryFrom({memory: () => import("./entry-storage-memory.ts"),file: () => import("./entry-storage-file.ts"),postgres: () => import("./entry-storage-postgres.ts"),redis: () => import("./entry-storage-redis.ts"),});const storage = await storageFromEnv({ role, drivers });
test/server-cli-registry.test.ts and test/server.bun.test.ts check, against the real drivers,
that the role defaults reach them:
- the file directory;
- the libSQL and SQLite tables;
- the Postgres table;
- the Mongo database and collection;
- the Redis prefix, plus string vs RedisJSON.
describeStorage(role) gives the banner line:
- the absolute path for
file,sqliteandfile:libSQL URLs; memory (ephemeral — not persisted);- the driver name only for everything else. Connection strings, which may contain credentials, are never printed.
serve and startServer
Section titled “serve and startServer”await serve({ name: "xtandard/webhooks", port: flags.port ? Number(flags.port) : undefined, buildPanel: async ({ basePath, title, logoUrl, readonly, env }) => createFetchHandler({ storage, auth, authorization, basePath, title, logoUrl, readonly }), banner: async () => [`storage: ${await describeStorage(STORAGE)}`],}); // never resolves: the server owns the processservereadsPORT(3000),BASE_PATH(normalized),TITLE,LOGO_URLandREADONLY.- It warns on
AUTH_MODE=none, prints the banner andlistening on http://localhost:<port><base>. startServer(port, fetch)usesBun.serveunder Bun. Under Node it usesnode:httpover the shared Node bridge, which keeps everySet-Cookieand streams responses. It returns{ port, stop }.
pkgVersion and the self-run guard
Section titled “pkgVersion and the self-run guard”import { isMainModule, pkgVersion } from "@xtandard/kit/cli";
export async function run(argv: string[]): Promise<number> { if (flags.version) return void console.log(await pkgVersion(import.meta.url)), 0; …}
// au 6761a34: run only when executed directly, not when imported// (by bin/xtandard-<product>.mjs, the standalone app, or a test).if (isMainModule(import.meta.url)) run(process.argv.slice(2)).then((code) => process.exit(code));isMainModuletakesimport.meta.url, or the wholeimport.metato useimport.meta.mainwhere the runtime has it. Prefer the URL: a bareimport.metamakes the CJS build warn (EMPTY_IMPORT_META). With the URL, it compares the module withprocess.argv[1].pkgVersion(import.meta.url)reads../package.json, which is right for bothdist/cli.mjsandsrc/cli.ts. It returns"unknown"when the file can’t be read.