2022-11-13 19:01:03 +09:00
|
|
|
import validator from 'validator';
|
2021-09-14 09:56:11 -07:00
|
|
|
|
2022-06-06 14:59:53 +07:00
|
|
|
import Logger from '../libs/Logger';
|
2021-09-14 09:56:11 -07:00
|
|
|
|
2023-04-23 13:13:20 +07:00
|
|
|
const requiredENV = [
|
|
|
|
|
'NODE_ENV',
|
|
|
|
|
'DATABASE_URL',
|
|
|
|
|
'DISCORD_TOKEN',
|
|
|
|
|
'PRIVATE_BOT',
|
|
|
|
|
'IMGPROXY_HOST',
|
|
|
|
|
'IMGPROXY_KEY',
|
|
|
|
|
'IMGPROXY_SALT'
|
|
|
|
|
];
|
2021-09-14 09:56:11 -07:00
|
|
|
|
|
|
|
|
class Environment {
|
|
|
|
|
public init(): void {
|
|
|
|
|
for (let param of requiredENV) {
|
2022-11-13 19:01:03 +09:00
|
|
|
if (this.isUndefinedOrEmpty(process.env[param])) throw new Error(`.env ${param} is undefined`);
|
2021-09-14 09:56:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NODE_ENV Checks
|
2022-06-06 14:59:53 +07:00
|
|
|
if (this.get().NODE_ENV != 'production' && this.get().NODE_ENV != 'development')
|
2021-09-14 09:56:11 -07:00
|
|
|
throw new Error('.env NODE_ENV must be either "production" or "development"');
|
|
|
|
|
|
2022-11-13 19:01:03 +09:00
|
|
|
// DISCORD_TOKEN Checks
|
|
|
|
|
if (this.get().DISCORD_TOKEN.length != 59)
|
|
|
|
|
throw new Error('.env DISCORD_TOKEN is not a valid discord bot token');
|
|
|
|
|
|
|
|
|
|
// DEVELOPER_IDS Checks
|
|
|
|
|
if (this.get().DEVELOPER_IDS) {
|
|
|
|
|
let developerIds = this.get().DEVELOPER_IDS.split(',');
|
|
|
|
|
for (let id of developerIds) {
|
|
|
|
|
if (isNaN(parseInt(id))) throw new Error(`.env DEVELOPER_IDS contains an invalid id: ${id}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PRIVATE_BOT Checks
|
|
|
|
|
if (this.get().PRIVATE_BOT.toLowerCase() != 'true' && this.get().PRIVATE_BOT.toLowerCase() != 'false')
|
|
|
|
|
throw new Error('.env PRIVATE_BOT must be either "true" or "false"');
|
2023-03-05 14:46:25 +07:00
|
|
|
|
2022-11-13 19:01:03 +09:00
|
|
|
// SUPPORT_URL Checks
|
|
|
|
|
if (this.get().SUPPORT_URL && !validator.isURL(this.get().SUPPORT_URL))
|
|
|
|
|
throw new Error('.env SUPPORT_URL is not a valid URL');
|
|
|
|
|
|
|
|
|
|
// WEB_HOST Checks
|
2023-03-05 14:46:25 +07:00
|
|
|
if (
|
|
|
|
|
this.get().WEB_HOST &&
|
|
|
|
|
this.get().WEB_HOST.toLowerCase() != 'localhost' &&
|
|
|
|
|
!validator.isIP(this.get().WEB_HOST)
|
|
|
|
|
)
|
2022-11-13 19:01:03 +09:00
|
|
|
throw new Error('.env WEB_HOST must be either localhost or a valid IP address');
|
|
|
|
|
|
|
|
|
|
// WEB_PORT Checks
|
|
|
|
|
if (this.get().WEB_PORT && !validator.isPort(this.get().WEB_PORT))
|
|
|
|
|
throw new Error('.env WEB_PORT must be a valid port number');
|
|
|
|
|
|
|
|
|
|
// YOUTUBE_COOKIE_BASE64 Checks
|
|
|
|
|
if (this.get().YOUTUBE_COOKIE_BASE64 && !validator.isBase64(this.get().YOUTUBE_COOKIE_BASE64))
|
|
|
|
|
throw new Error('.env YOUTUBE_COOKIE_BASE64 must be a valid base64 string');
|
2021-09-14 09:56:11 -07:00
|
|
|
|
|
|
|
|
Logger.log('info', `Running in ${process.env.NODE_ENV} environment`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get(): any {
|
|
|
|
|
const NODE_ENV = process.env.NODE_ENV;
|
2023-04-23 18:54:32 +07:00
|
|
|
const VERBOSE = process.env.VERBOSE;
|
2021-09-14 09:56:11 -07:00
|
|
|
|
|
|
|
|
const DISCORD_TOKEN = process.env.DISCORD_TOKEN;
|
2021-09-18 05:19:56 -07:00
|
|
|
const DEVELOPER_IDS = process.env.DEVELOPER_IDS;
|
|
|
|
|
const PRIVATE_BOT = process.env.PRIVATE_BOT;
|
2022-06-29 19:59:53 +07:00
|
|
|
const SUPPORT_URL = process.env.SUPPORT_URL;
|
2021-09-19 04:03:13 -07:00
|
|
|
|
2022-08-26 06:36:45 +07:00
|
|
|
const WEB_HOST = process.env.WEB_HOST;
|
|
|
|
|
const WEB_PORT = process.env.WEB_PORT;
|
|
|
|
|
|
2021-09-19 04:03:13 -07:00
|
|
|
const OSU_API_KEY = process.env.OSU_API_KEY;
|
2022-01-30 21:35:19 +07:00
|
|
|
const YOUTUBE_COOKIE_BASE64 = process.env.YOUTUBE_COOKIE_BASE64;
|
2022-06-06 14:59:53 +07:00
|
|
|
|
2022-07-01 20:59:35 +07:00
|
|
|
const SPOTIFY_CLIENT_ID = process.env.SPOTIFY_CLIENT_ID;
|
2023-03-05 14:46:25 +07:00
|
|
|
const SPOTIFY_CLIENT_SECRET = process.env.SPOTIFY_CLIENT_SECRET;
|
|
|
|
|
const SPOTIFY_REFRESH_TOKEN = process.env.SPOTIFY_REFRESH_TOKEN;
|
|
|
|
|
const SPOTIFY_CLIENT_MARKET = process.env.SPOTIFY_CLIENT_MARKET;
|
|
|
|
|
|
2023-04-23 13:13:20 +07:00
|
|
|
const VRC_CONTACT_EMAIL = process.env.VRC_CONTACT_EMAIL;
|
|
|
|
|
const VRC_API_KEY = process.env.VRC_API_KEY;
|
|
|
|
|
const VRC_COOKIE = process.env.VRC_COOKIE;
|
|
|
|
|
|
|
|
|
|
const IMGPROXY_HOST = process.env.IMGPROXY_HOST;
|
|
|
|
|
const IMGPROXY_KEY = process.env.IMGPROXY_KEY;
|
|
|
|
|
const IMGPROXY_SALT = process.env.IMGPROXY_SALT;
|
|
|
|
|
|
2021-09-14 09:56:11 -07:00
|
|
|
return {
|
|
|
|
|
NODE_ENV,
|
2023-04-23 18:54:32 +07:00
|
|
|
VERBOSE,
|
2021-09-14 09:56:11 -07:00
|
|
|
|
2021-09-18 05:19:56 -07:00
|
|
|
DISCORD_TOKEN,
|
|
|
|
|
DEVELOPER_IDS,
|
2021-09-19 04:03:13 -07:00
|
|
|
PRIVATE_BOT,
|
2022-06-29 19:59:53 +07:00
|
|
|
SUPPORT_URL,
|
2021-09-19 04:03:13 -07:00
|
|
|
|
2022-08-26 06:36:45 +07:00
|
|
|
WEB_HOST,
|
|
|
|
|
WEB_PORT,
|
|
|
|
|
|
2022-01-30 21:35:19 +07:00
|
|
|
OSU_API_KEY,
|
2022-07-01 20:59:35 +07:00
|
|
|
YOUTUBE_COOKIE_BASE64,
|
|
|
|
|
|
|
|
|
|
SPOTIFY_CLIENT_ID,
|
|
|
|
|
SPOTIFY_CLIENT_SECRET,
|
|
|
|
|
SPOTIFY_REFRESH_TOKEN,
|
2023-04-23 13:13:20 +07:00
|
|
|
SPOTIFY_CLIENT_MARKET,
|
|
|
|
|
|
|
|
|
|
VRC_CONTACT_EMAIL,
|
|
|
|
|
VRC_API_KEY,
|
|
|
|
|
VRC_COOKIE,
|
|
|
|
|
|
|
|
|
|
IMGPROXY_HOST,
|
|
|
|
|
IMGPROXY_KEY,
|
|
|
|
|
IMGPROXY_SALT
|
2021-09-14 09:56:11 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private isUndefinedOrEmpty(value: String | undefined): boolean {
|
2022-06-06 14:59:53 +07:00
|
|
|
if (typeof value === 'undefined') return true;
|
2021-09-14 09:56:11 -07:00
|
|
|
|
2022-06-06 14:59:53 +07:00
|
|
|
if (value === undefined) return true;
|
2021-09-14 09:56:11 -07:00
|
|
|
|
2022-06-06 14:59:53 +07:00
|
|
|
if (value === '') return true;
|
2021-09-14 09:56:11 -07:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default new Environment();
|