86 lines
3.7 KiB
TypeScript
86 lines
3.7 KiB
TypeScript
import { homedir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { VERSION } from "../../version.js";
|
|
import { runServiceCommand, ServiceConfig, getPidFile, getLogFile } from "../service.js";
|
|
import { printJson, printKeyValue } from "../printer.js";
|
|
import { numberOption, stringOption } from "../parser.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
|
import { createEkbClient } from "../../core/auth/ekb.js";
|
|
import { EventStore } from "../../domains/events/store.js";
|
|
import { pollEvents } from "../../domains/events/poller.js";
|
|
|
|
export const SERVICE_STATE_DIR = process.env.HOSE_STATE_DIR || join(homedir(), ".config", "hose-use");
|
|
|
|
export async function handleServiceCommand(
|
|
parsed: CliParsedArgs,
|
|
runtime: CliRuntime
|
|
): Promise<CliResult> {
|
|
const action = parsed.action || parsed.args[0] || "status";
|
|
const port = numberOption(parsed.options, ["port", "p"]);
|
|
const host = stringOption(parsed.options, ["host", "h"]);
|
|
|
|
if (action === "poll") {
|
|
const corp = runtime.selectedCorp;
|
|
if (!corp) throw new Error("事件轮询需要企业上下文,请通过 --corp=<id> 指定企业。");
|
|
const corpId = corp.corpId || corp.id;
|
|
const client = await createEkbClient(corp);
|
|
const limit = numberOption(parsed.options, ["limit", "count", "n"], 50) || 50;
|
|
const store = new EventStore(join(SERVICE_STATE_DIR, `events-${corpId}.json`));
|
|
const result = await pollEvents(client, corpId, store, limit);
|
|
if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
return { stdout: `====== 事件轮询 (企业: ${corp.name}) ======\n${printKeyValue({ "待办扫描": result.scanned.todo, "未读消息扫描": result.scanned.messages, "新增事件": result.newEvents.length, "重复事件": result.duplicateCount, "事件状态文件": join(SERVICE_STATE_DIR, `events-${corpId}.json`) })}\n`, stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
const config: ServiceConfig = {
|
|
appRoot: runtime.appRoot || process.cwd(),
|
|
entryFile: runtime.entryFile || join(runtime.appRoot || process.cwd(), "src", "cli.ts"),
|
|
stateDir: SERVICE_STATE_DIR,
|
|
version: VERSION,
|
|
launcher: runtime.launcher,
|
|
port,
|
|
host,
|
|
};
|
|
|
|
const result = await runServiceCommand(action, config);
|
|
|
|
if (result === undefined) {
|
|
// 'run' keeps running, resolves on shutdown
|
|
return { stdout: "", stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
if (runtime.jsonMode) {
|
|
return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
|
|
}
|
|
|
|
if (action === "logs" || action === "log") {
|
|
const logsResult = result as { logFile: string; lines: string[]; content: string };
|
|
return {
|
|
stdout: `====== hose-use 守护进程日志 (${logsResult.logFile}) ======\n${logsResult.content}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const status = result as Record<string, unknown>;
|
|
const isRunning = Boolean(status.running);
|
|
|
|
const displayPairs: Record<string, unknown> = {
|
|
"服务状态": isRunning ? "🟢 正在运行 (Running)" : "⚪ 已停止 (Stopped)",
|
|
...(status.pid ? { "进程 PID": status.pid } : {}),
|
|
...(status.url ? { "服务地址": status.url } : {}),
|
|
...(status.url ? { "交互式文档": `${status.url}/docs` } : {}),
|
|
...(status.url ? { "CLI 网关": `POST ${status.url}/api/cli` } : {}),
|
|
"运行版本": status.version || config.version,
|
|
"状态目录": status.stateDir || config.stateDir,
|
|
"PID 文件": status.pidFile || getPidFile(config),
|
|
"日志文件": status.logFile || getLogFile(config),
|
|
};
|
|
|
|
const formatted = printKeyValue(displayPairs);
|
|
return {
|
|
stdout: `====== hose-use 常驻服务管理 (Service) ======\n${formatted}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|