#!/usr/bin/env node
/**
 * TFDS 版本更新检测（只读）
 *   node node_modules/@tfdesign/dashboard/scripts/check-tfds-update.mjs [--json]
 */
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import {
  printTfdsUpdateCheckHuman,
  runTfdsUpdateCheck,
} from './lib/check-tfds-update-core.mjs';

const pkgRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const jsonMode = process.argv.includes('--json');
const projectRoot = process.env.TFDS_PROJECT_ROOT || process.env.INIT_CWD || process.cwd();

runTfdsUpdateCheck({
  pkgRoot,
  projectRoot,
  skillName: '产品设计-体服平台数据看板-界面生成',
  setupCommand: 'npx -y -p @tfdesign/dashboard@latest tfds-dashboard-setup',
})
  .then((result) => {
    if (jsonMode) console.log(JSON.stringify(result, null, 2));
    else printTfdsUpdateCheckHuman(result);
    process.exit(result.exitCode ?? (result.ok ? 0 : 2));
  })
  .catch((error) => {
    if (jsonMode) {
      console.log(JSON.stringify({ ok: false, status: 'error', message: error?.message || String(error) }, null, 2));
    } else {
      console.log('\n[tfds-update-check] 更新检测脚本异常退出。');
      console.log(`  错误：${error?.message || error}\n`);
    }
    process.exit(2);
  });
