#!/usr/bin/env node // ----------------------------------------------------------------------------- // Lou-Ka — kit Figma · build.mjs // Assemble plugin/code.js = `var REFS = [...]` (captures + photos réduites, // base64) + plugin/src/code.js. À relancer après shots.mjs ou toute édition // de plugin/src/code.js. // ----------------------------------------------------------------------------- import { readFileSync, writeFileSync, readdirSync, mkdirSync, existsSync, statSync } from "node:fs"; import { execSync } from "node:child_process"; import path from "node:path"; const ROOT = new URL(".", import.meta.url).pathname; const REF = path.join(ROOT, "references"); const TMP = path.join(ROOT, "plugin", ".embed"); mkdirSync(TMP, { recursive: true }); function dims(file) { const out = execSync(`sips -g pixelWidth -g pixelHeight "${file}"`).toString(); return { w: +out.match(/pixelWidth: (\d+)/)[1], h: +out.match(/pixelHeight: (\d+)/)[1] }; } /** Réduit une image (largeur max + qualité JPEG) dans .embed/ et renvoie l'entrée REFS. */ function embed(file, kind, maxW, quality) { const out = path.join(TMP, `${kind}-${path.basename(file)}`); execSync(`sips -s format jpeg -s formatOptions ${quality} -Z ${maxW} "${file}" --out "${out}" >/dev/null 2>&1`); const { w, h } = dims(out); return { name: path.basename(file, ".jpg"), kind, w, h, b64: readFileSync(out).toString("base64") }; } const refs = []; const fold = path.join(REF, "fold"); if (existsSync(fold)) for (const f of readdirSync(fold).filter((f) => f.endsWith(".jpg")).sort()) { refs.push(embed(path.join(fold, f), "shot", f.startsWith("desktop") ? 1440 : 780, 72)); } const photos = path.join(REF, "photos"); if (existsSync(photos)) for (const f of readdirSync(photos).filter((f) => /^photo-\d+\.jpg$/.test(f)).sort()) { refs.push(embed(path.join(photos, f), "photo", 560, 74)); } const src = readFileSync(path.join(ROOT, "plugin/src/code.js"), "utf8"); const header = `// GÉNÉRÉ par build.mjs le ${new Date().toISOString()} — ne pas éditer : modifier plugin/src/code.js\n`; const out = `${header}var REFS = ${JSON.stringify(refs)};\n${src}`; writeFileSync(path.join(ROOT, "plugin/code.js"), out); const kb = (n) => `${(n / 1024).toFixed(0)} Ko`; console.log(`plugin/code.js écrit : ${kb(statSync(path.join(ROOT, "plugin/code.js")).size)} — ${refs.filter((r) => r.kind === "shot").length} captures, ${refs.filter((r) => r.kind === "photo").length} photos embarquées`); for (const r of refs) console.log(` · ${r.kind.padEnd(5)} ${r.name.padEnd(22)} ${r.w}×${r.h} ${kb(r.b64.length * 0.75)}`);