feat(dacli): form/codegen.ts wrapCommand 迁移 (P2 batch4, 8 exit 收口)

全 action 包 wrapCommand + try/catch. 3 action process.exit + ensureAlias helper 5 process.exit → throw/ctx.fail.
顶层提前 const c (原 c 在首个 exit 后声明), 删冗余 const c. 空 tree/json 早返 → ctx.success.

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
sswandClaude committed 2026-07-21 21:47:12 +08:00
1 parent 32904b2efc
commit 2bce9dd3e1
1 file changed
+25 -11
+25 -11
View File
@@ -24,6 +24,7 @@ import { writeFileSync, mkdirSync, existsSync, readdirSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { wrapCommand } from '../_output_filter.ts' // v2.2+ (2026-07-21): wrapCommand 收口 5c (8 process.exit + ensureAlias throw)
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadVisualDev } from '../../webapi/visualDev.ts'
@@ -523,25 +524,25 @@ async function ensureAlias(
aliasTables = JSON.parse(content)
} catch (e) {
log.error(`--alias-file JSON 解析失败: ${(e as Error).message}`)
process.exit(2)
throw new Error(`--alias-file JSON 解析失败: ${(e as Error).message}`)
}
} else if (opts.alias) {
try {
aliasTables = JSON.parse(opts.alias)
} catch (e) {
log.error(`--alias JSON 解析失败: ${(e as Error).message}`)
process.exit(2)
throw new Error(`--alias JSON 解析失败: ${(e as Error).message}`)
}
} else if (opts.autoAlias) {
// 从 form.tables 推导主表别名
const origMainClassName = opts.mainClassName || ''
if (!origMainClassName) {
log.error('--auto-alias 需要 --mainClassName (或 form.enCode 已是 camelCase)')
process.exit(2)
throw new Error('--auto-alias 需要 --mainClassName (或 form.enCode 已是 camelCase)')
}
if (!Array.isArray(formTables) || formTables.length === 0) {
log.error('--auto-alias: form.tables 为空, 无法推导 (确认 form 已发布且含主表)')
process.exit(2)
throw new Error('--auto-alias: form.tables 为空, 无法推导 (确认 form 已发布且含主表)')
}
// JNPF alias 系统强制要求前 2 字符小写 (jnpf/.../VisualAliasServiceImpl.java:160-172)
// aibridge code-rule.md 推荐 PascalCase (如 JxcjTeam), 与 JNPF 冲突, 自动转 camelCase
@@ -586,7 +587,7 @@ async function ensureAlias(
const r = (await loadVisualDev.saveAlias(formId, { tableList: aliasTables })) as { code?: number; msg?: string }
if (r.code !== 200 && r.code !== undefined) {
log.error(`Alias/Save 失败 (code=${r.code}): ${r.msg}`)
process.exit(1)
throw new Error(`Alias/Save 失败 (code=${r.code}): ${r.msg}`)
}
log.ok(`✓ 已保存 ${aliasTables.length} 个表别名 (CodePreview 将使用这些别名生成代码, 不需要 post-fix)`)
return true
@@ -628,15 +629,23 @@ export function buildFormCodegenCommand(): Command {
.option('--auto-menu', '生成完直接调 menu create 挂菜单', false)
.option('--json', '原始 JSON 输出 (file tree)', false)
.action(async (formId: string, opts: CodegenOpts) => {
const c = await JnpfClient.fromToken()
await wrapCommand({
command: 'form codegen',
appCode: c.appCode,
startTime: Date.now(),
json: opts.json,
fixes: [],
}, async (ctx) => {
try {
// 0. 校验 company / module 格式 (aibridge §变量对应关系)
const company = opts.company || 'ssw'
const companyErr = validateCode(company, '--company')
if (companyErr) {
log.error(companyErr)
process.exit(2)
return ctx.fail(companyErr, { formId })
}
const c = await JnpfClient.fromToken()
// Phase 2: 改用 webapi
await initWebapi(c)
// '--dbLinkId=default' 字面量 → token.dbLinkId
@@ -650,7 +659,7 @@ export function buildFormCodegenCommand(): Command {
const moduleErr = validateCode(module, '--module (或 form.enCode)')
if (moduleErr) {
log.error(moduleErr)
process.exit(2)
return ctx.fail(moduleErr, { formId })
}
const mainClassName = opts.mainClassName || snakeToPascalCase(enCode, module)
@@ -755,16 +764,16 @@ export function buildFormCodegenCommand(): Command {
})
log.error(' 可能原因:')
for (const cause of causes) log.error(` ${cause}`)
process.exit(1)
return ctx.fail(`CodePreview 请求失败: ${err.message}`, { formId })
}
const tree = r.list || []
if (tree.length === 0) {
log.warn('CodePreview 返回空 list, 没文件可生成 (确认 form 已发布 state=1)')
return
return ctx.success({ formId, enCode, files: 0, note: 'empty tree' })
}
if (opts.json) {
printJson({ formId, enCode, fullName, company, module, moduleArtifactId, modulePackage, body, tree })
return
return ctx.success({ formId, enCode, files: tree.length })
}
// 2. 展开 tree, 分类
@@ -982,6 +991,11 @@ export function buildFormCodegenCommand(): Command {
if (opts.autoMenu) {
await autoMountMenu(c, formId, fullName, enCode)
}
ctx.success({ formId, enCode, module, moduleArtifactId })
} catch (e) {
ctx.fail(`form codegen 失败: ${(e as Error).message?.slice(0, 200)}`, { formId })
}
})
})
installHelpHook(cmd, {
purpose: '表单代码生成 (白皮书 5.5) — 调平台 CodePreview, 写到本地目录, 参考 aibridge CodeGenerator',