UNPKG

kist

Version:

Package Pipeline Processor

119 lines (118 loc) 5.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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DirectoryCopyAction = void 0; const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); const Action_1 = require("../../core/pipeline/Action"); // ============================================================================ // Classes // ============================================================================ /** * DirectoryCopyAction is a step action responsible for copying all files and * subdirectories from one directory to another, using asynchronous operations * for efficient handling. */ class DirectoryCopyAction extends Action_1.Action { // Methods // ======================================================================== /** * Executes the directory copy action. * @param options - The options specific to directory copying, including * the source and destination paths. * @returns A Promise that resolves when the directory contents have been * successfully copied, or rejects with an error if the action fails. */ execute(options) { return __awaiter(this, void 0, void 0, function* () { const srcDir = options.srcDir; const destDir = options.destDir; if (!srcDir || !destDir) { throw new Error("Missing required options: srcDir or destDir."); } this.logInfo(`Copying files from ${srcDir} to ${destDir}`); try { yield this.copyFiles(srcDir, destDir); this.logInfo(`Files copied successfully from ${srcDir} to ${destDir}`); } catch (error) { this.logError(`Error copying files from ${srcDir} to ${destDir}: ${error}`); throw error; } }); } /** * Asynchronously copies all files and subdirectories from the source * directory to the destination directory. If the destination directory * does not exist, it will be created. * * @param srcDir - The path of the source directory. * @param destDir - The path of the destination directory. * @throws {Error} If any file or directory could not be copied. */ copyFiles(srcDir, destDir) { return __awaiter(this, void 0, void 0, function* () { const resolvedSrcDir = path_1.default.resolve(srcDir); const resolvedDestDir = path_1.default.resolve(destDir); try { yield this.recursiveCopy(resolvedSrcDir, resolvedDestDir); } catch (error) { throw new Error(`Failed to copy from ${resolvedSrcDir} to ${resolvedDestDir}: ${error}`); } }); } /** * Recursively copies files and directories from the source to the * destination directory. * This method creates the destination directory if it does not exist and * recursively copies all nested files and directories. * * @param srcDir - Source directory. * @param destDir - Destination directory. */ recursiveCopy(srcDir, destDir) { return __awaiter(this, void 0, void 0, function* () { yield fs_1.promises.mkdir(destDir, { recursive: true }); const entries = yield fs_1.promises.readdir(srcDir, { withFileTypes: true }); for (const entry of entries) { const srcPath = path_1.default.join(srcDir, entry.name); const destPath = path_1.default.join(destDir, entry.name); if (entry.isDirectory()) { // Recursively copy subdirectory yield this.recursiveCopy(srcPath, destPath); } else { // Copy file yield fs_1.promises.copyFile(srcPath, destPath); } } }); } /** * Provides a description of the action. * @returns A string description of the action. */ describe() { return "Copies all files and subdirectories from one directory to another, including handling of nested directories."; } } exports.DirectoryCopyAction = DirectoryCopyAction; // ============================================================================ // Export // ============================================================================ // export default DirectoryCopyAction;