Files
hose-use/src/cli/helpers.ts
T

122 lines
3.3 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 {
createSessionClient,
getEasoClient,
getUnityClient,
getScriptEngineClient,
} from "../core/http/index.js";
import { CliRuntime, OutputOptions } from "./types.js";
export function initCorpRuntime(
options: {
corpId?: string;
stateless?: boolean;
stateful?: boolean;
jsonMode?: boolean;
dryRun?: boolean;
confirmed?: 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,
dryRun: options.dryRun ?? false,
confirmed: options.confirmed ?? false,
outputOptions: options.outputOptions,
appRoot: options.appRoot,
entryFile: options.entryFile,
launcher: options.launcher,
};
}
export async function createClientsForCorp(corp: CorpConfig) {
const sessionClient = await createSessionClient(corp);
let easoClient: Awaited<ReturnType<typeof getEasoClient>>["client"];
let unityClient: Awaited<ReturnType<typeof getUnityClient>>["client"];
try {
const easo = await getEasoClient(corp);
easoClient = easo.client;
unityClient = easo.client;
} catch {
const unity = await getUnityClient(corp);
unityClient = unity.client;
easoClient = unity.client;
}
return {
corp,
sessionClient,
ekbClient: sessionClient,
unityClient,
easoClient,
};
}
export async function createScriptEngineClientForCorp(corp: CorpConfig, platformId: string) {
return getScriptEngineClient(corp, platformId);
}
export const createPiperClientForCorp = createScriptEngineClientForCorp;
export function requireSelectedCorp(
runtime: CliRuntime,
message = "未指定或未找到有效企业。请通过 --corp=<id> 指定企业,或运行 hose-use auth login 登录。"
): CorpConfig {
if (!runtime.selectedCorp) {
throw new Error(message);
}
return runtime.selectedCorp;
}
export async function getSessionClientForRuntime(runtime: CliRuntime) {
const corp = requireSelectedCorp(runtime);
const corpId = corp.credentials?.corpId || corp.corpId || corp.id;
const client = await createSessionClient(corp);
return { corp, corpId, client };
}
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());
});
});
}