filemanagementsystemfornode
Version:
A file management system for node.js application making managements of files easier, all in one code, you can delete, move, etc, files all in one code, making ability to manage a system eg a application requiring transfering of files in folders easier
54 lines (46 loc) • 1.68 kB
text/typescript
import * as fs from "fs";
import * as path from "path";
import { promisify } from "util";
// Convert callback-based functions to promises
const renameAsync = promisify(fs.rename);
const unlinkAsync = promisify(fs.unlink);
const existsAsync = (filePath: string): Promise<boolean> =>
new Promise((resolve) => fs.access(filePath, fs.constants.F_OK, (err) => resolve(!err)));
export class FileComponent {
private filePath: string;
constructor(filePath: string) {
this.filePath = path.resolve(filePath);
}
async rename(newName: string): Promise<void> {
try {
const newPath = path.join(path.dirname(this.filePath), newName);
await renameAsync(this.filePath, newPath);
this.filePath = newPath;
} catch (error) {
console.error(`Error renaming file: ${error}`);
}
}
async move(destination: string): Promise<void> {
try {
const newPath = path.join(destination, path.basename(this.filePath));
await renameAsync(this.filePath, newPath);
this.filePath = newPath;
} catch (error) {
console.error(`Error moving file: ${error}`);
}
}
async delete(): Promise<void> {
try {
if (await existsAsync(this.filePath)) {
await unlinkAsync(this.filePath);
} else {
console.warn(`File not found: ${this.filePath}`);
}
} catch (error) {
console.error(`Error deleting file: ${error}`);
}
}
getPath(): string {
return this.filePath;
}
}