import { createWriteStream, existsSync, mkdirSync, statSync } from "node:fs"; import { pipeline } from "node:stream/promises"; import { Readable } from "node:stream"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { RAW_BASE, SOURCES } from "./sources.mjs"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const RAW_DIR = path.resolve(__dirname, "../raw"); mkdirSync(RAW_DIR, { recursive: true }); async function download(fbx) { const dest = path.join(RAW_DIR, fbx); if (existsSync(dest) && statSync(dest).size > 0) { console.log(`✓ já baixado: ${fbx}`); return; } const url = RAW_BASE + encodeURIComponent(fbx); console.log(`↓ baixando ${fbx}...`); const res = await fetch(url); if (!res.ok || !res.body) { throw new Error(`Falha ao baixar ${fbx}: HTTP ${res.status} (${url})`); } await pipeline(Readable.fromWeb(res.body), createWriteStream(dest)); console.log(`✓ ${fbx} baixado (${(statSync(dest).size / 1024 / 1024).toFixed(1)} MB)`); } for (const { fbx } of SOURCES) { await download(fbx); } console.log("Todos os modelos-fonte baixados.");