import fs from 'node:fs';
import path from 'node:path';
import { spawnSync } from 'node:child_process';

const localRoot = process.cwd();
const distDir = path.join(localRoot, 'dist');
const webroot = process.env.CMS_WEBROOT || '/usr/local/www/clawdie-si';

if (!fs.existsSync(distDir)) {
  console.error(`Build output not found: ${distDir}`);
  process.exit(1);
}

const result = spawnSync(
  'rsync',
  ['-av', '--delete', `${distDir}/`, `${webroot}/`],
  { stdio: 'inherit', env: process.env },
);

if (result.status !== 0) {
  process.exit(result.status ?? 1);
}
