import fs from 'fs' const path = new URL('../src/api/biz.ts', import.meta.url) let s = fs.readFileSync(path, 'utf8') if (!s.includes("from './sjnmtybt/realBiz'")) { s = s.replace( "import {\n mockAccounts,", "import * as real from './sjnmtybt/realBiz'\nimport {\n mockAccounts,", ) } const pairs = [ ['fetchDashboard', 'params'], ['fetchVillageBatches', ''], ['fetchPeople', 'params'], ['savePerson', 'payload'], ['submitVillageBatch', 'batchId'], ['fetchInsuranceBatches', ''], ['fetchInsuranceResults', 'params'], ['triggerInsuranceMatch', 'batchIds'], ['confirmInsuranceManual', 'personId'], ['submitToLeader', 'batchId'], ['exportCompareList', '_batchId'], ['returnBatchToTown', 'batchId, reason'], ['fetchApprovals', 'params'], ['reviewPersonQualify', 'payload'], ['decideApproval', 'payload'], ['fetchStatusBatches', ''], ['fetchStandards', 'town'], ['createStandard', 'payload'], ['updateStandard', 'payload'], ['updateDeathStatus', 'deathId'], ['applyPersonStatusAction', 'payload'], ['fetchSpecials', ''], ['fetchDeaths', ''], ['createDeathRecord', 'payload'], ['createSpecialBiz', 'payload'], ['exportSpecialSummary', 'type'], ['triggerAdjustReissue', 'town'], ['fetchAdjustReissueRoster', 'params'], ['fetchVillageMonthlyBatches', 'town'], ['fetchMonthlyBatches', ''], ['calculateMonthly', ''], ['confirmMonthly', 'id'], ['exportMonthlyFile', 'id'], ['issueVillageMonthlyDisk', 'batchId'], ['confirmVillageMonthlyReturn', 'payload'], ['fetchDisks', 'town'], ['exportDisk', ''], ['confirmReturnDisk', 'payload'], ['uploadReturnDiskFile', 'payload'], ['fetchDiskFailPeople', 'diskId'], ['confirmDiskFailPaid', 'failPersonId'], ['archiveToNextMonth', 'id'], ['fetchAlerts', ''], ['handleAlert', 'payload, result'], ['fetchReportBatches', ''], ['fetchAccounts', ''], ['fetchOpLogs', ''], ] const lines = s.split('\n') const out = [] let i = 0 let patched = 0 while (i < lines.length) { const line = lines[i] const m = line.match(/^export function (\w+)/) if (!m) { out.push(line) i++ continue } const fname = m[1] const pair = pairs.find((p) => p[0] === fname) let j = i let brace = 0 let started = false const block = [] while (j < lines.length) { const L = lines[j] block.push(L) for (const ch of L) { if (ch === '{') { brace++ started = true } if (ch === '}') brace-- } j++ if (started && brace === 0) break } if (!pair) { out.push(...block) i = j continue } let body = block.join('\n') const args = pair[1] const call = args ? `return real.${fname}(${args})` : `return real.${fname}()` // Replace the final real-api return (get/post) after mock branch const next = body.replace(/\n return (?:get|post)[\s\S]*$/m, `\n ${call}`) if (next !== body) patched++ out.push(...next.split('\n')) i = j } fs.writeFileSync(path, out.join('\n')) console.log('patched functions:', patched)