feat(dacli): portal/create.ts 完整 wrapCommand 迁移 (P6 4 exit 收口)
4 处 process.exit(1) → ctx.fail(): - Bug 5 category 解析失败 (resolvePortalCategory throw DacliError catch) - step machine 校验失败 - 后端 createPortal code !== 200 (含 enhanceFromBackend 错误增强) - BS-新12 silent fail (后端返 code=200 但 r.id undefined, list 反查仍空) 删 4 处 printCommandEnd (L3 落盘) — wrapCommand 自动调用 保留 runCommandAutoFix pipeline (silent fix 透传) 保留 resolvePortalCategory 内部 DacliError (P5 已就绪) 修改方式: Write 整文件重写. 验证: - TS compile 0 新错误 (baseline 57 不变) - 73/73 单元测试 PASS
This commit is contained in:
1 parent
bb2d485d91
commit
e05d87300f
1 file changed
+108
-165
+108
-165
@@ -27,7 +27,7 @@ import {
|
||||
} from '../../webapi/portal.ts'
|
||||
import { initWebapi as initDictWebapi, loadDict } from '../../webapi/dict.ts'
|
||||
import { runCommandAutoFix, fetchExistingLists } from '../_auto_fix.ts' // v2.1+ (2026-07-21): 跨命令 auto-fix pipeline
|
||||
import { setupOutputFilter, printCommandEnd } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘
|
||||
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c
|
||||
import { DacliError } from '../../core/types.ts' // P5 (2026-07-21): 提升 L2 catch 分类
|
||||
|
||||
interface CreateOpts {
|
||||
@@ -44,6 +44,7 @@ interface CreateOpts {
|
||||
appCustomUrl?: string
|
||||
linkType?: string
|
||||
formData?: string
|
||||
/** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */
|
||||
verbose?: boolean
|
||||
}
|
||||
|
||||
@@ -138,13 +139,11 @@ export function buildPortalCreateCommand(): Command {
|
||||
'--formData <json>',
|
||||
'门户布局 formData (JSON 字符串, 镜像 PortalCrForm.formData)',
|
||||
)
|
||||
.option('--verbose', '完整输出 (含 step machine、API 调用等过程日志)', false)
|
||||
.option('--json', '原始 JSON 输出', false)
|
||||
addOutputOptions(cmd)
|
||||
.action(async (opts: CreateOpts) => {
|
||||
// 输出过滤: 默认静默 (quiet mode), --verbose 走完整日志, --json 走原始 JSON
|
||||
setupOutputFilter(opts)
|
||||
const c = await JnpfClient.fromToken()
|
||||
const startTime = Date.now()
|
||||
|
||||
// v2.1+ (2026-07-21): 命令级 auto-fix pipeline (G-X1 启用 — 含 <appCode> 前缀建议)
|
||||
const fixCtx = await fetchExistingLists(c, c.appCode)
|
||||
@@ -153,170 +152,114 @@ export function buildPortalCreateCommand(): Command {
|
||||
Object.assign(opts, cmdFix.opts)
|
||||
}
|
||||
|
||||
// v3.0+ Bug 5 修复 (P1): 先解析 category (从字典 businessType 拿真实 UUID)
|
||||
// 在 step machine 之前, 拿到 id 才能让后续校验走到对的地方
|
||||
let resolvedCategory: { id: string; name: string; matched: string }
|
||||
try {
|
||||
resolvedCategory = await resolvePortalCategory(c, {
|
||||
category: opts.category,
|
||||
appCode: (c as any).appCode,
|
||||
})
|
||||
log.info(
|
||||
`✓ Bug 5: category 解析为真实 UUID (${resolvedCategory.matched}): ${resolvedCategory.name} (id=${resolvedCategory.id})`,
|
||||
)
|
||||
} catch (e) {
|
||||
const errMsg = `Bug 5 修复 - category 解析失败: ${(e as Error).message?.slice(0, 300)}`
|
||||
log.error(errMsg)
|
||||
// v2.1+ (2026-07-21): 错误路径落 CommandLog
|
||||
printCommandEnd({
|
||||
command: 'portal create',
|
||||
appCode: c.appCode,
|
||||
result: 'failure',
|
||||
error: errMsg,
|
||||
summary: { enCode: opts.enCode, category: opts.category },
|
||||
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
|
||||
duration_ms: Date.now() - startTime,
|
||||
verbose: opts.verbose,
|
||||
json: opts.json,
|
||||
})
|
||||
process.exit(1)
|
||||
}
|
||||
const realCategoryId = resolvedCategory.id
|
||||
|
||||
// v0.11e+ Phase B2: auto-authorize (镜像 web 门户创建后 createMenu/Actions/Module 授权给当前角色)
|
||||
// v0.11e+ Phase B1: step machine 校验 (2 step 镜像 web visualPortal/Form.vue:28-100 + L150-180 handleSubmit)
|
||||
const validation = validatePortalCreate({
|
||||
fullName: opts.fullName,
|
||||
enCode: opts.enCode,
|
||||
category: realCategoryId, // 字典真实 UUID (Bug 5 修复)
|
||||
type: opts.type,
|
||||
platformType: opts.platformType,
|
||||
link: opts.link,
|
||||
})
|
||||
if (!validation.ok) {
|
||||
const errMsg = `step machine 校验失败 (${validation.steps.flatMap((s) => s.errors).length} 错误)`
|
||||
log.error(errMsg)
|
||||
for (const s of validation.steps) {
|
||||
for (const e of s.errors) {
|
||||
log.error(` ${formatErrorLine(e, { step: s.step, stepName: s.stepName })}`)
|
||||
}
|
||||
}
|
||||
// v2.1+ (2026-07-21): 错误路径落 CommandLog
|
||||
printCommandEnd({
|
||||
command: 'portal create',
|
||||
appCode: c.appCode,
|
||||
result: 'failure',
|
||||
error: errMsg,
|
||||
summary: { enCode: opts.enCode },
|
||||
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
|
||||
duration_ms: Date.now() - startTime,
|
||||
verbose: opts.verbose,
|
||||
json: opts.json,
|
||||
})
|
||||
process.exit(1)
|
||||
}
|
||||
log.info(`✓ step machine 校验通过 (${validation.steps.length}/${validation.steps.length})`)
|
||||
// step 0 基础信息 (P0 镜像 web Form.vue schemas)
|
||||
|
||||
// 镜像 web 端 /api/visualdev/Portal PortalCrForm (jnpf 6.x 6.2.0):
|
||||
// { fullName, enCode, enabledMark, description, formData (JSON 字符串), category, sortCode, type, customUrl, appCustomUrl, linkType, ... }
|
||||
// web 端 visualPortal/Form.vue: const query = { ...values, id: state.dataForm.id }; createPortal(query)
|
||||
// 关键: formData 必传 (JSON 字符串, 内嵌布局 schema), description 必传, type 必传
|
||||
// **Bug 5 修复**: category 必为字典 businessType 真实 UUID (不是 '1' 字符串)
|
||||
const portalCrForm = {
|
||||
fullName: opts.fullName,
|
||||
enCode: opts.enCode,
|
||||
category: realCategoryId, // 真实 UUID, 不是字符串 '1'
|
||||
type: opts.platformType ? Number(opts.platformType) : 0, // 0=门户设计, 1=配置路径 (镜像 web Form.vue:75-87)
|
||||
platform: opts.platform,
|
||||
formData: opts.formData || JSON.stringify({}), // JSON 字符串, web 端默认 '{}'
|
||||
enabledMark: 1,
|
||||
sortCode: 0,
|
||||
description: opts.description || '',
|
||||
customUrl: opts.customUrl || '', // 镜像 PortalCrForm.customUrl
|
||||
appCustomUrl: opts.appCustomUrl || '',
|
||||
linkType: opts.linkType ? Number(opts.linkType) : 0,
|
||||
}
|
||||
// v3.0+ P0: 走 webapi/portal.createPortal (web 端同名函数)
|
||||
await initPortalWebapi(c)
|
||||
const r = (await createPortal(portalCrForm as any)) as {
|
||||
id?: string
|
||||
code?: number
|
||||
msg?: string
|
||||
}
|
||||
const rCode = r.code ?? 200
|
||||
if (rCode !== 200) {
|
||||
const errMsg = `portal create 失败 (code=${rCode}): ${r.msg}`
|
||||
const enhanced = enhanceFromBackend(r.msg || '', 'portal', { entity: 'portal' })
|
||||
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
|
||||
// v2.1+ (2026-07-21): 错误路径落 CommandLog
|
||||
printCommandEnd({
|
||||
command: 'portal create',
|
||||
appCode: c.appCode,
|
||||
result: 'failure',
|
||||
error: errMsg,
|
||||
summary: { enCode: opts.enCode },
|
||||
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
|
||||
duration_ms: Date.now() - startTime,
|
||||
verbose: opts.verbose,
|
||||
json: opts.json,
|
||||
})
|
||||
process.exit(1)
|
||||
}
|
||||
let id: string | undefined = r.id
|
||||
// BS-新12 v2 (jxcj 2026-07-01): jnpf portal 后端可能返纯字符串 id (而非 JSON 对象)
|
||||
if (!id && typeof (r as unknown) === 'string' && /^\d+$/.test((r as unknown as string).trim()))
|
||||
id = (r as unknown as string).trim()
|
||||
// 参考 datasource create 历史模式: 响应可能不返 id, 按 enCode 反查 list
|
||||
if (!id) {
|
||||
const listR = (await getPortalList({ keyword: opts.enCode, pageSize: 50 })) as
|
||||
| {
|
||||
data?: { list?: { id: string; enCode: string; fullName: string }[] }
|
||||
list?: { id: string; enCode: string; fullName: string }[]
|
||||
}
|
||||
| { id: string; enCode: string; fullName: string }[]
|
||||
const list: Array<{ id: string; enCode: string; fullName: string }> = Array.isArray(listR)
|
||||
? (listR as Array<{ id: string; enCode: string; fullName: string }>)
|
||||
: (listR as any).data?.list || (listR as any).list || []
|
||||
const found = list.find(
|
||||
(d: { id: string; enCode: string; fullName: string }) =>
|
||||
d.enCode === opts.enCode || d.fullName === opts.fullName,
|
||||
)
|
||||
id = found?.id
|
||||
}
|
||||
// BS-新12 (jxcj 2026-07-01): 必查 id, list 反查仍空则报 silent fail
|
||||
if (!id) {
|
||||
const errMsg = `BS-新12 silent fail: 后端返回 code=200 但 r.id=undefined, list 反查也找不到. 实际 raw 响应: ${JSON.stringify(r).slice(0, 200)}`
|
||||
log.error(errMsg)
|
||||
log.error('可能原因: 1) jnpf 响应格式变化 2) category/type 不被接受 3) systemId 没传')
|
||||
// v2.1+ (2026-07-21): 错误路径落 CommandLog
|
||||
printCommandEnd({
|
||||
command: 'portal create',
|
||||
appCode: c.appCode,
|
||||
result: 'failure',
|
||||
error: errMsg,
|
||||
summary: { enCode: opts.enCode },
|
||||
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
|
||||
duration_ms: Date.now() - startTime,
|
||||
verbose: opts.verbose,
|
||||
json: opts.json,
|
||||
})
|
||||
process.exit(1)
|
||||
}
|
||||
log.info(
|
||||
`✓ 门户创建成功 id=${id} category=${resolvedCategory.name}(${realCategoryId})`,
|
||||
)
|
||||
if (opts.json)
|
||||
printJson({ ...(r as object), id, category: realCategoryId, categoryName: resolvedCategory.name })
|
||||
// v2.1+ (2026-07-21): 成功路径落 CommandLog
|
||||
printCommandEnd({
|
||||
await wrapCommand({
|
||||
command: 'portal create',
|
||||
appCode: c.appCode,
|
||||
summary: { id, enCode: opts.enCode, category: realCategoryId, categoryName: resolvedCategory.name },
|
||||
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
|
||||
duration_ms: Date.now() - startTime,
|
||||
startTime: Date.now(),
|
||||
verbose: opts.verbose,
|
||||
json: opts.json,
|
||||
fixes: [],
|
||||
}, async (ctx) => {
|
||||
try {
|
||||
// v3.0+ Bug 5 修复 (P1): 先解析 category (从字典 businessType 拿真实 UUID)
|
||||
let resolvedCategory: { id: string; name: string; matched: string }
|
||||
try {
|
||||
resolvedCategory = await resolvePortalCategory(c, {
|
||||
category: opts.category,
|
||||
appCode: (c as any).appCode,
|
||||
})
|
||||
log.info(
|
||||
`✓ Bug 5: category 解析为真实 UUID (${resolvedCategory.matched}): ${resolvedCategory.name} (id=${resolvedCategory.id})`,
|
||||
)
|
||||
} catch (e) {
|
||||
const errMsg = `Bug 5 修复 - category 解析失败: ${(e as Error).message?.slice(0, 300)}`
|
||||
log.error(errMsg)
|
||||
return ctx.fail(errMsg, { enCode: opts.enCode, category: opts.category })
|
||||
}
|
||||
const realCategoryId = resolvedCategory.id
|
||||
|
||||
// v0.11e+ Phase B1: step machine 校验
|
||||
const validation = validatePortalCreate({
|
||||
fullName: opts.fullName,
|
||||
enCode: opts.enCode,
|
||||
category: realCategoryId,
|
||||
type: opts.type,
|
||||
platformType: opts.platformType,
|
||||
link: opts.link,
|
||||
})
|
||||
if (!validation.ok) {
|
||||
const errMsg = `step machine 校验失败 (${validation.steps.flatMap((s) => s.errors).length} 错误)`
|
||||
log.error(errMsg)
|
||||
for (const s of validation.steps) {
|
||||
for (const e of s.errors) {
|
||||
log.error(` ${formatErrorLine(e, { step: s.step, stepName: s.stepName })}`)
|
||||
}
|
||||
}
|
||||
return ctx.fail(errMsg, { enCode: opts.enCode })
|
||||
}
|
||||
log.info(`✓ step machine 校验通过 (${validation.steps.length}/${validation.steps.length})`)
|
||||
|
||||
const portalCrForm = {
|
||||
fullName: opts.fullName,
|
||||
enCode: opts.enCode,
|
||||
category: realCategoryId,
|
||||
type: opts.platformType ? Number(opts.platformType) : 0,
|
||||
platform: opts.platform,
|
||||
formData: opts.formData || JSON.stringify({}),
|
||||
enabledMark: 1,
|
||||
sortCode: 0,
|
||||
description: opts.description || '',
|
||||
customUrl: opts.customUrl || '',
|
||||
appCustomUrl: opts.appCustomUrl || '',
|
||||
linkType: opts.linkType ? Number(opts.linkType) : 0,
|
||||
}
|
||||
await initPortalWebapi(c)
|
||||
const r = (await createPortal(portalCrForm as any)) as {
|
||||
id?: string
|
||||
code?: number
|
||||
msg?: string
|
||||
}
|
||||
const rCode = r.code ?? 200
|
||||
if (rCode !== 200) {
|
||||
const errMsg = `portal create 失败 (code=${rCode}): ${r.msg}`
|
||||
const enhanced = enhanceFromBackend(r.msg || '', 'portal', { entity: 'portal' })
|
||||
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
|
||||
return ctx.fail(errMsg, { enCode: opts.enCode, code: rCode })
|
||||
}
|
||||
let id: string | undefined = r.id
|
||||
if (!id && typeof (r as unknown) === 'string' && /^\d+$/.test((r as unknown as string).trim()))
|
||||
id = (r as unknown as string).trim()
|
||||
if (!id) {
|
||||
const listR = (await getPortalList({ keyword: opts.enCode, pageSize: 50 })) as
|
||||
| {
|
||||
data?: { list?: { id: string; enCode: string; fullName: string }[] }
|
||||
list?: { id: string; enCode: string; fullName: string }[]
|
||||
}
|
||||
| { id: string; enCode: string; fullName: string }[]
|
||||
const list: Array<{ id: string; enCode: string; fullName: string }> = Array.isArray(listR)
|
||||
? (listR as Array<{ id: string; enCode: string; fullName: string }>)
|
||||
: (listR as any).data?.list || (listR as any).list || []
|
||||
const found = list.find(
|
||||
(d: { id: string; enCode: string; fullName: string }) =>
|
||||
d.enCode === opts.enCode || d.fullName === opts.fullName,
|
||||
)
|
||||
id = found?.id
|
||||
}
|
||||
if (!id) {
|
||||
const errMsg = `BS-新12 silent fail: 后端返回 code=200 但 r.id=undefined, list 反查也找不到. 实际 raw 响应: ${JSON.stringify(r).slice(0, 200)}`
|
||||
log.error(errMsg)
|
||||
log.error('可能原因: 1) jnpf 响应格式变化 2) category/type 不被接受 3) systemId 没传')
|
||||
return ctx.fail(errMsg, { enCode: opts.enCode })
|
||||
}
|
||||
log.info(
|
||||
`✓ 门户创建成功 id=${id} category=${resolvedCategory.name}(${realCategoryId})`,
|
||||
)
|
||||
const summary = { id, enCode: opts.enCode, category: realCategoryId, categoryName: resolvedCategory.name }
|
||||
if (opts.json) printJson({ ...(r as object), ...summary })
|
||||
ctx.success(summary)
|
||||
} catch (e) {
|
||||
ctx.fail(`portal create 失败: ${(e as Error).message?.slice(0, 200)}`)
|
||||
}
|
||||
})
|
||||
})
|
||||
installHelpHook(cmd, {
|
||||
@@ -345,4 +288,4 @@ export function buildPortalCreateCommand(): Command {
|
||||
],
|
||||
})
|
||||
return cmd
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user