#!/usr/bin/env -S npx tsx /** * scripts/seed-tds-baseline.ts * * 把 workSystem 5-7 张代表性 form export 到 /tmp/baseline/, 再 import 到 tds. * 输出 /tmp/baseline-mapping.json (origId → cloneId) 供 baseline spec 用. * * 用户指引: 测试在 tds 做, 数据从官方示例复制 — 官方示例只读不动. * * 跑法: * npx tsx scripts/seed-tds-baseline.ts * * 选择标准 (5 张代表性, 5 维度覆盖): * - yuangongpx (type=1, 无 flow) 单表 * - ruzhishenq (type=1, 无 flow) 单表 (HR) * - renewal (type=1, 无 flow) 单表 (合同) * - leaveApply (type=2, flow=1) 工作流表单 (经典) * - crmOrder (type=2, flow=1) 工作流表单 (订单) */ import { execSync } from 'node:child_process' import { mkdirSync, writeFileSync, existsSync } from 'node:fs' const SELECTED = [ { enCode: 'yuangongpx', fullName: '员工培训' }, { enCode: 'ruzhishenq', fullName: '入职申请' }, { enCode: 'renewal', fullName: '劳动合同续签' }, { enCode: 'leaveApply', fullName: '请假申请' }, { enCode: 'crmOrder', fullName: '订单示例' }, ] as const const EXPORT_DIR = '/tmp/baseline-orig' const MAPPING_FILE = '/tmp/baseline-mapping.json' const CLONE_SUFFIX = '_ws' // workSystem 来源标记 interface Form { id: string enCode: string fullName: string type: number enableFlow: number | null } interface Mapping { origAppCode: string origId: string origEnCode: string origFullName: string cloneAppCode: string cloneId: string cloneEnCode: string cloneFullName: string imported: string } function dacli(args: string, opts: { silent?: boolean } = {}): string { if (!opts.silent) console.log(` $ dacli ${args}`) return execSync(`npx tsx src/cli.ts ${args}`, { encoding: 'utf-8' }) } async function main() { mkdirSync(EXPORT_DIR, { recursive: true }) // 1. 切到 workSystem, 拿 5 张 form 的 id console.log('▶ 切到 workSystem 拿 id...') dacli(`switch-app --appCode=workSystem`, { silent: true }) const listRaw = dacli(`form list --limit=200 --json`, { silent: true }) const all = (JSON.parse(listRaw) as { list: Form[] }).list const targets = SELECTED.map((s) => { const f = all.find((x) => x.enCode === s.enCode) if (!f) throw new Error(`workSystem 找不到 ${s.enCode}, list 总数=${all.length}`) return f }) console.log(` ✓ 锁定 ${targets.length} 张 workSystem form`) // 2. export 每张 console.log('▶ export workSystem 5 张 → /tmp/baseline-orig/') for (const f of targets) { const out = `${EXPORT_DIR}/${f.enCode}.json` dacli(`form export ${f.id} --out=${out}`, { silent: true }) console.log(` ✓ ${f.enCode} (${f.fullName})`) } // 3. 切到 tds, 清掉旧 _ws 克隆 console.log('▶ 切到 tds, 清理旧 _ws 克隆') dacli(`switch-app --appCode=tds`, { silent: true }) const tdsRaw = dacli(`form list --limit=200 --json`, { silent: true }) const tdsAll = (JSON.parse(tdsRaw) as { list: Form[] }).list for (const f of tdsAll.filter((x) => x.enCode.endsWith(CLONE_SUFFIX))) { try { dacli(`form delete ${f.id} --yes`, { silent: true }) console.log(` ✗ 删旧 ${f.enCode}`) } catch (e) { console.log(` ! 删 ${f.enCode} 失败 (忽略): ${(e as Error).message.slice(0, 80)}`) } } // 4. import 每张 → tds console.log('▶ import 5 张 → tds') const mapping: Mapping[] = [] for (const f of targets) { const file = `${EXPORT_DIR}/${f.enCode}.json` const cloneEnCode = `${f.enCode}${CLONE_SUFFIX}` const cloneFullName = `${f.fullName}(ws克隆)` try { const out = dacli( `form import ${file} --enCode=${cloneEnCode} --fullName="${cloneFullName}"${f.enableFlow === 1 ? '' : ' --release'}`, { silent: true } ) // import 输出 "✓ 导入成功 id=XXX enCode=YYY" const m = out.match(/id=(\S+)\s+enCode=/) const cloneId = m?.[1] && m[1] !== '(unknown)' ? m[1] : '' if (!cloneId) { console.log(` ! ${f.enCode} import 返回未拿到 id: ${out.trim().slice(0, 150)}`) continue } mapping.push({ origAppCode: 'workSystem', origId: f.id, origEnCode: f.enCode, origFullName: f.fullName, cloneAppCode: 'tds', cloneId, cloneEnCode, cloneFullName, imported: new Date().toISOString(), }) console.log(` ✓ ${f.enCode} → ${cloneEnCode} (id=${cloneId})`) } catch (e) { console.log(` ✗ ${f.enCode} import 失败: ${(e as Error).message.slice(0, 200)}`) } } // 5. 写 mapping writeFileSync(MAPPING_FILE, JSON.stringify(mapping, null, 2)) console.log(`\n✓ ${mapping.length}/${targets.length} 张克隆成功`) console.log(`✓ mapping 写到 ${MAPPING_FILE}`) console.log('\n后续: npx playwright test tests/e2e/baseline/workSystem/ --project=chromium') } void main().catch((e) => { console.error('FAIL:', e.message) process.exit(1) })