Skip to content

Commit

Permalink
✨ Support use env as config
Browse files Browse the repository at this point in the history
  • Loading branch information
fuergaosi233 committed Dec 9, 2022
1 parent b6fb144 commit a4d0b23
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CHAT_GPT_EMAIL=
CHAT_GPT_PASSWORD=
CHAT_GPT_SESSION_TOKEN=
CHAT_GPT_RETRY_TIMES=
CHAT_PRIVATE_TRIGGER_KEYWORD=
28 changes: 21 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import * as dotenv from "dotenv";
dotenv.config();
import { parse } from "yaml";
import fs from "fs";
import { IConfig, IAccount } from "./interface";
const file = fs.readFileSync("./config.yaml", "utf8");
const configFile = parse(file);
// If config file exist read config file. else read config from environment variables.
let configFile: any = {};
if (fs.existsSync("./config.yaml")) {
const file = fs.readFileSync("./config.yaml", "utf8");
configFile = parse(file);
} else {
configFile = {
chatGPTAccountPool: [
{
email: process.env.CHAT_GPT_EMAIL,
password: process.env.CHAT_GPT_PASSWORD,
session_token: process.env.CHAT_GPT_SESSION_TOKEN,
},
],
chatGptRetryTimes: Number(process.env.CHAT_GPT_RETRY_TIMES),
chatPrivateTiggerKeyword: process.env.CHAT_PRIVATE_TRIGGER_KEYWORD,
};
}
dotenv.config();

export const config: IConfig = {
chatGPTAccountPool: configFile.chatGPTAccountPool as Array<IAccount>,
chatGptRetryTimes:
configFile.chatGptRetryTimes ||
Number(process.env.CHAT_GPT_RETRY_TIMES) ||
3,

chatGptRetryTimes: configFile.chatGptRetryTimes || 3,
chatPrivateTiggerKeyword:
configFile.chatPrivateTiggerKeyword ||
// Try compatible with previous designs
Expand Down
6 changes: 3 additions & 3 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export const isAccountWithUserInfo = (
account: IAccount
): account is AccountWithUserInfo => {
return (
(account as AccountWithUserInfo).email !== undefined &&
(account as AccountWithUserInfo).password !== undefined
!!(account as AccountWithUserInfo).email &&
!!(account as AccountWithUserInfo).password
);
};
export const isAccountWithSessionToken = (
account: IAccount
): account is AccountWithSessionToken => {
return (account as AccountWithSessionToken).session_token !== undefined;
return !!(account as AccountWithSessionToken).session_token;
};

// Account will be one in the session token or email and password
Expand Down

0 comments on commit a4d0b23

Please sign in to comment.