import * as fs from 'fs/promises'; import path from 'path'; export async function checkIfExists(path: string): Promise { try { await fs.access(path); return true; } catch { return false; } } export async function copyFile(srcFile: string, destFolder: string): Promise { await ensureFolderExists(destFolder); const fileName = path.basename(srcFile); const destFile = path.join(destFolder, fileName); await fs.copyFile(srcFile, destFile); } export async function copyFiles(srcFolder: string, destFolder: string): Promise { await ensureFolderExists(destFolder); const files = await fs.readdir(srcFolder); for (const file of files) { const srcPath = `${srcFolder}/${file}`; const destPath = `${destFolder}/${file}`; await fs.copyFile(srcPath, destPath); } } export async function ensureFolderExists(path: string): Promise { await fs.mkdir(path, { recursive: true }); } export async function listFiles(path: string): Promise { return (await fs.readdir(path)).filter(file => !file.startsWith('.') && file.includes('.')); } export async function listFolders(dirPath: string): Promise { const entries = await fs.readdir(dirPath, { withFileTypes: true }); return entries .filter(entry => entry.isDirectory() && !entry.name.startsWith('.')) .map(entry => entry.name); } export async function loadFile(path: string): Promise { return await fs.readFile(path, 'utf8'); } export async function saveFile(path: string, content: string): Promise { await fs.writeFile(path, content, 'utf8'); } export async function deleteFile(path: string): Promise { await fs.unlink(path); } export async function copyFolderRecursive(srcFolder: string, destFolder: string): Promise { await ensureFolderExists(destFolder); const entries = await fs.readdir(srcFolder, { withFileTypes: true }); for (const entry of entries) { const srcPath = path.join(srcFolder, entry.name); const destPath = path.join(destFolder, entry.name); if (entry.isDirectory()) { await copyFolderRecursive(srcPath, destPath); } else { await fs.copyFile(srcPath, destPath); } } } export async function deleteFolder(dirPath: string): Promise { await fs.rm(dirPath, { recursive: true }); } // --- Multi-folder helpers --- /** * Search folders in order, return the full path to the first existing match * for the given filename. */ export async function findFileInFolders(folders: string[], filename: string): Promise { for (const folder of folders) { const candidate = path.join(folder, filename); if (await checkIfExists(candidate)) { return candidate; } } return undefined; } /** * Merge file listings from multiple folders. First folder takes priority on * name collisions (earlier occurrence wins). */ export async function listFilesFromFolders(folders: string[]): Promise { const seen = new Set(); const result: string[] = []; for (const folder of folders) { if (!await checkIfExists(folder)) continue; const files = await listFiles(folder); for (const f of files) { if (!seen.has(f)) { seen.add(f); result.push(f); } } } return result; } /** * Copy files from multiple source folders into a single destination. * First folder takes priority on duplicate filenames (copy is skipped * if the file already exists in dest from an earlier folder). */ export async function copyFilesFromFolders(folders: string[], destFolder: string): Promise { await ensureFolderExists(destFolder); const copied = new Set(); for (const folder of folders) { if (!await checkIfExists(folder)) continue; const files = await fs.readdir(folder); for (const file of files) { if (copied.has(file)) continue; copied.add(file); const srcPath = path.join(folder, file); const destPath = path.join(destFolder, file); await fs.copyFile(srcPath, destPath); } } }