import { copyFile, mkdir, readdir } from 'node:fs/promises';
import { resolve } from 'node:path';

await mkdir(resolve('public/build'), { recursive: true });
await mkdir(resolve('public/fonts'), { recursive: true });
await copyFile(resolve('src/client/app.js'), resolve('public/build/app.js'));
await copyFile(resolve('src/client/flash.js'), resolve('public/build/flash.js'));
await copyFile(resolve('node_modules/alpinejs/dist/cdn.min.js'), resolve('public/build/alpine.min.js'));
const fontDir = resolve('node_modules/@fontsource-variable/vazirmatn/files');
const fontFiles = await readdir(fontDir);
const preferred = fontFiles.find((name) => /arabic-wght-normal\.woff2$/.test(name)) ?? fontFiles.find((name) => /wght-normal\.woff2$/.test(name));
if (!preferred) throw new Error('فایل فونت Vazirmatn پیدا نشد.');
await copyFile(resolve(fontDir, preferred), resolve('public/fonts/vazirmatn.woff2'));
