Skip to content

Core Functions

Entry point for all SysopKit scripts. Creates a root execution context, sets up a reporter and abort handling, and runs the provided function.

import { start } from 'sysopkit/start';
async function start<R>(fn: () => Promise<R>, options?: StartOptions): Promise<StartResult<R>>;
OptionTypeDefaultDescription
reporterReporterConsoleReporterCustom reporter for output
dryRunbooleanSYSOPKIT_DRY_RUN env varPrevent actual changes
signalAbortSignalExternal signal for cancellation
varsRecord<symbol | string, any>Initial context variables
type StartResult<T> =
| { success: true; result: T; duration: number }
| { success: false; error: unknown; duration: number };

Always returns a result — never throws. Check result.success to determine outcome.


Creates a named child context for a user-facing operation. Task output appears at normal verbosity.

import { task } from 'sysopkit';
async function task<R>(
name: string,
fn: (ctx: ExecutionContext) => Promise<R>,
opts?: TaskOptions,
): Promise<R>;
OptionTypeDescription
detailsstring | (() => string | Record)Additional context info for reporter
verbosityVerbosityOverride verbosity for this task
varsRecord<string, any>Scoped context variables
signalAbortSignalScoped abort signal

Creates a named child context for internal helper operations. Output is suppressed at normal verbosity (shown at debug level).

import { utility } from 'sysopkit';
async function utility<R>(
name: string,
fn: (ctx: ExecutionContext) => Promise<R>,
options?: UtilityOptions,
): Promise<R>;
OptionTypeDescription
varsRecord<symbol | string, any>Scoped context variables
signalAbortSignalScoped abort signal

Returns the current ExecutionContext. Throws if called outside a start() scope.

import { context } from 'sysopkit';
function context(): ExecutionContext;
PropertyTypeDescription
typeContextType'root' | 'apply' | 'connector' | 'middleware' | 'utility' | 'task'
parentExecutionContext | nullParent context in the tree
reporterReporterShared reporter instance
connConnector | nullActive connector (null in root)
dryRunbooleanDry-run mode flag
namestringContext name
varsRecord<symbol | string, any>Context variables
signalAbortSignalAbort signal for cancellation
verbosityVerbosityCurrent verbosity level
MethodDescription
tryGet(key)Get variable by key, returns undefined if absent
get(key)Get variable by key, throws if absent
abort(reason?)Abort execution with optional reason
on(event, handler)Register event handler for this context
info(msg)Log at info level
warn(msg)Log at warning level
error(msg)Log at error level

Dispatches an event that propagates up the context chain. Registered handlers at each level are called.

import { emit } from 'sysopkit';
function emit<T>(event: Event<T>, data: T): void;

Convenience function for operations to report infrastructure modifications. Emits a CHANGE_EVENT with the provided entry or entries.

import { emitChanged } from 'sysopkit';
function emitChanged(entries: ChangeEntry | ChangeEntry[]): void;

Registers a change listener for the duration of a function. The handler is called whenever a CHANGE_EVENT is emitted within the scope.

import { onChange } from 'sysopkit';
async function onChange<R>(
handler: (event: ChangeEntry | ChangeEntry[]) => void,
fn: () => R | Promise<R>,
): Promise<R>;

Returns a closure that starts false and flips to true on the first truthy call. Used with onChange() to detect whether changes occurred.

import { latch } from 'sysopkit';
function latch<T>(): (value?: T) => boolean;
const changed = latch();
// onChange will call changed(true) on any change
await onChange(changed, async () => {
await createFile({ path: '/etc/conf', content: 'data' });
});
if (changed()) {
// React to changes
}

Wraps a connector with middleware for the duration of a function. The middleware factory receives the current connector and context, and returns a new connector.

import { middleware } from 'sysopkit';
function middleware<R>(
name: string,
fn: (ctx: ExecutionContext) => Promise<R>,
wrap: (next: Connector, ctx: ExecutionContext) => Connector,
options?: MiddlewareContextOptions,
): Promise<R>;