46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import path from 'path';
|
|
import fs from 'fs/promises';
|
|
import { b64 } from './html';
|
|
|
|
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 interface Game {
|
|
name: string;
|
|
iconUrl?: string;
|
|
}
|
|
|
|
export async function getGames(): Promise<Game[]> {
|
|
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);
|
|
const gameNames = list.filter(d => d !== 'index').sort();
|
|
|
|
const games: Game[] = [];
|
|
for (const name of gameNames) {
|
|
const gameDir = path.resolve(dir, name);
|
|
const pwaIconFile = Bun.file(path.resolve(gameDir, 'assets', 'pwa_icon.png'));
|
|
const iconFile = Bun.file(path.resolve(gameDir, 'assets', 'favicon.ico'));
|
|
let iconUrl: string | undefined;
|
|
if (await pwaIconFile.exists()) {
|
|
iconUrl = `data:image/png;base64,${await b64(pwaIconFile)}`;
|
|
} else if (await iconFile.exists()) {
|
|
iconUrl = `data:image/x-icon;base64,${await b64(iconFile)}`;
|
|
}
|
|
games.push({ name, iconUrl });
|
|
}
|
|
return games;
|
|
} |