import type { FlagDef, FlagMapping, MigrationPlan, ParsedFlags, ResolvedChxConfig, SafeParseable, SchemaDefinition, } from '@chkit/core' import type { ClickHouseExecutor } from '@chkit/clickhouse' export { defineFlags, parseFlags, typedFlags, MissingFlagValueError, UnknownFlagError, type FlagDef, type FlagMapping, type InferFlags, type ParsedFlags, type SafeParseable, } from '@chkit/core' export interface TableScope { enabled: boolean selector?: string matchedTables: string[] matchCount: number } export interface LoadedPlugin { options: Record rawOptions: Record plugin: ChxPlugin /** * True for built-in plugins (e.g. `core`) loaded by the CLI itself rather * than from the user's config. Their hook/command errors are surfaced raw, * without the `Plugin "" failed in ...` wrapper that only makes sense * for third-party plugins the user explicitly registered. */ internal?: boolean } export interface PluginRuntime { plugins: ReadonlyArray getCommand( pluginName: string, commandName: string, ): { command: ChxPluginCommand; plugin: LoadedPlugin } | null resolveContext( input: Omit, ): Promise disposeContext(ctx: PluginContext): Promise runOnInit(context: Omit): Promise runOnComplete( context: Omit & { exitCode?: number }, ): Promise runOnConfigLoaded( context: Omit & { tableScope?: TableScope }, ): Promise runOnSchemaLoaded( context: ChxOnSchemaLoadedContext, ): Promise runOnSnapshotLoaded( context: ChxOnSchemaLoadedContext, ): Promise runOnPlanCreated( context: Omit & { tableScope?: TableScope }, plan: MigrationPlan, ): Promise runOnBeforeApply(context: ChxOnBeforeApplyContext): Promise runOnAfterApply(context: ChxOnAfterApplyContext): Promise runOnCheck( context: Omit & { tableScope?: TableScope }, ): Promise runOnCheckReport( results: ChxOnCheckResult[], print: (line: string) => void, ): Promise runOnBeforePluginCommand( pluginName: string, commandName: string, context: Omit< ChxOnBeforePluginCommandContext, 'targetPlugin' | 'command' | 'options' >, ): Promise runPluginCommand( pluginName: string, commandName: string, context: Omit< ChxPluginCommandContext, 'pluginName' | 'options' | 'rawOptions' | 'tableScope' | 'pluginRuntime' | 'pluginContext' > & { tableScope?: TableScope }, ): Promise } export interface PluginContext { executor: ClickHouseExecutor /** True when an executor was provided (either from config or a plugin). False when using the null executor. */ hasExecutor: boolean } export interface ChxGetContextInput { config: ResolvedChxConfig configPath: string command: string flags: ParsedFlags defaults: PluginContext } export interface CommandExtension { command: string | string[] flags: readonly FlagDef[] } export interface ChxPluginManifest { name: string apiVersion: 1 version?: string compatibility?: { cli?: { minMajor?: number maxMajor?: number } } } export interface ChxPluginHookContextBase { command: string config: ResolvedChxConfig tableScope: TableScope flags: ParsedFlags } export interface ChxOnConfigLoadedContext extends ChxPluginHookContextBase { configPath: string options: Record } export interface ChxOnSchemaLoadedContext extends ChxPluginHookContextBase { jsonMode?: boolean definitions: SchemaDefinition[] } export interface ChxOnPlanCreatedContext extends ChxPluginHookContextBase { plan: MigrationPlan } export interface ChxOnBeforeApplyContext extends ChxPluginHookContextBase { migration: string sql: string statements: string[] } export interface ChxOnAfterApplyContext extends ChxPluginHookContextBase { migration: string statements: string[] appliedAt: string } export interface ChxOnInitContext { command: string configPath: string isInteractive: boolean jsonMode: boolean flags: ParsedFlags config: ResolvedChxConfig options: Record } export interface ChxOnCompleteContext { command: string isInteractive: boolean jsonMode: boolean exitCode: number options: Record } export interface ChxCheckFinding { code: string message: string severity: 'info' | 'warn' | 'error' metadata?: Record } export interface ChxOnCheckContext extends ChxPluginHookContextBase { command: 'check' configPath: string jsonMode: boolean options: Record } export interface ChxOnCheckResult { plugin: string evaluated: boolean ok: boolean findings: ChxCheckFinding[] metadata?: Record } export interface ChxOnBeforePluginCommandContext { targetPlugin: string command: string config: ResolvedChxConfig configPath: string jsonMode: boolean args: string[] flags: ParsedFlags options: Record tableScope: TableScope print: (value: unknown) => void } export type ChxOnBeforePluginCommandResult = | { handled: true; exitCode: number } | { handled: false } export interface ChxPluginCommandContext> { pluginName: string config: ResolvedChxConfig configPath: string jsonMode: boolean args: string[] flags: ParsedFlags options: TOptions rawOptions: Record tableScope: TableScope print: (value: unknown) => void pluginRuntime: PluginRuntime pluginContext: PluginContext } export interface ChxPluginCommand> { name: string description?: string flags?: readonly FlagDef[] optionsSchema?: SafeParseable flagMapping?: FlagMapping /** * `false` when the command takes no positional arguments: the CLI then * rejects any, before or after `--`, as it does for its own commands, and * the command does not run (#659). Unset, the command gets them in `args`. */ takesArgs?: boolean run: ( context: ChxPluginCommandContext, ) => undefined | number | Promise } export interface ChxPluginHooks { getContext?: ( input: ChxGetContextInput ) => PluginContext | Partial | undefined | Promise | undefined> onInit?: (context: ChxOnInitContext) => void | Promise onComplete?: (context: ChxOnCompleteContext) => void | Promise onBeforePluginCommand?: ( context: ChxOnBeforePluginCommandContext ) => ChxOnBeforePluginCommandResult | Promise onConfigLoaded?: (context: ChxOnConfigLoadedContext) => void | Promise onSchemaLoaded?: ( context: ChxOnSchemaLoadedContext ) => SchemaDefinition[] | undefined | Promise /** * Runs in `generate` on the previous snapshot's definitions before they are * compared with the schema. A plugin that rewrites the schema in * `onSchemaLoaded` applies the same rewrite here, so a snapshot written * before the rewrite existed does not plan a change. Must not log or throw * for definitions the schema no longer has. */ onSnapshotLoaded?: ( context: ChxOnSchemaLoadedContext ) => SchemaDefinition[] | undefined | Promise onPlanCreated?: ( context: ChxOnPlanCreatedContext ) => MigrationPlan | undefined | Promise onBeforeApply?: ( context: ChxOnBeforeApplyContext ) => | undefined | { statements?: string[] } | Promise onAfterApply?: (context: ChxOnAfterApplyContext) => void | Promise onCheck?: (context: ChxOnCheckContext) => ChxOnCheckResult | undefined | Promise onCheckReport?: (context: { result: ChxOnCheckResult print: (line: string) => void }) => void | Promise } export interface ChxPlugin { manifest: ChxPluginManifest optionsSchema?: SafeParseable> hooks?: ChxPluginHooks commands?: ChxPluginCommand[] extendCommands?: CommandExtension[] } export function definePlugin(plugin: ChxPlugin): ChxPlugin { return plugin }