dacli(form): info --json 选项 (BS-新1, 2026-06-30)
v0.13+ BS-新1: form info 输出 formData/columnData/tables 是 escaped JSON 字符串, 需 Python 二次解码才能用 jq 提取 formData.fields. 修复: 加 --json 选项, 自动 JSON.parse 这 3 个字段 (容错失败保留原值), 使用 'dacli form info <id> --json | jq .formData.fields' 直接生效. 测试场 testspace 验证: form 841753583530543237 - 修复前: formData 是 string, 需 '| python3 -c import json; json.loads(...)' - 修复后: formData 是 dict, 直接 jq Co-Authored-By: Claude <[email protected]>
This commit is contained in:
1 parent
b098aa3ee9
commit
89d891008d
1 file changed
+46
-3
@@ -2,6 +2,14 @@
|
||||
* commands/form/info.ts — 拉表单完整 schema
|
||||
*
|
||||
* v0.11+ Phase 1 (2026-06-25): 用 webapi 调 web 端 getInfo (替代 c.get)
|
||||
*
|
||||
* v0.13+ BS-新1 (2026-06-30): 加 --json 选项
|
||||
* 后端 /visualdev/Base/{id} 返 formData/columnData/tables 是 escaped JSON 字符串,
|
||||
* 字段值如 "{\"popupType\":\"general\",...}" 这种.
|
||||
* 默认 printJson 是把外层 JSON 美化输出, 但 formData/columnData/tables 仍是 escaped 字符串.
|
||||
* 用 --json 时: 自动 JSON.parse 这 3 个字段 (容错失败保留原值),
|
||||
* 这样 `dacli form info <id> --json | jq .formData.fields` 直接生效,
|
||||
* 不需 Python 二次解码.
|
||||
*/
|
||||
import { Command } from 'commander'
|
||||
import { JnpfClient } from '../../core/client.ts'
|
||||
@@ -10,12 +18,29 @@ import { printJson, log } from '../../core/log.ts'
|
||||
import type { FormDetail } from '../../core/types.ts'
|
||||
import { initWebapi, loadVisualDev } from '../../webapi/visualDev.ts'
|
||||
|
||||
/**
|
||||
* 尝试 JSON.parse 字符串, 失败返回原值. 用于 formData/columnData/tables 解 escaped JSON.
|
||||
*/
|
||||
function tryParseJsonString(value: unknown): unknown {
|
||||
if (typeof value !== 'string') return value
|
||||
const trimmed = value.trim()
|
||||
if (!trimmed) return value
|
||||
// 启发式: 只有看起来像 JSON 对象/数组时才 parse (避免误 parse 普通字符串)
|
||||
if (trimmed[0] !== '{' && trimmed[0] !== '[') return value
|
||||
try {
|
||||
return JSON.parse(trimmed)
|
||||
} catch {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
export function buildFormInfoCommand(): Command {
|
||||
const cmd = new Command('info')
|
||||
.description('拉表单完整 schema (含 formData/columnData/tables)')
|
||||
.argument('<id>', '表单 id')
|
||||
.option('--summary', '只打概要 (id/enCode/state/type/webType/dbLinkId/字段数)', false)
|
||||
.action(async (id: string, opts: { summary?: boolean }) => {
|
||||
.option('--json', 'BS-新1: 解码 formData/columnData/tables escaped JSON, 直接 jq (无需 Python 二次解码)', false)
|
||||
.action(async (id: string, opts: { summary?: boolean; json?: boolean }) => {
|
||||
const c = await JnpfClient.fromToken()
|
||||
// v0.11+ Phase 1: 用 web 端 getInfo (替代 c.get('/visualdev/Base/{id}'))
|
||||
await initWebapi(c)
|
||||
@@ -30,14 +55,32 @@ export function buildFormInfoCommand(): Command {
|
||||
log.dim(`tables : ${tables.length} (${tables.map((t) => t.table).join(', ')})`)
|
||||
return
|
||||
}
|
||||
// v0.13+ BS-新1: --json 模式解 formData/columnData/tables escaped JSON
|
||||
if (opts.json) {
|
||||
const decoded = { ...data } as Record<string, unknown>
|
||||
for (const key of ['formData', 'columnData', 'appColumnData', 'tables']) {
|
||||
if (key in decoded) {
|
||||
decoded[key] = tryParseJsonString(decoded[key])
|
||||
}
|
||||
}
|
||||
printJson(decoded)
|
||||
return
|
||||
}
|
||||
printJson(data)
|
||||
})
|
||||
|
||||
installAiHelpHook(cmd, {
|
||||
purpose: '拉表单完整 schema. clone 前先 info 看结构',
|
||||
required: { '<id>': '表单 id' },
|
||||
optional: { '--summary': '只打概要' },
|
||||
example: ['dacli form info 123456 --summary', 'dacli form info 123456 > /tmp/x.json'],
|
||||
optional: {
|
||||
'--summary': '只打概要',
|
||||
'--json': 'BS-新1: 解码 formData/columnData/tables escaped JSON (默认输出仍是字符串, 需 Python 二次解码)',
|
||||
},
|
||||
example: [
|
||||
'dacli form info 123456 --summary',
|
||||
'dacli form info 123456 --json | jq .formData.fields', // BS-新1: 直接 jq
|
||||
'dacli form info 123456 > /tmp/x.json', // 老路径 (formData 是 escaped 字符串)
|
||||
],
|
||||
source: { api: 'GET /api/visualdev/Base/:id' },
|
||||
})
|
||||
|
||||
|
||||
Reference in new issue
Block a user