import { resolveEntity } from "../../../domains/platform/datalink/services.js"; import { resolvePlatform } from "../../../domains/platform/applications/services.js"; import { createClientsForCorp } from "../../helpers.js"; import type { CliParsedArgs, CliRuntime } from "../../types.js"; export function requiredTarget(parsed: CliParsedArgs, index: number, usage: string): string { const target = parsed.args[index]?.trim(); if (!target) throw new Error(`请提供目标参数: ${usage}`); return target; } export function splitFields(value?: string): string[] { return value ? value.split(",").map((field) => field.trim()).filter(Boolean) : []; } export function cell(value: unknown): string { if (value === undefined || value === null) return ""; if (typeof value === "object") return JSON.stringify(value); return String(value); } export async function resolveAppAndEntity(parsed: CliParsedArgs, runtime: CliRuntime, appIndex = 0, entityIndex = 1) { const appTarget = requiredTarget(parsed, appIndex, "hose-use platform <子命令> <应用 ID|名称>"); const clients = await createClientsForCorp(runtime.selectedCorp!); const corpId = runtime.selectedCorp!.credentials.corpId; const platform = await resolvePlatform(clients.ekbClient, corpId, appTarget); if (!platform) throw new Error(`[NOT FOUND] 当前企业下未找到自建应用: ${appTarget}`); const entityTarget = parsed.args[entityIndex]?.trim(); if (!entityTarget) return { clients, corpId, platform, entity: undefined }; const entity = await resolveEntity(clients.ekbClient, corpId, platform, entityTarget); if (!entity) throw new Error(`[NOT FOUND] 应用 [${platform.name}] 下未找到业务对象: ${entityTarget}`); return { clients, corpId, platform, entity }; }