#!/usr/bin/env node /** * scripts/extract-all.mjs * * 一键从 web 源码提取所有资产到 src/data/*.json * * 用法: * node scripts/extract-all.mjs * * JNPF 升级时重跑: * 1. cd $ccWorkDir/dacli * 2. node scripts/extract-all.mjs * 3. 看 git diff data/ 知道 web 改了哪些 * 4. 跑 baseline 验证 dacli 还兼容 */ import { readFileSync, writeFileSync, existsSync } from 'node:fs' import { resolve, dirname } from 'node:path' import { fileURLToPath } from 'node:url' const __dirname = dirname(fileURLToPath(import.meta.url)) const ROOT = resolve(__dirname, '..') // web 源码绝对路径 const WEB_ROOT = process.env.WEB_ROOT || '/Users/xs/Desktop/workspace/web' const COMP_MAP = `${WEB_ROOT}/apps/jnpf-web-apps-main/src/components/FormGenerator/src/helper/componentMap.ts` const FORM_VALIDATE = `${WEB_ROOT}/packages/jnpf/utils/src/helpers/formValidate.ts` const R_DIR = `${WEB_ROOT}/apps/jnpf-web-apps-main/src/components/FormGenerator/src/rightComponents` // 输出 const OUT_DIR = resolve(ROOT, 'src/data') // ==================== 1. componentMap ==================== function extractComponentMap() { const src = readFileSync(COMP_MAP, 'utf-8') const items = [...src.matchAll(/jnpfKey:\s*['"](\w+)['"][\s\S]*?tag:\s*['"]([^'"]+)['"]/g)] const map = {} for (const m of items) map[m[1]] = m[2] writeFileSync(resolve(OUT_DIR, 'component-map.json'), JSON.stringify(map, null, 2)) return items.length } // ==================== 2. fields (Rxxx.vue) ==================== // 注: 需要 @vue/compiler-sfc, 这里用 require 加载 async function extractFields() { const { parse } = await import('@vue/compiler-sfc') const { readdirSync } = await import('node:fs') // 读 componentMap const cmSrc = readFileSync(COMP_MAP, 'utf-8') const jnpfKeyToTag = {} for (const m of cmSrc.matchAll(/jnpfKey:\s*['"](\w+)['"][\s\S]*?tag:\s*['"]([^'"]+)['"]/g)) { jnpfKeyToTag[m[1]] = m[2] } const schemas = {} for (const key of Object.keys(jnpfKeyToTag)) { const pascal = key.charAt(0).toUpperCase() + key.slice(1) const f = resolve(R_DIR, `R${pascal}.vue`) if (!existsSync(f)) continue const src = readFileSync(f, 'utf-8') const { descriptor } = parse(src, { filename: `R${pascal}.vue` }) const tmpl = descriptor.template?.content if (!tmpl) continue const fields = [...new Set( [...tmpl.matchAll(/activeData\.([a-zA-Z_][\w.]*)/g)].map(m => m[1]) )].sort() // 过滤掉方法调用 (e.g. __config__.regList.splice 不是字段) const cleanFields = fields.filter(f => !f.includes('.splice') && !f.includes('.push')) schemas[key] = cleanFields } writeFileSync(resolve(OUT_DIR, 'field-schemas.json'), JSON.stringify(schemas, null, 2)) return Object.keys(schemas).length } // ==================== 3. l1-rules ==================== function extractL1Rules() { const src = readFileSync(FORM_VALIDATE, 'utf-8') const match = src.match(/const regularList\s*=\s*\{([\s\S]*?)\n\}/) if (!match) throw new Error('regularList not found in formValidate.ts') const rules = {} // 提每条 key 的 msg + rule for (const m of match[1].matchAll(/(\w+):\s*\{[^{}]*?msg:\s*['"]([^'"]+)['"][^{}]*?rule:\s*(\/[^\/]+\/[a-z]*)/g)) { rules[m[1]] = { msg: m[2], rule: m[3] } } writeFileSync(resolve(OUT_DIR, 'validation-rules.json'), JSON.stringify(rules, null, 2)) return Object.keys(rules).length } // ==================== 4. l2-rules (Form.vue) ==================== async function extractL2Rules() { const { parse } = await import('@vue/compiler-sfc') const { readdirSync, statSync } = await import('node:fs') const rules = { di: [], dm: [], dict: [], workflow: [], menu: [], role: [], user: [] } function walk(dir) { if (!existsSync(dir)) return for (const f of readdirSync(dir)) { const p = resolve(dir, f) const s = statSync(p) if (s.isDirectory()) walk(p) else if (f.endsWith('.vue') && f === 'Form.vue') { parseForm(p, rules) } } } function parseForm(file, rules) { const module = file.includes('/dataInterface/') ? 'di' : file.includes('/dataModel/') ? 'dm' : file.includes('/dictionary/') ? 'dict' : file.includes('/flowEngine/') ? 'workflow' : file.includes('/menu/') ? 'menu' : file.includes('/role/') ? 'role' : file.includes('/user/') ? 'user' : null if (!module || !rules[module]) return const src = readFileSync(file, 'utf-8') const { descriptor } = parse(src, { filename: file }) const script = descriptor.scriptSetup?.content || descriptor.script?.content || '' for (const m of script.matchAll(/createMessage\.(error|warning)\s*\(\s*['"]([^'"]+)['"]/g)) { rules[module].push({ level: m[1], msg: m[2], line: getLineNumber(script, m.index), source: file.replace(/^.*\/web\//, 'web/'), }) } } function getLineNumber(text, index) { return text.slice(0, index).split('\n').length } walk(`${WEB_ROOT}/apps/jnpf-web-apps-main/src/views`) writeFileSync(resolve(OUT_DIR, 'l2-rules.json'), JSON.stringify(rules, null, 2)) return Object.values(rules).reduce((a, b) => a + b.length, 0) } // ==================== 5. flow nodes ==================== async function extractFlow() { const { parse } = await import('@vue/compiler-sfc') const FLOW_FILE = `${WEB_ROOT}/apps/jnpf-web-apps-main/src/components/FlowProcess/src/index.vue` if (!existsSync(FLOW_FILE)) { console.log(' (FlowProcess/src/index.vue 不存在, 跳过)') return 0 } const src = readFileSync(FLOW_FILE, 'utf-8') const { descriptor } = parse(src, { filename: FLOW_FILE }) const script = descriptor.scriptSetup?.content || descriptor.script?.content || '' const nodes = {} for (const m of script.matchAll(/setErrorList\s*\(\s*['"]([^'"]+)['"]\s*,\s*['"]([^'"]*)['"]\s*,\s*['"]([^'"]*)['"]/g)) { const [_, msg, key, title] = m if (title && !nodes[title]) nodes[title] = { msg, key } } writeFileSync(resolve(OUT_DIR, 'flow-nodes.json'), JSON.stringify(nodes, null, 2)) return Object.keys(nodes).length } // ==================== main ==================== async function main() { console.log('▶ 一键提取 web 源码 → src/data/*.json\n') try { const n1 = extractComponentMap() console.log(` ✓ component-map.json: ${n1} 个 jnpfKey → tag`) } catch (e) { console.log(` ✗ component-map: ${e.message}`) } try { const n2 = await extractFields() console.log(` ✓ field-schemas.json: ${n2} 个控件`) } catch (e) { console.log(` ✗ field-schemas: ${e.message}`) } try { const n3 = extractL1Rules() console.log(` ✓ validation-rules.json: ${n3} 条 L1 正则`) } catch (e) { console.log(` ✗ validation-rules: ${e.message}`) } try { const n4 = await extractL2Rules() console.log(` ✓ l2-rules.json: ${n4} 条 L2 错误消息`) } catch (e) { console.log(` ✗ l2-rules: ${e.message}`) } try { const n5 = await extractFlow() console.log(` ✓ flow-nodes.json: ${n5} 个工作流节点`) } catch (e) { console.log(` ✗ flow-nodes: ${e.message}`) } console.log('\n✓ 全部提取完成') console.log(' 下一步:') console.log(' 1. 看 src/data/ 下的 JSON') console.log(' 2. 跑 baseline: bash examples/baseline/all.sh --skip-visual') console.log(' 3. 全通: npx playwright test') } main().catch(e => { console.error(e); process.exit(1) })