#!/usr/bin/env node /** * šŸ”„ TILED AUTO-SYNC WATCHER * Spremlja Faza1_Finalna.tmx in avtomatsko exporta v JSON + reload Electron */ const fs = require('fs'); const path = require('path'); const { exec } = require('child_process'); const WATCH_FILE = path.join(__dirname, 'assets/maps/Faza1_Finalna.tmx'); const JSON_OUTPUT = path.join(__dirname, 'assets/maps/Faza1_Finalna.json'); const TILED_CLI = '/Applications/Tiled.app/Contents/MacOS/tiled'; // Mac path let isProcessing = false; let lastModified = 0; console.log('šŸ”„ TILED AUTO-SYNC WATCHER STARTED'); console.log(`šŸ“‚ Watching: ${WATCH_FILE}`); console.log(`šŸ“¤ Output: ${JSON_OUTPUT}`); console.log(`⚔ Auto-Reload: ENABLED\n`); // Function to export TMX to JSON using Tiled CLI function exportToJSON() { if (isProcessing) return; isProcessing = true; console.log('šŸ”„ Change detected! Exporting TMX → JSON...'); const exportCmd = `"${TILED_CLI}" --export-map json "${WATCH_FILE}" "${JSON_OUTPUT}"`; exec(exportCmd, (error, stdout, stderr) => { if (error) { console.error('āŒ Export failed:', error.message); console.log('šŸ’” Tip: Check if Tiled is installed at:', TILED_CLI); isProcessing = false; return; } if (stderr && !stderr.includes('QStandardPaths')) { console.warn('āš ļø Export warning:', stderr); } console.log('āœ… JSON exported successfully!'); console.log('⚔ Electron will auto-reload on Cmd+R\n'); console.log('šŸ‘€ Waiting for changes...\n'); isProcessing = false; }); } // Watch for file changes fs.watchFile(WATCH_FILE, { interval: 500 }, (curr, prev) => { // Only trigger if file was actually modified (not just accessed) if (curr.mtime.getTime() !== lastModified) { lastModified = curr.mtime.getTime(); // Debounce - wait 100ms to avoid multiple triggers setTimeout(() => { exportToJSON(); }, 100); } }); // Initial message console.log('šŸ‘€ Watching for changes in Tiled map...'); console.log('šŸ’” Save in Tiled (Cmd+S) → Auto-export → Cmd+R in Electron\n'); // Keep process running process.on('SIGINT', () => { console.log('\nšŸ›‘ Watcher stopped.'); process.exit(0); });