UNPKG

kist

Version:

Package Pipeline Processor

86 lines (85 loc) 4 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.Step = void 0; const AbstractProcess_1 = require("../abstract/AbstractProcess"); const ActionRegistry_1 = require("./ActionRegistry"); // ============================================================================ // Class // ============================================================================ /** * Represents a single step in a stage, encapsulating its execution logic. * This class manages the resolution and execution of actions associated * with each step. */ class Step extends AbstractProcess_1.AbstractProcess { // Constructor // ======================================================================== /** * Constructs a Step instance based on the provided step definition. * Dynamically resolves the action class from the registry. * * @param step - The step definition containing the step name, action name, * and options. * @throws Error if the specified action is not registered in the action * registry. */ constructor(step) { super(); this.name = step.name; // Resolve the action class from the registry using the action name const actionRegistry = ActionRegistry_1.ActionRegistry.getInstance(); // console.log(step.action) // const ActionClass = actionRegistry.getAction(step.action.name); const ActionClass = actionRegistry.getAction(String(step.action)); if (!ActionClass) { let msg = ` Unknown action "${step.action}" for step "${this.name}". Ensure the action is registered in the registry. `; this.logError(msg); throw new Error(msg); } // Initialize the action with the specific class from the registry this.action = new ActionClass(); this.options = step.options; this.logInfo(`Step "${this.name}" initialized with action "${step.action.constructor.name}".`); } // Methods // ======================================================================== /** * Executes the step by invoking its action's execute method. */ execute() { return __awaiter(this, void 0, void 0, function* () { this.logInfo(`Executing step: ${this.name}`); try { // Validate options if the action provides a validation method if (typeof this.action.validateOptions === "function") { const isValid = this.action.validateOptions(this.options || {}); if (!isValid) { throw new Error(`Invalid options for step: ${this.name}`); } } // Execute the action with the provided options yield this.action.execute(this.options || {}); this.logInfo(`Step "${this.name}" completed successfully.`); } catch (error) { this.logError(`Error executing step "${this.name}": ${error}`, error); } }); } } exports.Step = Step;