Files
ontime/apps/server/src/utils/fileManagement.ts
T
2024-05-13 08:08:43 +02:00

41 lines
1.1 KiB
TypeScript

import { existsSync, mkdirSync } from 'fs';
import { readdir } from 'fs/promises';
import { parse } from 'path';
/**
* @description Creates a directory if it doesn't exist
* @param {string} directory - directory that should exist or will be created
*/
export function ensureDirectory(directory: string): void {
if (!existsSync(directory)) {
try {
mkdirSync(directory, { recursive: true });
} catch (err) {
throw new Error(`Could not create directory: ${err}`);
}
}
}
/**
* Ensures that a filename ends with .json extension
*/
export function ensureJsonExtension(filename: string | undefined): string | undefined {
if (!filename) return filename;
return filename.endsWith('.json') ? filename : `${filename}.json`;
}
/**
* Lists all files in a directory
*/
export async function getFilesFromFolder(folderPath: string): Promise<string[]> {
return await readdir(folderPath);
}
/**
* @description Takes a filename and removes the extension
* @param {string} filename - filename with extension
*/
export const removeFileExtension = (filename: string): string => {
return parse(filename).name;
};