import chokidar from "chokidar"; import { execSync } from "child_process"; import WebSocket, { WebSocketServer } from "ws"; const [ , , clientName, appName, variantName ] = process.argv; if (!clientName || !appName || !variantName) { console.error("❌ Usage: node watch.js "); process.exit(1); } console.log("👀 Watch mode"); console.log(` Client : ${clientName}`); console.log(` Application : ${appName}`); console.log(` Variant : ${variantName}`); const WATCH_PATHS = [ "2-tokens", "3-styles", `4-clients/${clientName}` ]; let timeout = null; // Port DEV uniquement const WS_PORT = 24601; // Serveur WebSocket const wss = new WebSocketServer({ port: WS_PORT }); console.log(`🔌 LiveDev WebSocket listening on ws://localhost:${WS_PORT}`); function notifyReload() { console.log("📢 Notify browser: reload CSS"); wss.clients.forEach(client => { if (client.readyState === WebSocket.OPEN) { client.send("reload-css"); } }); } function rebuild() { console.log("\n🔄 Change detected"); try { execSync( `node 5-build/build-css.js ${clientName} ${variantName}`, { stdio: "inherit" } ); execSync( `node 5-build/deploy-one.js ${clientName} ${appName} ${variantName}`, { stdio: "inherit" } ); notifyReload(); console.log("✅ Rebuild + deploy done"); } catch (err) { console.error("❌ Error during rebuild/deploy"); console.error(err); } } const watcher = chokidar.watch(WATCH_PATHS, { ignoreInitial: true }); watcher.on("all", () => { clearTimeout(timeout); timeout = setTimeout(rebuild, 100); });