Files
dacli/src/commands/org/create.ts
T
ssw df213fdfad fix(dacli-docs): 修审计报告 14 项 + 加 CI 校验防再漂 (P0/P1/P2 全清)
审计报告 docs/dacli-doc-audit-report.md 14 项全部采纳:

P0 (3 项事实错误, 直接误导 AI 执行):
- ANTI_PATTERN F_→f_ 前缀 (form update --fields 实际是小写)
- ANTI_PATTERN AP-fc-4 vs AP-fu-1 矛盾 (统一指向 form import --autoTable)
- ANTI_PATTERN form info <enCode> 误导 (改为 <id>, 加 enCode 自动解析 fallback)

P1 (3 项文档漂移):
- CLAUDE.md "含 FK 时必用 --fields" → 实际已废弃, 删除 + form/create.ts Commander 加 DEPRECATED
- doc/get.ts 引用 Java DocCommand.java → 改指 TS _parser.ts
- AP-fd-1 "--cascade 必带" 过度约束 → 改为可选

P2 (2 项衔接断裂):
- Source 路径 web/.../ → web/apps/jnpf-web-apps-main/src/ (73 文件)
- test/run TC-B 前置提示 (需 menu 挂载)

附加 (CI script 抓出的 8 条额外路径 drift, 已就近修正):
- di/create|export|copy: DataInterfaceController.java 路径补 jnpf-java-boot/ + jnpf/base/controller/
- print/delete|update: PrintDevController.java 同上
- org/create: OrganizeController.java 路径 jnpf/controller/ → jnpf/permission/controller/
- form/codegen|audit: FormGenerator/FormGenerator.vue 加 /components/ + /src/ 前缀
- workflow/create: flowEngine + FlowProcess 加 views/onlineDev/ + components/ 前缀
- workflow/syncForm: FlowableProcessDesigner.vue 不存在 → 改指 Form.vue
- msg/notice*: notice.ts 不存在 → 改指 msgMonitor.ts
- bigscreen/delete|update: 双前缀 web/.../src/jnpf-web-datascreen-vue3/ → jnpf/前端/
- app/login: api/auth.ts → api/core/auth.ts
- apis/refresh|status|info: Java ApiService 路径 → TS 化指向
- test/render: sysData/formDesign 加 views/ 前缀
- form/create: formDesign/Form.vue 加 views/onlineDev/ 前缀

Task 1.6 收尾: 删 core/help.ts legacy (~85 行 dead code):
- AI_FLAGS / getMeta / maybeInterceptAiHelp / renderAiHelp
- installAiHelpHook alias / _aiMeta 副作用 / AiHelp type alias

新增 docs/doc-drift-findings-2026-07-24.md (2 条无法自动修复的 docs 路径, 待人工)

新增 scripts/check-doc-drift.ts (scripts/check-doc-drift.ts, 218 行):
- 4 类检查: 命令引用 / arg-opt 一致性 / source 路径 / ANTI_PATTERNS 命令引用
- 接入 npm run doc:check
- 验证: 修复前 21 处 drift → 修复后 2 处 (已归档)

测试: typecheck 0 errors, 387/387 tests pass, doc:check 报告 2 known issues
2026-07-24 16:33:31 +08:00

155 lines
6.9 KiB
TypeScript

