UNPKG

kist

Version:

Package Pipeline Processor

128 lines (127 loc) 5.61 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileCopyAction = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const Action_1 = require("../../core/pipeline/Action"); // ============================================================================ // Classes // ============================================================================ /** * FileCopyAction is a step action responsible for copying files from a source * location to a destination directory. This action handles file path * resolution and ensures that existing files in the destination can be * replaced if necessary. */ class FileCopyAction extends Action_1.Action { // Parameters // ======================================================================== // Constructor // ======================================================================== // Methods // ======================================================================== /** * Executes the file copy action. * @param options - The options specific to file copying, including source * file and destination directory. * @returns A Promise that resolves when the file has been successfully * copied, or rejects with an error if the action fails. */ execute(options) { return __awaiter(this, void 0, void 0, function* () { const srcFile = options.srcFile; const destDir = options.destDir; if (!srcFile || !destDir) { throw new Error("Missing required options: srcFile or destDir."); } this.logInfo(`Copying file from ${srcFile} to ${destDir}.`); try { yield this.copyFileToDirectory(srcFile, destDir); this.logInfo(`File copied successfully from ${srcFile} to ${destDir}.`); } catch (error) { this.logError(`Error copying file from ${srcFile} to ${destDir}: ${error}`); throw error; } }); } /** * Copies a file from a specified source to a destination directory. * Handles file path resolution and ensures the destination directory * exists. * * @param srcFile - The path of the source file to be copied. * @param destDir - The destination directory where the file should * be placed. * @returns A Promise that resolves when the file has been successfully * copied. * @throws {Error} If the file cannot be copied, including due to * permission errors or the source file not existing. */ copyFileToDirectory(srcFile, destDir) { return __awaiter(this, void 0, void 0, function* () { try { // Ensure the destination directory exists yield this.ensureDirectoryExists(destDir); // Resolve the destination file path const fileName = path_1.default.basename(srcFile); const destFilePath = path_1.default.join(destDir, fileName); // Copy the file yield fs_1.default.promises.copyFile(srcFile, destFilePath); } catch (error) { this.logError(`Error copying file: ${error}`); throw error; } }); } /** * Ensures that the given directory exists, creating it if it does not. * @param dirPath - The path of the directory to check and create. */ ensureDirectoryExists(dirPath) { return __awaiter(this, void 0, void 0, function* () { try { yield fs_1.default.promises.mkdir(dirPath, { recursive: true }); } catch (error) { if (error instanceof Error) { const nodeError = error; if (nodeError.code !== "EEXIST") { throw nodeError; } } else { throw error; } } }); } /** * Provides a description of the action. * @returns A string description of the action. */ describe() { return "Copies a file from a source location to a destination directory, ensuring directories exist and handling errors gracefully."; } } exports.FileCopyAction = FileCopyAction; // ============================================================================ // Export // ============================================================================ // export default FileCopyAction;