#!/usr/bin/env node /** * Patch reading-survey HTML on disk for WeChat WebView radio/submit compatibility. * * Usage: * node scripts/repair-reading-survey-wechat.mjs * node scripts/repair-reading-survey-wechat.mjs --user a70ff537-8908-486e-9b6c-042e07cc25db */ import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { applyWechatSurveyCompat } from '../mindspace-page-data-wechat-survey-compat.mjs'; import { PUBLISH_ROOT_DIR } from '../user-publish.mjs'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const root = path.resolve(__dirname, '..'); function parseArgs(argv) { const userIdx = argv.indexOf('--user'); return { userId: userIdx >= 0 ? String(argv[userIdx + 1] ?? '').trim() : 'a70ff537-8908-486e-9b6c-042e07cc25db', }; } function patchFile(absolutePath) { const before = fs.readFileSync(absolutePath, 'utf8'); const { html, patched } = applyWechatSurveyCompat(before); if (!patched) { console.log(`SKIP ${absolutePath} (no compat patch needed)`); return false; } fs.writeFileSync(absolutePath, html, 'utf8'); console.log(`OK ${absolutePath}`); return true; } const { userId } = parseArgs(process.argv.slice(2)); const base = path.join(root, PUBLISH_ROOT_DIR, userId, 'public'); const targets = ['reading-survey.html']; let changed = 0; for (const name of targets) { const filePath = path.join(base, name); if (!fs.existsSync(filePath)) { console.log(`MISS ${filePath}`); continue; } if (patchFile(filePath)) changed += 1; } console.log(`patched ${changed} file(s)`); process.exit(changed > 0 ? 0 : 1);