UNPKG

kist

Version:

Package Pipeline Processor

146 lines (145 loc) 6.49 kB
"use strict"; // ============================================================================ // Import // ============================================================================ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Kist = void 0; const AbstractProcess_1 = require("./core/abstract/AbstractProcess"); const ConfigStore_1 = require("./core/config/ConfigStore"); const ActionRegistry_1 = require("./core/pipeline/ActionRegistry"); const PipelineManager_1 = require("./core/pipeline/PipelineManager"); const LiveServer_1 = require("./live/LiveServer"); const LiveWatcher_1 = require("./live/LiveWatcher"); // ============================================================================ // Class // ============================================================================ /** * The Kist class encapsulates the kist CLI functionality. * It manages the pipeline execution, configuration loading, and live reload. */ class Kist extends AbstractProcess_1.AbstractProcess { // Constructor // ======================================================================== /** * Constructs the Kist class instance and initializes necessary components. */ constructor() { super(); this.logDebug("Kist initialized."); } // Methods // ======================================================================== /** * Executes the Kist workflow. * * This method orchestrates the execution of the Kist pipeline, starting * from initializing the ActionRegistry, loading configuration settings, * running the pipeline stages through the `PipelineManager`, and * optionally enabling live reload for real-time updates. * * @returns {Promise<void>} Resolves when the workflow completes successfully. * @example * const Kist = new Kist(); * Kist.run().then(() => console.log("Pipeline execution complete.")); */ run() { return __awaiter(this, void 0, void 0, function* () { this.logInfo("Starting Kist workflow..."); try { // Initialize the ActionRegistry with available actions this.initializeActionRegistry(); // Create and run the PipelineManager const liveReloadEnabled = ConfigStore_1.ConfigStore.getInstance().get("options.live.enabled"); const liveReloadServer = liveReloadEnabled ? new LiveServer_1.LiveServer() : null; const pipelineManager = new PipelineManager_1.PipelineManager(liveReloadServer); yield pipelineManager.runPipeline(); // Setup live reload if enabled if (liveReloadEnabled) { this.setupLiveReload(pipelineManager, liveReloadServer); } } catch (error) { this.handleError(error); } }); } /** * Initializes the ActionRegistry with available actions. * Automatically registers core actions and discovers external plugins. */ initializeActionRegistry() { this.logInfo("Initializing ActionRegistry..."); ActionRegistry_1.ActionRegistry.initialize(); this.logInfo("ActionRegistry initialized successfully."); } /** * Sets up live reload functionality. * Monitors file changes and restarts the pipeline when updates are detected. * * @param pipelineManager - The manager responsible for the pipeline process. * @param liveReloadServer - The server for live reload connections. */ setupLiveReload(pipelineManager, liveReloadServer) { this.logInfo("Enabling live reload functionality..."); new LiveWatcher_1.LiveWatcher((filePath) => { this.logInfo(`Detected change in: ${filePath}. Restarting pipeline...`); pipelineManager.restartPipelineWithDelay(500); }); pipelineManager.restartPipeline(); this.registerShutdownHandlers(pipelineManager, liveReloadServer); } /** * Registers handlers for graceful shutdown signals. * * @param pipelineManager - The manager responsible for the pipeline process. * @param liveReloadServer - The server for live reload connections. */ registerShutdownHandlers(pipelineManager, liveReloadServer) { process.on("SIGINT", () => this.handleShutdown(pipelineManager, liveReloadServer)); process.on("SIGTERM", () => this.handleShutdown(pipelineManager, liveReloadServer)); } /** * Handles graceful shutdown of the pipeline and live reload server. * * @param pipelineManager - The manager responsible for the pipeline process. * @param liveReloadServer - The server for live reload connections. */ handleShutdown(pipelineManager, liveReloadServer) { return __awaiter(this, void 0, void 0, function* () { this.logInfo("Shutdown signal received. Shutting down..."); try { yield pipelineManager.stopPipeline(); yield liveReloadServer.shutdown(); this.logInfo("Shutdown completed successfully."); } catch (error) { this.logError("Error during shutdown.", error); } finally { process.exit(0); } }); } /** * Handles errors occurring during the execution of the Kist workflow. * * @param error - The error object to log and handle. */ handleError(error) { const errorMessage = error instanceof Error ? error.message : String(error); this.logError(`An error occurred: ${errorMessage}`, error); process.exit(1); } } exports.Kist = Kist;