kist
Version:
Lightweight Package Pipeline Processor with Plugin Architecture
49 lines • 1.91 kB
JavaScript
import { promises as fs } from "fs";
import path from "path";
import { Action } from "../../core/pipeline/Action.js";
export class DirectoryCopyAction extends Action {
async execute(options) {
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 {
await 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;
}
}
async copyFiles(srcDir, destDir) {
const resolvedSrcDir = path.resolve(srcDir);
const resolvedDestDir = path.resolve(destDir);
try {
await this.recursiveCopy(resolvedSrcDir, resolvedDestDir);
}
catch (error) {
throw new Error(`Failed to copy from ${resolvedSrcDir} to ${resolvedDestDir}: ${error}`);
}
}
async recursiveCopy(srcDir, destDir) {
await fs.mkdir(destDir, { recursive: true });
const entries = await fs.readdir(srcDir, { withFileTypes: true });
for (const entry of entries) {
const srcPath = path.join(srcDir, entry.name);
const destPath = path.join(destDir, entry.name);
if (entry.isDirectory()) {
await this.recursiveCopy(srcPath, destPath);
}
else {
await fs.copyFile(srcPath, destPath);
}
}
}
describe() {
return "Copies all files and subdirectories from one directory to another, including handling of nested directories.";
}
}
//# sourceMappingURL=DirectoryCopyAction.js.map