Skip to content
Moku
@moku-labs/common pages

API reference

Every export of @moku-labs/common, /browser and /cli, with signatures.

On this page

Entries

ImportFormatFor
@moku-labs/commonESM and CJSNode and Bun: every plugin and every provider.
@moku-labs/common/browserESM onlyClient bundles. No Node module in its import graph.
@moku-labs/common/cliESM and CJSTerminal tools: console, prompts, log sink, ANSI primitives.

Signatures below are simplified for reading. The installed dist/*.d.mts files are the source.

Plugins

Both come from @moku-labs/common and @moku-labs/common/browser.

const logPlugin: CorePluginInstance<"log", LogConfig, LogState, LogApi>;
const envPlugin: CorePluginInstance<"env", EnvConfig, EnvState, EnvApi>;

Register them in createCoreConfig({ plugins }). Config, API and examples are on their pages: log and env.

Env providers

function dotenv(path?: string): EnvProvider;
function processEnv(): EnvProvider;
function cloudflareBindings(): EnvProvider;
function workerSafeProcessEnv(): EnvProvider;
function browserEnv(options?: { globalKey?: string }): EnvProvider;
ProviderNameReadsEntry
dotenvdotenv:<path>The file at path, .env.local by default, on each load()root
processEnvprocess-envA copy of process.envroot
cloudflareBindingscloudflareglobalThis.__CLOUDFLARE_ENV__root
workerSafeProcessEnvworker-process-envA copy of process.env, {} without processroot
browserEnvbrowser-envimport.meta.env, then globalThis[globalKey], default __ENV__root and /browser

Type namespaces

Log and Env hold types only: Log.LogApi, Env.EnvConfig. At run time they are empty objects. The same types are also exported flat, so a framework's inferred types can name them.

TypeWhat it is
LogConfig{ mode }.
LogLevel"debug" | "info" | "warn" | "error".
LogEntry{ level, event, data?, ts, plugin? }.
LogSink{ write(entry) }.
LogApiThe API at ctx.log.
ExpectChainWhat expect() returns.
LogState{ entries, sinks }. Internal.
EnvConfig{ schema, providers, publicPrefix }.
EnvVarSpec{ public, required?, default?, secret? }.
EnvProvider{ name, load() }.
EnvApiThe API at ctx.env.
EnvState{ resolved, publicMap }. Internal.

CLI: console and prompts

From @moku-labs/common/cli. Members and options are on Branded CLI.

function createBrandConsole(options?: BrandConsoleOptions): BrandConsole;
function createBrandPrompts(options?: BrandPromptsOptions): BrandPrompts;
function brandedSink(minLevel?: LogLevel): LogSink;

CLI: primitives

const ANSI: { reset; bold; dim; red; green; yellow; blue; magenta; cyan; gray };
const BRAND_PINK: { r: 255; g: 30; b: 111 };
const CLEAR_LINE: string;
const CLEAR_BELOW: string;
const SPINNER_FRAMES: readonly string[];

function fg24(r: number, g: number, b: number): string;
function cursorUp(n: number): string;
function supportsColor(stream?: { isTTY?: boolean }, noColor?: string): boolean;
function supportsTruecolor(colorTerm?: string): boolean;
function spinnerFrameAt(elapsedMs: number, frameMs?: number): string;
function boxGlyphs(color: boolean): BoxGlyphs;
function visibleWidth(text: string): number;
function makePalette(color: boolean, truecolor?: boolean): Palette;
function box(lines: string[], color: boolean, minInnerWidth?: number): string[];
ExportDescription
ANSISGR codes, each with the ESC byte.
BRAND_PINKThe brand color as an RGB triple.
CLEAR_LINEErases the current line.
CLEAR_BELOWErases from the cursor to the end of the screen.
SPINNER_FRAMESThe braille spinner glyphs.
fg24A 24-bit foreground escape.
cursorUpMoves the cursor up n lines. "" when n is 0 or less.
supportsColortrue on a TTY with NO_COLOR unset.
supportsTruecolortrue when COLORTERM is truecolor or 24bit.
spinnerFrameAtThe frame for an elapsed time, 80 ms per frame by default.
boxGlyphsUnicode corners and edges when color is true, ASCII when false.
visibleWidthString length without ANSI codes.
makePalettepaint, bold, dim, green, yellow, red, cyan, pink. Each returns its input as it is when color is false.
boxReturns framed lines. It does not print.

Types: AnsiCode, BoxGlyphs, BrandConsole, BrandConsoleOptions, BrandPrompts, BrandPromptsOptions, ColorStream, LockupOptions, Palette.