Files
hose-use/src/cli/helpers.ts
T
gitea c1dc9f1616
🚀 Release / 🚀 Build & Publish Multi-Platform Binaries (push) Has been cancelled
feat: release v0.5.0 with embedded HTTP server and resident daemon service
2026-09-03 15:19:12 +08:00

79 lines
2.0 KiB
TypeScript

import readline from "readline";
import { loadCorpConfigs, loadGlobalConfig, getEnterprisesDirCandidates } from "../core/config/loader.js";
import { CorpContext, resolveCorpConfig } from "../core/config/manager.js";
import { CorpConfig } from "../core/config/types.js";
import { createEkbClient } from "../core/auth/ekb.js";
import { getEasoClient } from "../core/auth/easo.js";
import { CliRuntime, OutputOptions } from "./types.js";
export function initCorpRuntime(
options: {
corpId?: string;
stateless?: boolean;
stateful?: boolean;
jsonMode?: boolean;
outputOptions?: OutputOptions;
appRoot?: string;
entryFile?: string;
launcher?: {
command: string;
args?: string[];
};
} = {}
): CliRuntime {
const corps = loadCorpConfigs();
const globalConfig = loadGlobalConfig();
// 命令行参数 --stateless / --stateful 具有最高优先级
const isStateful = options.stateless
? false
: options.stateful
? true
: globalConfig.stateful;
const corpContext = new CorpContext(corps, {
stateful: isStateful,
defaultCorpId: globalConfig.defaultCorpId,
});
let selectedCorp: CorpConfig | null = null;
try {
selectedCorp = corpContext.resolve(options.corpId, getEnterprisesDirCandidates());
} catch {
selectedCorp = null;
}
return {
corpContext,
selectedCorp,
jsonMode: options.jsonMode ?? false,
outputOptions: options.outputOptions,
appRoot: options.appRoot,
entryFile: options.entryFile,
launcher: options.launcher,
};
}
export async function createClientsForCorp(corp: CorpConfig) {
const ekbClient = await createEkbClient(corp);
const { client: easoClient } = await getEasoClient(corp);
return {
corp,
ekbClient,
easoClient,
};
}
export function prompt(question: string): Promise<string> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer.trim());
});
});
}