kist
Version:
Lightweight Package Pipeline Processor with Plugin Architecture
56 lines • 2.29 kB
JavaScript
import { __awaiter } from "tslib";
import { promises as fs } from "fs";
import path from "path";
import { Action } from "../../core/pipeline/Action.js";
export class DirectoryCopyAction extends Action {
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;
}
});
}
copyFiles(srcDir, destDir) {
return __awaiter(this, void 0, void 0, function* () {
const resolvedSrcDir = path.resolve(srcDir);
const resolvedDestDir = path.resolve(destDir);
try {
yield this.recursiveCopy(resolvedSrcDir, resolvedDestDir);
}
catch (error) {
throw new Error(`Failed to copy from ${resolvedSrcDir} to ${resolvedDestDir}: ${error}`);
}
});
}
recursiveCopy(srcDir, destDir) {
return __awaiter(this, void 0, void 0, function* () {
yield fs.mkdir(destDir, { recursive: true });
const entries = yield 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()) {
yield this.recursiveCopy(srcPath, destPath);
}
else {
yield 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