Files
dacli/scripts/extract-all.mjs
T

350 lines
14 KiB
JavaScript

#!/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
}
// ==================== 6. report-schemas (报表设计器) ====================
async function extractReportSchemas() {
const { parse } = await import('@vue/compiler-sfc')
const REPORT_DIRS = [
`${WEB_ROOT}/apps/jnpf-web-apps-main/src/views/onlineDev/report`,
`${WEB_ROOT}/apps/jnpf-web-apps-main/src/views/onlineDev/dataReport`,
`${WEB_ROOT}/apps/jnpf-web-apps-main/src/views/common/dynamicReport`,
]
const schemas = { version: 'v0.8-scaffold', components: [], categories: [], apiRef: '/api/extend/ReportManage' }
for (const dir of REPORT_DIRS) {
if (!existsSync(dir)) continue
const FormPath = resolve(dir, 'Form.vue')
if (!existsSync(FormPath)) continue
const src = readFileSync(FormPath, 'utf-8')
const { descriptor } = parse(src, { filename: FormPath })
const tmpl = descriptor.template?.content || ''
const script = descriptor.scriptSetup?.content || descriptor.script?.content || ''
const fields = [...new Set(
[...tmpl.matchAll(/activeData\.([a-zA-Z_][\w.]*)/g)].map(m => m[1])
)].sort().filter(f => !f.includes('.splice') && !f.includes('.push'))
const calls = [...script.matchAll(/api\.(get|post|put|del)\s*\(\s*['"]([^'"]+)['"]/g)].map(m => m[2])
schemas.components.push({ dir: dir.split('/views/').pop(), fields, apiCalls: [...new Set(calls)] })
}
// 报表分类 (1=简易 2=专业)
schemas.categories = [{ id: 1, name: '简易报表' }, { id: 2, name: '专业报表' }]
writeFileSync(resolve(OUT_DIR, 'report-schemas.json'), JSON.stringify(schemas, null, 2))
return schemas.components.length
}
// ==================== 7. portal-schemas (门户设计器) ====================
async function extractPortalSchemas() {
const { parse } = await import('@vue/compiler-sfc')
const PORTAL_DIRS = [
`${WEB_ROOT}/apps/jnpf-web-apps-main/src/views/onlineDev/visualPortal`,
`${WEB_ROOT}/apps/jnpf-web-apps-main/src/views/common/dynamicPortal`,
]
const schemas = { version: 'v0.8-scaffold', components: [], categories: [], apiRef: '/api/visualdev/Portal' }
for (const dir of PORTAL_DIRS) {
if (!existsSync(dir)) continue
const FormPath = resolve(dir, 'Form.vue')
if (!existsSync(FormPath)) continue
const src = readFileSync(FormPath, 'utf-8')
const { descriptor } = parse(src, { filename: FormPath })
const tmpl = descriptor.template?.content || ''
const script = descriptor.scriptSetup?.content || descriptor.script?.content || ''
const fields = [...new Set(
[...tmpl.matchAll(/activeData\.([a-zA-Z_][\w.]*)/g)].map(m => m[1])
)].sort().filter(f => !f.includes('.splice') && !f.includes('.push'))
const calls = [...script.matchAll(/api\.(get|post|put|del)\s*\(\s*['"]([^'"]+)['"]/g)].map(m => m[2])
schemas.components.push({ dir: dir.split('/views/').pop(), fields, apiCalls: [...new Set(calls)] })
}
// 门户分类 (1=经典 2=全屏)
schemas.categories = [{ id: 1, name: '经典门户' }, { id: 2, name: '全屏门户' }]
writeFileSync(resolve(OUT_DIR, 'portal-schemas.json'), JSON.stringify(schemas, null, 2))
return schemas.components.length
}
// ==================== 8. bigdata-schemas (大屏设计器) ====================
async function extractBigdataSchemas() {
const { parse } = await import('@vue/compiler-sfc')
const BIG_DIR = `${WEB_ROOT}/apps/jnpf-web-apps-main/src/views/extend/bigData`
const schemas = { version: 'v0.8-scaffold', components: [], categories: [], apiRef: '/api/extend/BigData' }
if (existsSync(BIG_DIR)) {
const FormPath = resolve(BIG_DIR, 'index.vue')
if (existsSync(FormPath)) {
const src = readFileSync(FormPath, 'utf-8')
const { descriptor } = parse(src, { filename: FormPath })
const tmpl = descriptor.template?.content || ''
const script = descriptor.scriptSetup?.content || descriptor.script?.content || ''
const fields = [...new Set(
[...tmpl.matchAll(/activeData\.([a-zA-Z_][\w.]*)/g)].map(m => m[1])
)].sort().filter(f => !f.includes('.splice') && !f.includes('.push'))
const calls = [...script.matchAll(/api\.(get|post|put|del)\s*\(\s*['"]([^'"]+)['"]/g)].map(m => m[2])
schemas.components.push({ dir: 'bigData', fields, apiCalls: [...new Set(calls)] })
}
}
// 大屏分类 (1=数据大屏 2=仪表盘)
schemas.categories = [{ id: 1, name: '数据大屏' }, { id: 2, name: '仪表盘' }]
writeFileSync(resolve(OUT_DIR, 'bigdata-schemas.json'), JSON.stringify(schemas, null, 2))
return schemas.components.length
}
// ==================== 9. datasource-drivers (静态) ====================
// 数据源 driver 列表, 来自 JNPF `application.yml` 默认配置 + jnpf-base 数据源
// (这是 v0.8-scaffold: 静态清单, 后续接 web DbLinkForm.vue 提取动态 driver 列表)
function extractDatasourceDrivers() {
const drivers = [
{ id: 'MySQL', name: 'MySQL', defaultPort: 3306, jdbc: 'jdbc:mysql://' },
{ id: 'SQLServer', name: 'SQL Server', defaultPort: 1433, jdbc: 'jdbc:sqlserver://' },
{ id: 'Oracle', name: 'Oracle', defaultPort: 1521, jdbc: 'jdbc:oracle://' },
{ id: 'PostgreSQL', name: 'PostgreSQL', defaultPort: 5432, jdbc: 'jdbc:postgresql://' },
{ id: 'DM', name: '达梦 DM', defaultPort: 5236, jdbc: 'jdbc:dm://' },
{ id: 'Kingbase', name: '人大金仓 Kingbase', defaultPort: 54321, jdbc: 'jdbc:kingbase8://' },
{ id: 'HighGo', name: '高斯 DB HighGo', defaultPort: 5866, jdbc: 'jdbc:highgo://' },
{ id: 'SQLite', name: 'SQLite', defaultPort: 0, jdbc: 'jdbc:sqlite:' },
]
const out = {
version: 'v0.8-scaffold',
note: '静态 driver 列表, 后续接 web DbLinkForm.vue 提取动态',
apiRef: '/api/system/DataSource',
drivers,
}
writeFileSync(resolve(OUT_DIR, 'datasource-drivers.json'), JSON.stringify(out, null, 2))
return drivers.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}`)
}
// v0.8 W7-D4 新增 4 extractor
try {
const n6 = await extractReportSchemas()
console.log(` ✓ report-schemas.json: ${n6} 个报表组件`)
} catch (e) {
console.log(` ✗ report-schemas: ${e.message}`)
}
try {
const n7 = await extractPortalSchemas()
console.log(` ✓ portal-schemas.json: ${n7} 个门户组件`)
} catch (e) {
console.log(` ✗ portal-schemas: ${e.message}`)
}
try {
const n8 = await extractBigdataSchemas()
console.log(` ✓ bigdata-schemas.json: ${n8} 个大屏组件`)
} catch (e) {
console.log(` ✗ bigdata-schemas: ${e.message}`)
}
try {
const n9 = extractDatasourceDrivers()
console.log(` ✓ datasource-drivers.json: ${n9} 个 driver`)
} catch (e) {
console.log(` ✗ datasource-drivers: ${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) })