UNPKG

kist

Version:

Lightweight Package Pipeline Processor with Plugin Architecture

93 lines 3.38 kB
import { spawn } from "child_process"; import path from "path"; import { AbstractProcess } from "../abstract/AbstractProcess.js"; import { ConfigStore } from "../config/ConfigStore.js"; import { Pipeline } from "./Pipeline.js"; export class PipelineManager extends AbstractProcess { liveServer; pipelineProcess = null; isRestarting = false; constructor(liveServer) { super(); this.liveServer = liveServer; this.logInfo("PipelineManager initialized."); } async runPipeline() { const config = ConfigStore.getInstance().getConfig(); if (!config.stages || !Array.isArray(config.stages)) { throw new Error("Invalid configuration: 'stages' must be an array."); } this.logInfo("Initializing pipeline..."); const pipeline = new Pipeline(config); try { await pipeline.run(); this.logInfo("Pipeline execution finished successfully."); this.liveServer?.reloadClients(); } catch (error) { this.logError("Error during pipeline execution.", error); throw error; } } restartPipeline() { if (this.isRestarting) { this.logWarn("Pipeline restart already in progress. Skipping..."); return; } this.isRestarting = true; if (this.pipelineProcess) { this.logInfo("Stopping current pipeline process..."); this.stopPipeline(); } this.logInfo("Starting pipeline..."); const scriptPath = path.resolve(process.cwd(), "dist/js/cli.js"); this.pipelineProcess = spawn("node", [scriptPath], { stdio: "inherit", }); this.attachProcessListeners(); this.isRestarting = false; } attachProcessListeners() { if (!this.pipelineProcess) return; this.pipelineProcess.on("close", (code) => { if (code !== 0) { this.logError(`Pipeline process exited with code ${code}`); } else { this.logInfo("Pipeline process exited successfully."); } this.liveServer?.reloadClients(); }); this.pipelineProcess.on("error", (error) => { this.logError("Error starting pipeline process.", error); }); this.pipelineProcess.on("exit", (code, signal) => { if (signal) { this.logWarn(`Pipeline process was terminated with signal: ${signal}`); } else { this.logInfo(`Pipeline process exited with code: ${code}`); } }); } stopPipeline() { if (this.pipelineProcess) { this.logInfo("Stopping pipeline process..."); this.pipelineProcess.kill("SIGTERM"); this.pipelineProcess = null; this.logInfo("Pipeline process stopped."); } else { this.logWarn("No pipeline process is currently running."); } } isPipelineRunning() { return this.pipelineProcess !== null && !this.pipelineProcess.killed; } restartPipelineWithDelay(delay = 1000) { this.logInfo(`Delaying pipeline restart by ${delay} milliseconds...`); setTimeout(() => this.restartPipeline(), delay); } } //# sourceMappingURL=PipelineManager.js.map