UNPKG

kist

Version:

Package Pipeline Processor

78 lines (77 loc) 3.55 kB
"use strict"; // ============================================================================ // Imports // ============================================================================ 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RunScriptAction = void 0; const child_process_1 = require("child_process"); const path_1 = __importDefault(require("path")); const util_1 = __importDefault(require("util")); const Action_1 = require("../../core/pipeline/Action"); // ============================================================================ // Constants // ============================================================================ const execFileAsync = util_1.default.promisify(child_process_1.execFile); // ============================================================================ // Classes // ============================================================================ /** * RunScriptAction executes an external JavaScript file as part of a KIST pipeline. * The script is executed in a separate process to avoid blocking the main application. */ class RunScriptAction extends Action_1.Action { /** * Executes the external script file. * * @param options - The options specifying the script file to execute. * @returns A Promise that resolves when the script execution completes. * @throws {Error} If the script execution fails. */ execute(options) { return __awaiter(this, void 0, void 0, function* () { const { scriptPath, args = [] } = options; if (!scriptPath) { throw new Error("Invalid options: 'scriptPath' is required."); } const resolvedScriptPath = path_1.default.resolve(scriptPath); this.logInfo(`Executing external script: ${resolvedScriptPath}...`); try { const { stdout, stderr } = yield execFileAsync("node", [ resolvedScriptPath, ...args, ]); if (stderr) { this.logError(`Script execution failed: ${stderr}`); throw new Error(stderr); } this.logInfo(stdout); this.logInfo("Script executed successfully."); } catch (error) { this.logError("Error occurred while executing the script.", error); throw error; } }); } /** * Provides a description of the action. * * @returns A string description of the action. */ describe() { return "Executes an external JavaScript file as part of the KIST pipeline."; } } exports.RunScriptAction = RunScriptAction;