84 lines
2.2 KiB
TypeScript
84 lines
2.2 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 { getPiperClient } from "../core/auth/piper.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 async function createPiperClientForCorp(corp: CorpConfig, platformId: string) {
|
|
return getPiperClient(corp, platformId);
|
|
}
|
|
|
|
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());
|
|
});
|
|
});
|
|
}
|