feat(dacli): bigscreen/construct.ts 完整 wrapCommand 迁移 (P6 6 exit 收口)

6 处 process.exit(1) → ctx.fail():
- main action catch (构造失败兜底)
- runConstruct --from-json 文件不存在 (改 throw DacliError → handler catch → ctx.fail)
- runConstruct --from-json JSON.parse 失败 (同上)
- runConstruct validateOnly 校验失败 (原 process.exit(1) 单一分支)
- runConstruct 校验失败 (原 else 分支)
- runConstruct addObj 后端未返 id (同上)

runConstruct 内部 5 处 process.exit(1) → throw new DacliError (P5 已就绪模式)
main action 兜底 catch → ctx.fail. wrapCommand handler 自动 catch 所有 throw.

修改方式: Write 整文件重写.

验证:
  - TS compile 0 新错误 (baseline 57 不变)
  - 73/73 单元测试 PASS
This commit is contained in:
ssw committed 2026-07-21 20:25:04 +08:00
1 parent e05d87300f
commit e091907db7
1 file changed
+28 -41
+28 -41
View File
@@ -2,6 +2,7 @@
* commands/bigscreen/construct.ts — 大屏构造 (v0.13+ §8.2.4 阶段 5)
*
* 修改 P5 全量 (2026-07-21): throw new Error → DacliError 提升 L2 catch 分类.
* v2.2+ (2026-07-21): wrapCommand 收口 6 处 process.exit(1), 内部 process.exit 改 throw DacliError
*
* 用途: 一键创建带组件的大屏. 支持:
* - --template=base: 内置空大屏模板 (9 组件 demo)
@@ -34,7 +35,7 @@ import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadVisualData } from '../../webapi/visualdata.ts'
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 分类
import { runCommandAutoFix, fetchExistingLists } from '../_auto_fix.ts' // v2.1+ (2026-07-21): 跨命令 auto-fix pipeline
import {
@@ -86,6 +87,7 @@ interface ConstructOpts {
noRelease?: boolean
/** 原始 JSON 输出 */
json?: boolean
/** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */
verbose?: boolean
}
@@ -113,13 +115,11 @@ export function buildBigscreenConstructCommand(): Command {
.option('--status <s>', '状态 0/1 (默认 1=已发布, --no-release 时无效)', '1')
.option('--validate-only', '只校验, 不创建', false)
.option('--no-release', '创建后不发布 (默认 ON 发布)', false)
.option('--verbose', '完整输出 (含 step machine、API 调用等过程日志)', false)
.option('--json', '原始 JSON 输出', false)
addOutputOptions(cmd)
.action(async (opts: ConstructOpts) => {
// 输出过滤: 默认静默 (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)
@@ -128,40 +128,28 @@ export function buildBigscreenConstructCommand(): Command {
Object.assign(opts, cmdFix.opts)
}
try {
const summary = await runConstruct(opts)
// v2.1+ (2026-07-21): 成功路径落 CommandLog (L3) — validate-only 也算 success
printCommandEnd({
command: 'bigscreen construct',
appCode: c.appCode,
summary: {
await wrapCommand({
command: 'bigscreen construct',
appCode: c.appCode,
startTime: Date.now(),
verbose: opts.verbose,
json: opts.json,
fixes: [],
}, async (ctx) => {
try {
const summary = await runConstruct(opts)
ctx.success({
id: summary?.id,
enCode: summary?.enCode || opts.enCode,
validateOnly: summary?.validateOnly || false,
},
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
duration_ms: Date.now() - startTime,
verbose: opts.verbose,
json: opts.json,
})
} catch (err) {
const errMsg = `构造失败: ${(err as Error).message}`
log.error(errMsg)
if (process.env.DACLI_DEBUG === '1') log.error(err)
// v2.1+ (2026-07-21): 错误路径落 CommandLog (L3)
printCommandEnd({
command: 'bigscreen construct',
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)
}
})
} catch (err) {
const errMsg = `构造失败: ${(err as Error).message}`
log.error(errMsg)
if (process.env.DACLI_DEBUG === '1') log.error(err)
ctx.fail(errMsg, { enCode: opts.enCode })
}
})
})
installHelpHook(cmd, {
@@ -222,7 +210,7 @@ async function runConstruct(opts: ConstructOpts): Promise<{ id?: string; enCode:
// 2a. 从 JSON 文件读取
if (!existsSync(opts.fromJson)) {
log.error(`--from-json 文件不存在: ${opts.fromJson}`)
process.exit(1)
throw new DacliError(`--from-json 文件不存在: ${opts.fromJson}`)
}
try {
const json = JSON.parse(readFileSync(opts.fromJson, 'utf-8'))
@@ -230,7 +218,7 @@ async function runConstruct(opts: ConstructOpts): Promise<{ id?: string; enCode:
navInput = json.nav || []
} catch (e) {
log.error(`--from-json 解析失败: ${(e as Error).message}`)
process.exit(1)
throw new DacliError(`--from-json 解析失败: ${(e as Error).message?.slice(0, 200)}`)
}
} else if (opts.prop && opts.prop.length > 0) {
// 2b. 从 --prop 多次构造
@@ -284,8 +272,7 @@ async function runConstruct(opts: ConstructOpts): Promise<{ id?: string; enCode:
if (!validation.valid) {
log.error(`✗ 校验失败 (${validation.errors.length} 项):`)
for (const e of validation.errors) log.error(` - ${e}`)
if (opts.validateOnly) process.exit(1)
process.exit(1)
throw new DacliError(`构造校验失败 (${validation.errors.length} 项): ${validation.errors.slice(0, 3).join('; ').slice(0, 200)}`)
}
log.ok(`✓ 校验通过 (${validProps.length} 有效 prop, ${nav.length} 组件)`)
if (opts.validateOnly) {
@@ -327,7 +314,7 @@ async function runConstruct(opts: ConstructOpts): Promise<{ id?: string; enCode:
else if (data && typeof data === 'object') id = (data as { id?: string }).id
if (!id) {
log.error(`addObj 后端未返 id. raw=${JSON.stringify(addResp).slice(0, 200)}`)
process.exit(1)
throw new DacliError(`addObj 后端未返 id`)
}
log.ok(`✓ addObj 创建空大屏成功 id=${id}`)
@@ -364,4 +351,4 @@ async function runConstruct(opts: ConstructOpts): Promise<{ id?: string; enCode:
}
// v2.1+ (2026-07-21): 返回成功 summary (供 .action 落 CommandLog)
return { id, enCode: opts.enCode }
}
}