/**
* commands/org/create.ts — 新建组织 (Phase D1, v3.0+ 走 webapi/permission 镜像, v2.1+ 三层接入)
*
* 镜像 web organize/Form.vue + 后端 OrganizeController
* - PS036: 组织层级限制
* - PS013: 当前机构 id 不能与父机构 id 相同
*
* v0.11e+ Phase D1: P3 命令 step machine 集成
* v3.0+ (2026-06-29 regression audit P0): 改用 webapi/permission.createOrg
*
* v2.1+ (2026-07-21) 三层接入 (跨命令 auto-fix 模式, 参照 dict/typecreate.ts):
* L1: --verbose / --json 三模式 (commands/_output_filter.ts:setupOutputFilter)
* L2: 跨命令 auto-fix pipeline
* - enCode 含 `-`/`_` 自动 strip (S-X1 silent, BS-新38)
* - fullName 含 space/parens 自动 soften (S-X2 silent, BS-新52)
* - 已有 org 重名 → suggest 加 `<appCode>` 前缀 (G-X1 suggest)
* L3: 完成后落 CommandLog, 失败时也落 (含 PS013/PS036 错误)
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { validateOrgCreate } from './_validate.ts'
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
import {
initWebapi as initPermissionWebapi,
createOrg,
getOrganizeList,
} from '../../webapi/permission.ts'
import { runCommandAutoFix, fetchExistingLists } from '../_auto_fix.ts' // v2.1+ (2026-07-21): 跨命令 auto-fix (S-X1/S-X2 silent + G-X1 suggest)
import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ stdout 三模式 + CommandLog; v2.2+ wrapCommand 收口 5c
interface CreateOpts {
fullName: string
enCode: string
parentId: string
category: string
enabledMark?: string
sortCode?: string
json?: boolean
verbose?: boolean
}
export function buildOrgCreateCommand(): Command {
const cmd = new Command('create')
.description('新建组织 (v0.11e+ Phase D1 + step machine 校验)')
.requiredOption('--fullName <name>', '组织名称')
.requiredOption('--enCode <code>', '组织编码')
.requiredOption('--parentId <pid>', '上级组织 id (顶级传 -1)')
.requiredOption('--category <c>', '组织类型: 0=公司 1=部门 2=小组')
.option('--enabledMark <m>', '0/1 启用', '1')
.option('--sortCode <s>', '排序', '0')
addOutputOptions(cmd)
.action(async (opts: CreateOpts) => {
// v2.1+ L1: stdout 三模式 — 默认 quiet, --verbose 全部输出, --json 原始 JSON
setupOutputFilter(opts)
const c = await JnpfClient.fromToken()
const startTime = Date.now()
// v2.1+ L2 silent+assertive+suggest: 跨命令 auto-fix pipeline (S-X1/S-X2/G-X1)
const fixCtx = await fetchExistingLists(c, c.appCode)
const cmdFix = runCommandAutoFix(opts as unknown as Record<string, unknown>, fixCtx)
if (cmdFix.silent.length > 0) {
Object.assign(opts, cmdFix.opts)
}
await wrapCommand({
command: 'org create',
appCode: c.appCode,
startTime,
verbose: opts.verbose,
json: opts.json,
fixes: [...cmdFix.silent, ...cmdFix.assertive, ...cmdFix.suggestions],
}, async (ctx) => {
try {
// v0.11e+ Phase D1: step machine 校验 (PS036 层级 + PS013 parentId)
const validation = validateOrgCreate({
fullName: opts.fullName,
enCode: opts.enCode,
parentId: opts.parentId,
category: opts.category,
enabledMark: opts.enabledMark,
sortCode: opts.sortCode,
})
if (!validation.ok) {
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(
validation.steps.flatMap(s => s.errors).map(e => e.message).join('; '),
{ enCode: opts.enCode, fullName: opts.fullName },
)
}
log.info(`✓ step machine 校验通过 (${validation.steps.length}/${validation.steps.length})`)
const body = {
fullName: opts.fullName,
enCode: opts.enCode,
parentId: opts.parentId,
category: opts.category,
enabledMark: parseInt(opts.enabledMark || '1', 10),
sortCode: parseInt(opts.sortCode || '0', 10),
}
// v3.0+ P0: 走 webapi/permission.createOrg (web 端 create 函数)
await initPermissionWebapi(c)
const r = (await createOrg(body as any)) as { id?: string; code?: number; msg?: string }
if (r.code !== 200 && r.code !== undefined) {
const enhanced = enhanceFromBackend(r.msg || '', 'org', { entity: 'org' })
log.error(` ${formatErrorLine(enhanced, { step: 1, stepName: '后端API' })}`)
return ctx.fail(r.msg || `code=${r.code}`, { enCode: opts.enCode, fullName: opts.fullName, parentId: opts.parentId })
}
let id = r.id
if (!id) {
const listR = (await getOrganizeList({ parentId: opts.parentId, pageSize: 50 })) as
| { list?: { id: string; enCode: string; fullName: string }[] }
| { id: string; enCode: string; fullName: string }[]
const list = Array.isArray(listR) ? listR : listR.list || []
const found = list.find(
(d) => d.enCode === opts.enCode || d.fullName === opts.fullName,
)
id = found?.id
}
log.ok(`✓ 组织创建成功 id=${id}`)
if (opts.json) printJson({ ...(r as object), id })
ctx.success({ id, enCode: opts.enCode, fullName: opts.fullName, parentId: opts.parentId, category: opts.category })
} catch (e) {
ctx.fail(`org create 失败: ${(e as Error).message?.slice(0, 200)}`)
}
})
})
installHelpHook(cmd, {
purpose: '新建组织 (v0.11e+ Phase D1 + step machine 校验, 镜像 web organize/Form.vue + 后端 OrganizeController + PS013/PS036)',
required: {
'--fullName': '组织名称',
'--enCode': '组织编码',
'--parentId': '上级组织 id (顶级 -1)',
'--category': '0=公司 1=部门 2=小组',
},
optional: { '--enabledMark': '0/1 启用', '--sortCode': '排序' },
example: 'dacli org create --fullName=技术部 --enCode=tech_dept --parentId=-1 --category=1',
source: {
web: 'web/apps/jnpf-web-apps-main/src/views/permission/organize/OrganizeForm.vue',
api: 'POST /api/permission/Organize',
ref: 'jnpf/后端/jnpf-java-boot/jnpf-permission/jnpf-permission-controller/src/main/java/jnpf/permission/controller/OrganizeController.java',
},
notes: [
'v0.11e+ step machine (镜像 web organize/Form.vue + PS013/PS036):',
' step 0 基础信息: fullName 必填 + enCode 必填 + parentId 必填 + category ∈ {0,1,2}',
],
})
return cmd
}