import { mkdir, readdir, readFile, rm, stat, writeFile, } from "node:fs/promises"; import { dirname, posix, resolve } from "node:path"; import { fileURLToPath } from "node:url"; const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url)); const APP_DIR = resolve(SCRIPT_DIR, ".."); const SRC_ROOT = resolve(APP_DIR, "../hardware/unoq"); const DST_ROOT = resolve(APP_DIR, "public/hardware/unoq"); const SKIP_DIRS = new Set([ "__pycache__", ".pytest_cache", "tests", "policies", ]); const SKIP_FILES = new Set([".last_deploy.json", ".gitkeep", "files.json"]); async function collectFiles(dir, relPrefix = "", out = []) { const entries = await readdir(dir, { withFileTypes: true }); entries.sort((a, b) => a.name.localeCompare(b.name)); for (const entry of entries) { const rel = relPrefix ? posix.join(relPrefix, entry.name) : entry.name; const full = resolve(dir, entry.name); if (entry.isDirectory()) { if (SKIP_DIRS.has(entry.name)) continue; await collectFiles(full, rel, out); } else if (entry.isFile()) { if (SKIP_FILES.has(entry.name) || entry.name.endsWith(".pyc")) continue; const st = await stat(full); const executable = (st.mode & 0o111) !== 0 || rel.endsWith(".sh"); out.push({ path: rel, full, executable }); } } return out; } export async function syncUnoqBundle() { const files = await collectFiles(SRC_ROOT); await rm(DST_ROOT, { recursive: true, force: true }); await mkdir(DST_ROOT, { recursive: true }); const manifest = []; for (const item of files) { const dest = resolve(DST_ROOT, item.path); await mkdir(dirname(dest), { recursive: true }); await writeFile(dest, await readFile(item.full)); manifest.push({ path: item.path, executable: item.executable }); } await writeFile( resolve(DST_ROOT, "files.json"), JSON.stringify({ schema: 1, files: manifest }, null, 2) + "\n", "utf8", ); return manifest; } const isDirect = process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url); if (isDirect) { const synced = await syncUnoqBundle(); console.log( `Synced ${synced.length} UNO Q runtime files to app/public/hardware/unoq/`, ); }