refactor: initialise data

This commit is contained in:
Carlos Valente
2024-07-05 21:19:14 +02:00
committed by Carlos Valente
parent dae32e89a3
commit 84792bc00d
31 changed files with 1031 additions and 545 deletions
+39 -1
View File
@@ -1,6 +1,6 @@
import { existsSync, mkdirSync } from 'fs';
import { readdir } from 'fs/promises';
import { parse } from 'path';
import { basename, extname, join, parse } from 'path';
/**
* @description Creates a directory if it doesn't exist
@@ -47,3 +47,41 @@ export function appendToName(filePath: string, append: string): string {
const extension = filePath.split('.').pop();
return filePath.replace(`.${extension}`, ` ${append}.${extension}`);
}
/**
* Generates a unique file name within the specified directory.
* If a file with the same name already exists, appends a counter to the filename.
*/
export function generateUniqueFileName(directory: string, filename: string): string {
const extension = extname(filename);
const baseName = basename(filename, extension);
let counter = 0;
let uniqueFilename = filename;
while (fileExists(uniqueFilename)) {
counter++;
// Append counter to filename if the file exists.
uniqueFilename = `${baseName} (${counter})${extension}`;
}
return uniqueFilename;
function fileExists(name: string) {
return existsSync(join(directory, name));
}
}
/**
* retrieves the filename from a given path
*/
export function getFileNameFromPath(filePath: string): string {
return basename(filePath);
}
/**
* Utility naivly checks for paths on whether it includes directories
*/
export function isPath(filePath: string): boolean {
return filePath !== basename(filePath);
}