/** * @file Static Statechart Extractor for @doeixd/machine * @description * This build-time script uses the TypeScript Compiler API via `ts-morph` to analyze * your machine source code. It reads the "type-level metadata" encoded by the * primitives (`transitionTo`, `guarded`, etc.) and generates a formal, JSON-serializable * statechart definition compatible with tools like Stately Viz. * * This script does NOT execute your code. It performs a purely static analysis of the types. * * @usage * 1. Ensure you have `ts-node` and `ts-morph` installed: `npm install -D ts-node ts-morph` * 2. Create a configuration object or use .statechart.config.ts * 3. Run the script from your project root: `npx ts-node ./scripts/extract-statechart.ts` */ import { Project, Type } from 'ts-morph'; /** * Configuration for a parallel region */ export interface ParallelRegionConfig { /** A unique name for this region (e.g., 'fontStyle') */ name: string; /** The initial state class for this region */ initialState: string; /** All reachable state classes within this region */ classes: string[]; } /** * Configuration for child states in a hierarchical machine */ export interface ChildStatesConfig { /** The property in the parent's context that holds the child machine */ contextProperty: string; /** An array of all possible child state class names */ classes: string[]; /** The initial child state */ initialState: string; } /** * Configuration for a single machine to extract */ export interface MachineConfig { /** Path to the source file containing the machine */ input: string; /** Output file path (optional, defaults to stdout) */ output?: string; /** Top-level ID for the statechart */ id: string; /** Optional description of the machine */ description?: string; /** Array of class names that represent states (for simple FSM) */ classes?: string[]; /** Name of the class that represents the initial state (for simple FSM) */ initialState?: string; /** Configuration for parallel regions (mutually exclusive with initialState/classes) */ parallel?: { regions: ParallelRegionConfig[]; }; /** Configuration for hierarchical/nested states */ children?: ChildStatesConfig; } /** * Global extraction configuration */ export interface ExtractionConfig { /** Array of machines to extract */ machines: MachineConfig[]; /** Validate output against XState JSON schema (optional) */ validate?: boolean; /** Output format (json, mermaid, or both) */ format?: 'json' | 'mermaid' | 'both'; /** Watch mode - auto-regenerate on file changes */ watch?: boolean; /** Verbose logging */ verbose?: boolean; } /** * Recursively traverses a `ts-morph` Type object and serializes it into a * plain JSON-compatible value. It's smart enough to resolve class constructor * types into their string names. * * Note: This function is kept for future extensibility but is not currently used * as the AST-based extraction approach (via extractFromCallExpression) is preferred. * * @param type - The `ts-morph` Type object to serialize. * @param verbose - Enable debug logging * @returns A JSON-compatible value (string, number, object, array). * @internal */ export declare function _typeToJson(type: Type, verbose?: boolean): any; /** * Extracts a single machine configuration to a statechart * * @param config - Machine configuration * @param project - ts-morph Project instance * @param verbose - Enable verbose logging * @returns The generated statechart object */ export declare function extractMachine(config: MachineConfig, project: Project, verbose?: boolean): any; /** * Extracts multiple machines based on configuration * * @param config - Full extraction configuration * @returns Array of generated statecharts */ export declare function extractMachines(config: ExtractionConfig): any[]; /** * Legacy function for backwards compatibility * Extracts a single hardcoded machine configuration * @deprecated Use extractMachine or extractMachines instead */ export declare function generateChart(): void; /** * Example configuration demonstrating hierarchical and parallel machines. * This is not used by default but serves as documentation. */ export declare const ADVANCED_CONFIG_EXAMPLES: { hierarchical: MachineConfig; parallel: MachineConfig; }; //# sourceMappingURL=extract.d.ts.map