import * as CBA from "./serivce"; import APIClient from "./common/http"; import { Logger } from "./common/logger"; import ConfigNotFoundException from "./common/exception/config-not-found-exception"; import { Config } from "./type"; import ConfigHelper, { CONFIG_UNIX_FILE_PATH } from "./common/config-helper"; const { program } = require("commander"); const logger = new Logger(); const fs = require("fs"); const os = require("os"); const inquirer = require("inquirer"); const user = os.userInfo(); export class ChatGPTCLI { cba: CBA.Service; logger: Logger; } const packageInfos = JSON.parse(fs.readFileSync("package.json", "utf8")); // register commands program.name("cba").description(packageInfos.description).version(packageInfos.version); program.command("config") .description("check or update your configurations file.") .option("-s, --show", `show configs in ${CONFIG_UNIX_FILE_PATH}`) .option("-u, --update", "update configs") .action((opts: any) => { try { if (opts.show) { const config = ConfigHelper.getConfig(); logger.info(JSON.stringify(config, undefined, 2)); } if (opts.update) { initConfig().then(r => { }); } } catch (e) { if (e instanceof ConfigNotFoundException) { logger.info(`No configuration found, please update the configuration first with the -u flag!`); } } }); export const initConfig = async () => { const questions = [ { type: "input", name: "username", message: `What's your odin username?(${user.username})`, default: user.username }, { type: "password", name: "password", message: "What's your odin password?" }, { type: "list", name: "env", message: "What's env of odin your will use?", choices: ["prod", "test"] }, { type: "list", name: "registLocalAsJobSlaver", message: "Do you want to use local machine as jenkins slaver?", choices: ["y", "n"] } ]; let config: Config = await inquirer.prompt(questions); await ConfigHelper.saveConfig(config); }; export const init = async (): Promise => { // read configurations let config: Config; try { config = ConfigHelper.getConfig(); } catch (e) { if (e instanceof ConfigNotFoundException) { await initConfig(); config = ConfigHelper.getConfig(); } } logger.warn(`your are now using chat-gpt cli [${config.env}] (${config.openaiAPIHost}), change using command 'cba config -u'`); const httpClient = new APIClient(config.openaiAPIHost); await httpClient.init(); logger.debug(`api client init success.`); const cba = await CBA.init(httpClient, program); const client: ChatGPTCLI = { cba, logger }; return client; }; init().then(() => { program.parse(); });