Files
novafarma/scripts/tiled-watcher.js
2026-01-20 01:05:17 +01:00

75 lines
2.2 KiB
JavaScript
Executable File

#!/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);
});