UNPKG

kist

Version:

Package Pipeline Processor

124 lines (123 loc) 5.77 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.DirectoryCleanAction = void 0; const fs_1 = __importDefault(require("fs")); const micromatch_1 = __importDefault(require("micromatch")); // For glob pattern matching const path_1 = __importDefault(require("path")); const Action_1 = require("../../core/pipeline/Action"); // ============================================================================ // Classes // ============================================================================ /** * DirectoryCleanAction is a step action responsible for cleaning a directory * by deleting all its contents while optionally retaining files and * directories that match specified glob patterns. */ class DirectoryCleanAction extends Action_1.Action { // Methods // ======================================================================== /** * Executes the directory cleaning action. * * @param options - The options specific to directory cleaning, including * the directory path and glob patterns to retain. * @returns A Promise that resolves when the directory has been * successfully cleaned, or silently resolves if the directory does not * exist. */ execute(options) { return __awaiter(this, void 0, void 0, function* () { const dirPath = options.dirPath; const keepPatterns = options.keep; if (!dirPath) { throw new Error("Missing required option: dirPath."); } if (!fs_1.default.existsSync(dirPath)) { this.logWarn(`Directory does not exist, skipping: ${dirPath}`); return; // Exit gracefully if directory does not exist } this.logInfo(`Cleaning directory: ${dirPath}`); try { yield this.cleanDirectoryContents(dirPath, keepPatterns); this.logInfo(`Directory cleaned successfully: ${dirPath}`); } catch (error) { this.logError(`Error cleaning directory "${dirPath}":`, error); } }); } /** * Deletes all contents of a specified directory, excluding files and * directories that match specified glob patterns. * * @param dirPath - The path to the directory to be cleaned. * @param keepPatterns - An optional array of glob patterns for files * and directories to retain. * @returns A Promise that resolves when the directory has been * successfully cleaned. * @throws {Error} Throws an error if deleting any file or directory fails. */ cleanDirectoryContents(dirPath, keepPatterns) { return __awaiter(this, void 0, void 0, function* () { const files = yield fs_1.default.promises.readdir(dirPath); for (const file of files) { const curPath = path_1.default.join(dirPath, file); const relativePath = path_1.default.relative(dirPath, curPath); // Skip files/directories matching keep patterns if (keepPatterns && micromatch_1.default.isMatch(relativePath, keepPatterns)) { this.logInfo(`Skipping: ${relativePath}`); continue; } try { const stat = yield fs_1.default.promises.lstat(curPath); if (stat.isDirectory()) { // Recursively clean subdirectory yield fs_1.default.promises.rmdir(curPath, { recursive: true }); this.logInfo(`Deleted directory: ${relativePath}`); } else { // Delete file yield fs_1.default.promises.unlink(curPath); this.logInfo(`Deleted file: ${relativePath}`); } } catch (error) { this.logError(`Error deleting: ${relativePath}`, error); } } }); } /** * Provides a description of the action. * @returns A string description of the action. */ describe() { let description = ` Cleans a directory by deleting all its contents while retaining files and directories matching specified glob patterns. If the directory does not exist, the action will skip gracefully. `; return description; } } exports.DirectoryCleanAction = DirectoryCleanAction; // ============================================================================ // Export // ============================================================================ // export default DirectoryCleanAction;