// `@ultimat3/ui`'s palette tables and `defineTheme`, reached at run time. The CLI does not depend on // `@ultimat3/ui` (`error-catalog.ts` names it an optional host for the same reason), and an app that // researches a design has it installed: resolved, the proposal's colours are contrast-checked and // then handed to `defineTheme` itself; unresolved, the proposal carries no colours at all. import type { DesignProposal, ThemeTables } from './design-map'; export interface ThemeHost { readonly tables: ThemeTables; /** `defineTheme`, which throws on a proposal it refuses. */ readonly defineTheme: (input: DesignProposal) => unknown; } const record = (value: unknown): Record | undefined => typeof value === 'object' && value !== null ? (value as Record) : undefined; const stringTable = (value: unknown): Record | undefined => { const table = record(value); if (table === undefined) return undefined; return Object.values(table).every((entry) => typeof entry === 'string') ? (table as Record) : undefined; }; const pairList = (value: unknown): ThemeTables['pairs'] => (Array.isArray(value) ? value : []).flatMap((entry: unknown) => { const pair = record(entry); const fg = pair?.['fg']; const bg = pair?.['bg']; const minimum = pair?.['minimum']; return typeof fg === 'string' && typeof bg === 'string' && typeof minimum === 'number' ? [{ fg, bg, minimum }] : []; }); /** The module's exports read as `unknown` and checked: a version that renamed one is no host. */ export function themeHostOf(module: unknown): ThemeHost | undefined { const ui = record(module); const tokens = record(ui?.['colorTokens']); const light = stringTable(tokens?.['light']); const dark = stringTable(tokens?.['dark']); const aliases = stringTable(ui?.['COLOR_ALIASES']); const contrast = ui?.['contrastRatio']; const define = ui?.['defineTheme']; if ( light === undefined || dark === undefined || aliases === undefined || typeof contrast !== 'function' || typeof define !== 'function' ) { return undefined; } return { tables: { colors: { light, dark }, aliases, pairs: [...pairList(ui?.['CONTRAST_PAIRS']), ...pairList(ui?.['ALIAS_CONTRAST_PAIRS'])], contrast: (a, b) => Number(contrast(a, b)), }, defineTheme: (input) => define(input), }; } /** The specifier is a value, not a literal: `tsc` must not resolve a package the CLI never declares. */ const UI_SPECIFIER = '@ultimat3/ui'; export async function loadThemeHost( load: (specifier: string) => Promise = (specifier) => import(specifier), ): Promise { try { return themeHostOf(await load(UI_SPECIFIER)); } catch { // Not installed beside the CLI: the documented degraded path, reported in reference.json. return undefined; } } /** * The proposal `defineTheme` accepts, by `defineTheme`'s own verdict: as mapped, else without its * colours (a palette rule this CLI predates), else `{}` marked unvalidated — never a proposal that * would not boot, and never a thrown run. */ export function acceptedProposal( proposal: DesignProposal, host: ThemeHost | undefined, ): { readonly proposal: DesignProposal; readonly validated: boolean; } { if (host === undefined) return { proposal, validated: false }; if (accepts(host, proposal)) return { proposal, validated: true }; const { colors: _dropped, ...rest } = proposal; if (accepts(host, rest)) return { proposal: rest, validated: true }; // A rule this CLI does not know refuses more than colours: an empty proposal is still a // `defineTheme` argument, and a research run never dies over a token it could not validate. return { proposal: {}, validated: false }; } const accepts = (host: ThemeHost, proposal: DesignProposal): boolean => { try { host.defineTheme(proposal); return true; } catch { return false; } };