// 回滚:把线上目录换回最近一次发布前的备份 // node scripts/rollback.mjs 回滚到最近的备份 // node scripts/rollback.mjs --list 列出可用备份 import { spawnSync } from 'node:child_process'; import { loadConfig } from './build.mjs'; const { host, siteDir, publicUrl } = loadConfig().deploy; const live = `${siteDir}/public/app`; const backups = `${siteDir}/.deploy/backups`; const list = process.argv.includes('--list'); const script = list ? `ls -1d ${backups}/app-* 2>/dev/null | sort -r || echo "(没有备份)"` : ` latest=$(ls -1d ${backups}/app-* 2>/dev/null | sort -r | head -1) [ -n "$latest" ] || { echo "没有可回滚的备份" >&2; exit 1; } mv ${live} ${siteDir}/.deploy/rolled-back-$(date +%Y%m%d-%H%M%S) mv "$latest" ${live} echo "已回滚到 $latest" `; const r = spawnSync('ssh', ['-o', 'BatchMode=yes', host, 'bash -s'], { input: `set -euo pipefail\n${script}`, stdio: ['pipe', 'inherit', 'inherit'], }); if (r.status !== 0) process.exit(r.status ?? 1); if (!list) console.log(`请打开 ${publicUrl} 确认。`);