import { execFileSync } from 'node:child_process';
import { readdirSync, mkdirSync } from 'node:fs';
import { resolve } from 'node:path';

const root = resolve(import.meta.dirname, '..');
const sourceDir = resolve(root, 'public/maps/source');
const outDir = resolve(root, 'public/maps');
const tiled = process.env.TILED ?? '/Applications/Tiled.app/Contents/MacOS/Tiled';
mkdirSync(outDir, { recursive: true });
for (const file of readdirSync(sourceDir).filter((name) => name.endsWith('.tmx'))) {
  execFileSync(tiled, ['--export-map', 'json', resolve(sourceDir, file), resolve(outDir, file.replace('.tmx', '.json'))], { stdio: 'inherit' });
}
console.log('Exported 10 Tiled JSON maps.');
