forked from ezppfarm/tazer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglob.ts
executable file
·56 lines (50 loc) · 1.82 KB
/
glob.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import Database from './app/usecases/database';
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';
import * as logger from './app/log/logger';
import {domainValidator} from './app/utils/validatorUtils';
const REQUIRED_ENV_KEYS = ['HTTP_PORT', 'MYSQL_HOST', 'MYSQL_DB'];
let _database: Database;
export function loadEnv() {
const envFile = path.join(process.cwd(), '.env');
if (!fs.existsSync(envFile)) return undefined;
const missingEnvKeys: string[] = [];
dotenv.config();
for (const envKey of REQUIRED_ENV_KEYS) {
if (!getEnv(envKey)) missingEnvKeys.push(envKey);
}
return missingEnvKeys;
}
export function getEnv(key: string, defaultValue?: string): string | undefined {
return process.env[key] ? (process.env[key] as string) : defaultValue;
}
export async function database(dbInit?: Database) {
if (!_database) {
if (!dbInit) {
throw Error('Database is not initialized.');
}
logger.info('Connecting to database...');
_database = dbInit;
const connected = await _database.connect();
if (!connected) throw Error('Failed to connect to database!');
logger.success('Database connection success!');
}
return _database.get();
}
export const getDataFolder = async (dataFolder: 'avatars' | 'osu') => {
const dataRoot = path.join(process.cwd(), '.data');
if (!fs.existsSync(dataRoot)) await fs.promises.mkdir(dataRoot);
const dataPath = path.join(dataRoot, dataFolder);
if (!fs.existsSync(dataPath)) await fs.promises.mkdir(dataPath);
return dataPath;
};
export const getDomain = (sub?: 'avatar') => {
const domain = getEnv(sub ? `${sub.toUpperCase() + '_DOMAIN'}` : 'DOMAIN');
if (!domain || !domainValidator.parse(domain)) {
throw Error(
`${sub ? `${sub.toUpperCase() + '_DOMAIN'}` : 'DOMAIN'} in .env not set!`
);
}
return domain;
};