kist
Version:
Package Pipeline Processor
85 lines (84 loc) • 3.76 kB
JavaScript
;
// ============================================================================
// 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.FileRenameAction = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const Action_1 = require("../../core/pipeline/Action");
// ============================================================================
// Classes
// ============================================================================
/**
* FileRenameAction handles renaming files within the file system.
* Ensures that file renaming operations are handled efficiently and safely.
*/
class FileRenameAction extends Action_1.Action {
/**
* Executes the file renaming action.
*
* @param options - The options specifying the source and target file paths.
* @returns A Promise that resolves when the file has been successfully renamed.
* @throws {Error} If the source file does not exist or cannot be renamed.
*/
execute(options) {
return __awaiter(this, void 0, void 0, function* () {
const { srcPath, targetPath } = options;
if (!srcPath || !targetPath) {
throw new Error("Invalid options: 'srcPath' and 'targetPath' are required.");
}
this.logInfo(`Renaming file: ${srcPath} → ${targetPath}`);
try {
yield this.renameFile(srcPath, targetPath);
this.logInfo(`File successfully renamed to ${targetPath}`);
}
catch (error) {
this.logError("Failed to rename file.", error);
throw error;
}
});
}
/**
* Renames a file from its current path to a new path.
*
* @param srcPath - The current file path.
* @param targetPath - The new file path.
* @returns A Promise that resolves once the file is successfully renamed.
*/
renameFile(srcPath, targetPath) {
return __awaiter(this, void 0, void 0, function* () {
try {
const resolvedSrcPath = path_1.default.resolve(srcPath);
const resolvedTargetPath = path_1.default.resolve(targetPath);
yield fs_1.promises.rename(resolvedSrcPath, resolvedTargetPath);
this.logDebug(`File renamed: ${resolvedSrcPath} → ${resolvedTargetPath}`);
}
catch (error) {
this.logError(`Error renaming file: ${srcPath} → ${targetPath}`, error);
throw error;
}
});
}
/**
* Provides a description of the action.
*
* @returns A string description of the action.
*/
describe() {
return "Renames a file from a specified source path to a target path.";
}
}
exports.FileRenameAction = FileRenameAction;