fipy
Version:
A simple and easy package to copy files
157 lines (156 loc) • 6.92 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileCopy = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const node_events_1 = require("node:events");
class FileCopy extends node_events_1.EventEmitter {
constructor(settings) {
super();
this.settings = settings;
}
/**
* Copies files from the source folder to the destination folder. Emits a fileCopied event for each file copied.
* @param renameFunction A function that takes a file name as an argument and returns a new file name. If not provided, the file name is not changed.
* @emits fileCopied An event that is emitted for each file copied. The event emits the source file and the destination file.
* @emits error An event that is emitted if an error occurs. The event emits the error message.
*/
copyFiles(renameFunction) {
// Check if source folder exists
if (!this.checkSourceFolder()) {
return;
}
// Check if destination folder exists
if (!this.checkDestinationFolder()) {
return;
}
// Get files to copy
const files = this.getFilesToCopy();
// Copy files
for (const file of files) {
this.copyFile(file, renameFunction);
}
}
/**
* Moves files from the source folder to the destination folder. Emits a fileCopied and a fileDeleted event for each file moved.
* @emits fileCopied An event that is emitted for each file copied. The event emits the source file and the destination file.
* @emits fileDeleted An event that is emitted for each file deleted. The event emits the source file.
* @emits error An event that is emitted if an error occurs. The event emits the error message.
*/
moveFiles(renameFunction) {
// Check if source folder exists
if (!this.checkSourceFolder()) {
return;
}
// Check if destination folder exists
if (!this.checkDestinationFolder()) {
return;
}
// Get files to copy
const files = this.getFilesToCopy();
// Copy files
for (const file of files) {
this.copyFile(file, renameFunction);
// Delete file
fs_1.default.unlinkSync(file);
// Emit event
this.emit('fileDeleted', file);
}
}
checkSourceFolder() {
// Check if source folder exists
if (!fs_1.default.existsSync(this.settings.source)) {
this.emit('error', `Source folder does not exist: ${this.settings.source}`);
return false;
}
return true;
}
checkDestinationFolder() {
// Check if destination folder exists
if (!fs_1.default.existsSync(this.settings.destination)) {
// Create destination folder if createDestinationFolder is true
if (this.settings.createDestinationFolder) {
fs_1.default.mkdirSync(this.settings.destination, { recursive: true });
}
else {
this.emit('error', `Destination folder does not exist: ${this.settings.destination}`);
return false;
}
}
return true;
}
copyFile(file, renameFunction) {
// If createDestinationFolder, create subfolders, else just use the destination folder
const basename = path_1.default.basename(file);
const destinationFolder = this.settings.createDestinationFolder ? path_1.default.join(this.settings.destination, path_1.default.dirname(path_1.default.relative(this.settings.source, file))) : this.settings.destination;
const destinationFile = path_1.default.join(destinationFolder, renameFunction ? renameFunction(basename) : basename);
// Get folder of destination file
const destinationFileFolder = path_1.default.dirname(destinationFile);
// Create destination folder if createDestinationFolder is true and if it doesn't exist
if (this.settings.createDestinationFolder && !fs_1.default.existsSync(destinationFileFolder)) {
fs_1.default.mkdirSync(destinationFileFolder, { recursive: true });
}
// Copy file
fs_1.default.copyFileSync(file, destinationFile, this.settings.overwrite ? undefined : fs_1.default.constants.COPYFILE_EXCL);
// Emit event
this.emit('fileCopied', file, destinationFile);
}
getFilesToCopy() {
// Create array to store files
const files = [];
// If recursive, get files recursively, else get top level files
if (this.settings.recursive) {
this.getFilesRecursive(this.settings.source, files);
}
else {
this.getFiles(this.settings.source, files);
}
return files;
}
getFilesRecursive(folder, files) {
const folderContents = fs_1.default.readdirSync(folder);
folderContents.forEach((item) => {
const itemPath = path_1.default.join(folder, item);
const itemStat = fs_1.default.statSync(itemPath);
if (itemStat.isDirectory()) {
if (this.settings.folderGlob && !item.match(this.settings.folderGlob)) {
return;
}
this.getFilesRecursive(itemPath, files);
}
else {
if (this.settings.fileGlob && !item.match(this.settings.fileGlob)) {
return;
}
files.push(itemPath);
}
});
}
getFiles(folder, files) {
const folderContents = fs_1.default.readdirSync(folder);
folderContents.forEach((item) => {
const itemPath = path_1.default.join(folder, item);
const itemStat = fs_1.default.statSync(itemPath);
if (itemStat.isDirectory()) {
return;
}
if (this.settings.fileGlob && !item.match(this.settings.fileGlob)) {
return;
}
files.push(itemPath);
});
}
/**
* Adds a listener for the fileCopied or error event.
* @param event The event to listen for. Supported events are 'fileCopied', 'fileDeleted', and 'error'.
* @param listener The callback function to call when the event is emitted. The callback function takes two arguments: file and destinationFile.
* @returns This instance of FileCopy.
*/
on(event, listener) {
return super.on(event, listener);
}
}
exports.FileCopy = FileCopy;