import { getEnterprisesDirCandidates, loadCorpConfigs, loadGlobalConfig, resolveGlobalConfigFile, saveGlobalConfig } from "../../core/config/loader.js"; import { resolveCorpConfig } from "../../core/config/manager.js"; import { globalConfigSummary } from "../helpers.js"; import { parseStateMode, stringOption } from "../parser.js"; import { printJson } from "../printer.js"; import type { ParsedArgs } from "../types.js"; export function handleConfigCommand(action: string | undefined, maybeName: string | undefined, args: ParsedArgs) { if (action === "paths") { printJson({ candidates: getEnterprisesDirCandidates(), globalConfigFile: resolveGlobalConfigFile() }); return; } if (action === "get" || action === "status") { printJson(globalConfigSummary()); return; } if (action === "mode") { const mode = maybeName ?? stringOption(args.options.mode); if (!mode) { printJson(globalConfigSummary()); return; } const stateful = parseStateMode(mode); const requestedCorpId = stringOption(args.options.corp); const previousConfig = loadGlobalConfig(); const defaultCorpId = stateful ? requestedCorpId ? resolveCorpConfig( loadCorpConfigs(), { stateful: true, corpId: requestedCorpId }, getEnterprisesDirCandidates() ).id : previousConfig.defaultCorpId : undefined; const config = saveGlobalConfig({ ...previousConfig, stateful, ...(defaultCorpId ? { defaultCorpId } : {}) }); printJson(globalConfigSummary(config)); return; } if (action === "lint") { const subAction = maybeName ?? stringOption(args.options.state) ?? stringOption(args.options.enable); const previousConfig = loadGlobalConfig(); const currentLint = previousConfig.lint ?? { enabled: false, activeLinter: "builtin", minScore: 0 }; if (!subAction || subAction === "status" || subAction === "get") { printJson({ lint: currentLint, configFile: resolveGlobalConfigFile() }); return; } let enabled = currentLint.enabled; if (subAction === "enable" || subAction === "on" || subAction === "true") { enabled = true; } else if (subAction === "disable" || subAction === "off" || subAction === "false") { enabled = false; } const activeLinter = stringOption(args.options.linter) ?? currentLint.activeLinter; const minScore = args.options.minScore !== undefined || args.options["min-score"] !== undefined ? Number(args.options.minScore ?? args.options["min-score"]) : currentLint.minScore; const nextLint = { enabled, activeLinter, minScore: isNaN(minScore) ? 0 : Math.max(0, Math.min(100, minScore)) }; const config = saveGlobalConfig({ ...previousConfig, lint: nextLint }); printJson(globalConfigSummary(config)); return; } if (action === "test-dir" || action === "test-enterprises-dir") { const isClear = args.options.clear === true || maybeName === "--clear" || maybeName === "clear"; const previousConfig = loadGlobalConfig(); if (isClear) { const config = saveGlobalConfig({ ...previousConfig, testEnterprisesDir: undefined }); printJson(globalConfigSummary(config)); return; } const newPath = maybeName && !maybeName.startsWith("-") ? maybeName : stringOption(args.options.path) ?? stringOption(args.options.dir); if (!newPath) { printJson({ testEnterprisesDir: previousConfig.testEnterprisesDir ?? null, configFile: resolveGlobalConfigFile() }); return; } const config = saveGlobalConfig({ ...previousConfig, testEnterprisesDir: newPath }); printJson(globalConfigSummary(config)); return; } throw new Error(`Unknown config command: ${action ?? ""}. Available: paths, get, status, mode, lint, test-dir. Run 'seal-use help' for available commands.`); }