diff --git a/src/commands/app/clear.ts b/src/commands/app/clear.ts index e3a17a5..c836c94 100644 --- a/src/commands/app/clear.ts +++ b/src/commands/app/clear.ts @@ -86,9 +86,10 @@ interface ClearOpts { includeSchedule?: boolean // base_schedule 等 3 张 (默认 true) includeDict?: boolean // base_dictionary_type + data (默认 true) includeBiz?: boolean // 业务库 RENAME (默认 true) + includeGhost?: boolean // ghost 模式: 跨 systemId 残留清理 (默认 false, 高危) // 短手: --only=workflow,di / --scope=conservative only?: string // 只清理这些范围 (空=全) - scopePreset?: string // 'conservative' (只 form+dict) | 'all' (默认) + scopePreset?: string // 'conservative' (只 form+dict) | 'all' (默认) | 'with-ghost' } interface Paths { @@ -162,16 +163,18 @@ export function buildAppClearCommand(): Command { .option('--verbose', '完整输出 (含 dry-run 详细预览)', false) .option('--json', '原始 JSON 输出 (--dry-run + --json 给 AI 解析清单; 真实执行时仍走人类 TTY)', false) // P1-2e (2026-07-23): 按范围控制清理 (扩展 --clean-docs 思路) + // 用 --no-include-xxx 命名让 commander 自动映射 includeXxx=false (与 ssw-loop 的 --no-restart 同模式) .option('--only ', '只清这些范围 (逗号分隔: workflow,di,portal,print,report,schedule,dict,biz,form; 例: --only=workflow,di 跨 app 清残留)') .option('--scope ', '预设范围: all (默认, 全清) | conservative (只 form+dict+biz, 不动 workflow/di/portal 全局资源)') - .option('--include-workflow', '包含 workflow_template 等 23 张 (默认 true)', true) - .option('--include-di', '包含 base_data_interface 等 5 张 (默认 true)', true) - .option('--include-portal', '包含 base_portal 等 3 张 (默认 true)', true) - .option('--include-print', '包含 base_print_template 等 3 张 (默认 true)', true) - .option('--include-report', '包含 report_template (默认 true)', true) - .option('--include-schedule', '包含 base_schedule 等 3 张 (默认 true)', true) - .option('--include-dict', '包含 base_dictionary_type + data (默认 true)', true) - .option('--include-biz', '包含业务库 RENAME (默认 true)', true) + .option('--no-include-workflow', '不包含 workflow_template 等 23 张 (默认 true)') + .option('--no-include-di', '不包含 base_data_interface 等 5 张 (默认 true)') + .option('--no-include-portal', '不包含 base_portal 等 3 张 (默认 true)') + .option('--no-include-print', '不包含 base_print_template 等 3 张 (默认 true)') + .option('--no-include-report', '不包含 report_template (默认 true)') + .option('--no-include-schedule', '不包含 base_schedule 等 3 张 (默认 true)') + .option('--no-include-dict', '不包含 base_dictionary_type + data (默认 true)') + .option('--no-include-biz', '不包含业务库 RENAME (默认 true)') + .option('--include-ghost', 'P1-2e 2026-07-23: 启用 ghost 模式, 清理跨 systemId 残留 (高危, 默认 false; 适用于删+重建同名 app 后清理旧 enCode)') .action(async (opts: ClearOpts) => { // v2.1+ L1: stdout 三模式 (default=quiet, --verbose=完整 dry-run, --json=仅 dry-run 输出 JSON) setupOutputFilter(opts) @@ -237,14 +240,15 @@ export function buildAppClearCommand(): Command { // P1-2e (2026-07-23) '--only ': '只清这些范围 (例: --only=workflow,di,dict 跨 app 清理残留)', '--scope ': '预设: all (默认) | conservative (只 form+dict+biz, 不动 workflow/di 全局资源)', - '--include-workflow / --no-include-workflow': '是否清 workflow_template 等 23 张 (默认 true)', - '--include-di / --no-include-di': '是否清 base_data_interface 等 5 张 (默认 true)', - '--include-portal / --no-include-portal': '是否清 base_portal 等 3 张 (默认 true)', - '--include-print / --no-include-print': '是否清 base_print_template 等 3 张 (默认 true)', - '--include-report / --no-include-report': '是否清 report_template (默认 true)', - '--include-schedule / --no-include-schedule': '是否清 base_schedule 等 3 张 (默认 true)', - '--include-dict / --no-include-dict': '是否清 base_dictionary_type + data (默认 true)', - '--include-biz / --no-include-biz': '是否清业务库 RENAME (默认 true)', + '--no-include-workflow': '不清理 workflow_template 等 23 张 (默认清理)', + '--no-include-di': '不清理 base_data_interface 等 5 张 (默认清理)', + '--no-include-portal': '不清理 base_portal 等 3 张 (默认清理)', + '--no-include-print': '不清理 base_print_template 等 3 张 (默认清理)', + '--no-include-report': '不清理 report_template (默认清理)', + '--no-include-schedule': '不清理 base_schedule 等 3 张 (默认清理)', + '--no-include-dict': '不清理 base_dictionary_type + data (默认清理)', + '--no-include-biz': '不清理业务库 RENAME (默认清理)', + '--include-ghost': 'P1-2e: 启用 ghost 模式 (跨 systemId 残留, 高危). 删+重建同名 app 后清旧 enCode 用', }, example: [ 'dacli app clear --appCode=tds2 --dry-run # AI 也可跑, 看清单', @@ -252,7 +256,7 @@ export function buildAppClearCommand(): Command { 'dacli app clear --appCode=jxcj --dry-run --verbose # 7 大类分组预览', 'dacli app clear --appCode=jxcj --only=workflow,di # 只清 workflow + DI 残留', 'dacli app clear --appCode=jxcj --scope=conservative # 保守: 不动全局资源', - 'dacli app clear --appCode=jxcj --no-include-workflow --include-di # 显式关 workflow, 其他全清', + 'dacli app clear --appCode=jxcj --no-include-workflow --no-include-di # 关 workflow + DI, 其他全清', 'dacli app clear --appCode=tds2 --skip-db # 只清文件 + 构建配置', ].join('\n'), source: { @@ -335,6 +339,7 @@ async function runClear(opts: ClearOpts): Promise { // ---- (4) 渲染预览 ---- const previewTs = formatTs('yyyyMMddHHmmss') + const scopeFlags = computeScopeFlags(opts) // P1-2e: 解析 includeXxx 实际值, 给 preview 用 renderPreview({ appCode: opts.appCode, layout, @@ -344,6 +349,8 @@ async function runClear(opts: ClearOpts): Promise { dbPreview, tokenAppCode, previewTs, + scopeFlags, + scopeStr, }) // v3.0+ P1-2d: docsAppDir 残留警告 (默认 --clean-docs=false 行为, 但提示用户有残留) @@ -544,34 +551,71 @@ async function executeDbClear(appCode: string, dbTs: string, scopeStr: string): } // ===================================================================== -// P1-2e (2026-07-23): scope 字符串构造 -// - opts.only="workflow,di" → 只清这两个, 其余关 -// - opts.includeXxx=false → 显式关 (从默认全开中扣掉) -// - opts.scopePreset="conservative" → 仅 form+dict+biz -// - 默认 → "all" +// P1-2e (2026-07-23): scope 标志解析 +// 返回 8 个 includeXxx 实际值, 给 preview 标"被跳过"用 // ===================================================================== -function buildScopeString(opts: ClearOpts): string { +interface ScopeFlags { + workflow: boolean + di: boolean + portal: boolean + print: boolean + report: boolean + schedule: boolean + dict: boolean + biz: boolean + ghost: boolean // P1-2e: 跨 systemId 残留清理 +} + +function computeScopeFlags(opts: ClearOpts): ScopeFlags { if (opts.scopePreset === 'conservative') { - return 'form,dict,biz' + return { workflow: false, di: false, portal: false, print: false, report: false, schedule: false, dict: true, biz: true, ghost: false } } - // opts.only 优先 if (opts.only && opts.only.length > 0) { - return opts.only + const includeSet = new Set(opts.only.split(',').map(s => s.trim().toLowerCase())) + return { + workflow: includeSet.has('workflow'), + di: includeSet.has('di'), + portal: includeSet.has('portal'), + print: includeSet.has('print'), + report: includeSet.has('report'), + schedule: includeSet.has('schedule'), + dict: includeSet.has('dict') || includeSet.has('form'), // form 隐含 dict + biz: includeSet.has('biz'), + ghost: includeSet.has('ghost'), + } } - // 显式 includeXxx=false → 从全开中扣掉 (反推 include 列表) - const hasExplicit = ['Workflow', 'Di', 'Portal', 'Print', 'Report', 'Schedule', 'Dict', 'Biz'] - .some(k => (opts as any)[`include${k}`] === false) - if (!hasExplicit) return 'all' + return { + workflow: opts.includeWorkflow !== false, + di: opts.includeDi !== false, + portal: opts.includePortal !== false, + print: opts.includePrint !== false, + report: opts.includeReport !== false, + schedule: opts.includeSchedule !== false, + dict: opts.includeDict !== false, + biz: opts.includeBiz !== false, + ghost: !!opts.includeGhost, // 默认 false (高危) + } +} + +function buildScopeString(opts: ClearOpts): string { + if (opts.only && opts.only.length > 0) return opts.only + const flags = computeScopeFlags(opts) + // P1-2e: ghost 模式独立成 "ghost" 短手, 不混在 all/conservative 里 + if (flags.ghost && Object.entries(flags).every(([k, v]) => k === 'ghost' || v)) { + return 'ghost' + } + if (Object.values(flags).every(v => v)) return 'all' // form 始终开 const includes = ['form'] - if (opts.includeWorkflow !== false) includes.push('workflow') - if (opts.includeDi !== false) includes.push('di') - if (opts.includePortal !== false) includes.push('portal') - if (opts.includePrint !== false) includes.push('print') - if (opts.includeReport !== false) includes.push('report') - if (opts.includeSchedule !== false) includes.push('schedule') - if (opts.includeDict !== false) includes.push('dict') - if (opts.includeBiz !== false) includes.push('biz') + if (flags.workflow) includes.push('workflow') + if (flags.di) includes.push('di') + if (flags.portal) includes.push('portal') + if (flags.print) includes.push('print') + if (flags.report) includes.push('report') + if (flags.schedule) includes.push('schedule') + if (flags.dict) includes.push('dict') + if (flags.biz) includes.push('biz') + if (flags.ghost) includes.push('ghost') return includes.join(',') } @@ -792,14 +836,23 @@ function renderPreview(ctx: { dbPreview: DbClearPreview | null tokenAppCode: string | null previewTs: string + // P1-2e: scope 标志, 用于 preview 标"将被跳过" + scopeFlags: ScopeFlags + scopeStr: string }): void { - const { appCode, layout, fileTargets, pomCount, ymlHasDs, dbPreview, tokenAppCode, previewTs } = ctx + const { appCode, layout, fileTargets, pomCount, ymlHasDs, dbPreview, tokenAppCode, previewTs, scopeFlags, scopeStr } = ctx log.info('') log.info(`目标应用: ${appCode}`) log.info(`工作目录: ${layout.workspace}`) if (dbPreview?.dbConn) { log.info(`备份去向: \`${HISTORY_BAK_DB}\`.\`D{ts}-{原表名}\` (示例 ts=${previewTs})`) } + // P1-2e: 显示 scope 摘要 (用 --no-include-* 关闭的范围会标 [SKIP]) + const skipped = (Object.entries(scopeFlags) as [keyof ScopeFlags, boolean][]) + .filter(([_, v]) => !v).map(([k]) => k) + if (skipped.length > 0) { + log.info(`当前 scope=${scopeStr}, 不清理: ${skipped.join(', ')}`) + } log.info('─'.repeat(60)) // 1. 文件 @@ -851,29 +904,38 @@ function renderPreview(ctx: { log.info(` 主库平台表 (systemId=${systemId}): (无受影响行)`) } else { // P1-2e: 按范围分组渲染 (后端透传 7 类 hits, 若未透传则退化为合并 mainHits) - const groups: Array<[string, TableHit[] | undefined, string]> = [ - ['字典 (jxcjxxx 等)', dictHits, 'base_dictionary'], - ['工作流 (workflow_template + 子表)', workflowHits, 'workflow_'], - ['数据接口 (DI)', diHits, 'base_data_interface'], - ['门户 (Portal)', portalHits, 'base_portal'], - ['打印模板 (Print)', printHits, 'base_print'], - ['报表 (Report)', reportHits, 'report_template'], - ['定时任务 (Schedule)', scheduleHits, 'base_schedule'], - ['其他 (form/menu/db_link/system)', undefined, ''], + // P1-2e: 第 3 列 = scopeKey, 用于匹配 scopeFlags 决定是否 [SKIP] + const groups: Array<[string, TableHit[] | undefined, string, keyof ScopeFlags | null]> = [ + ['字典 (jxcjxxx 等)', dictHits, 'base_dictionary', 'dict'], + ['工作流 (workflow_template + 子表)', workflowHits, 'workflow_', 'workflow'], + ['数据接口 (DI)', diHits, 'base_data_interface', 'di'], + ['门户 (Portal)', portalHits, 'base_portal', 'portal'], + ['打印模板 (Print)', printHits, 'base_print', 'print'], + ['报表 (Report)', reportHits, 'report_template', 'report'], + ['定时任务 (Schedule)', scheduleHits, 'base_schedule', 'schedule'], + ['其他 (form/menu/db_link/system)', undefined, '', null], ] const classifiedTables = new Set() - for (const [name, hits, prefix] of groups) { + for (const [name, hits, prefix, scopeKey] of groups) { + // 先选 bucket (不管是否 SKIP, 都需要 mark 已分类) let bucket: TableHit[] if (hits && hits.length > 0) { bucket = hits } else if (prefix) { bucket = mainHits.filter(h => h.table.startsWith(prefix)) } else { - // 兜底: 不在前面 7 类的 mainHits bucket = mainHits.filter(h => !classifiedTables.has(h.table)) } - if (bucket.length === 0) continue for (const h of bucket) classifiedTables.add(h.table) + if (bucket.length === 0) continue + // P1-2e: 该组被 scope 关闭 → 标 SKIP + if (scopeKey && !scopeFlags[scopeKey]) { + log.info(` [${name}] [SKIP-by-scope] (${bucket.length} 张, 实际不清理):`) + for (const h of bucket) { + log.info(` [skip] ${h.table}: ${h.count} 行 (本轮不清理)`) + } + continue + } log.info(` [${name}] (${bucket.length} 张):`) for (const h of bucket) { log.info(` [BAK+DEL] ${h.table}: ${h.count} 行 → \`${HISTORY_BAK_DB}\`.\`D${previewTs}-${h.table}\``)