import path from 'path';
import fs from 'fs/promises';

export async function isGame(name: string | null | undefined) {
    if (!name || name === 'index') return false;

    const dir = path.resolve(import.meta.dir, '..', 'src', 'games', name);

    if (!await fs.exists(dir)) return false;

    const stat = await fs.stat(dir);

    return stat.isDirectory();
}

export async function getGames() {
    const dir = path.resolve(import.meta.dir, '..', 'src', 'games');
    if (!await fs.exists(dir)) return [];

    const stat = await fs.stat(dir);
    if (!stat.isDirectory()) return [];

    const list = await fs.readdir(dir);
    return list.filter(d => d !== 'index').sort();
}