0
0
Fork 0

Initial commit

This commit is contained in:
Oliver 2022-12-23 15:19:37 -06:00 committed by GitHub
commit 56f2195962
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 1121 additions and 0 deletions

3
src/utils/cleanExit.ts Normal file
View file

@ -0,0 +1,3 @@
export function cleanExit() {
process.exit(0);
};

48
src/utils/config.ts Normal file
View file

@ -0,0 +1,48 @@
import { configSchema } from "$/schemas/config";
import type { config } from "$/types/config";
import { isDev } from "$/main";
import toml from "@iarna/toml";
import fs from "fs";
/**
* Attempts to load the config from disk and validate it's structure.
*/
export function loadConfig() {
let file = null;
if (isDev) {
try {
file = fs.readFileSync(`./config.dev.toml`, `utf-8`);
} catch (_) {
console.error(`Couldn't find development config, checking production`);
};
};
if (!file) {
try {
file = fs.readFileSync(`./config.toml`, `utf-8`);
} catch (_) {
console.error(`Couldn't find production config. Fill out the config and run the server again`);
process.exit(1);
};
};
try {
var data = toml.parse(file);
} catch (_) {
console.error(`Invalid TOML file, stopping server`);
process.exit(1);
};
let { error, value } = configSchema.validate(data, { abortEarly: false });
if (error) {
console.error(`Config failed to validate, see below for details:`)
for (const err of error.details) {
console.error(` - ${err.message}`);
};
process.exit(1);
};
return value as config;
};

View file

@ -0,0 +1,25 @@
import { databaseOptions } from "$/types/config";
import fs from "fs";
export class JSONDatabase {
private data = {};
private conf: databaseOptions;
constructor(conf: databaseOptions) {
this.conf = conf;
if (!fs.existsSync(conf.uri)) {
console.error(`Can't find database file, creating default`);
try {
fs.writeFileSync(conf.uri, `{}`);
} catch (_) {
console.log(`Couldn't create database file, ensure the uri is a valid filepath`);
};
};
this.data = JSON.parse(fs.readFileSync(conf.uri, `utf-8`));
};
public shutdown() {
fs.writeFileSync(this.conf.uri, JSON.stringify(this.data));
};
};