81 lines
3.3 KiB
JavaScript
81 lines
3.3 KiB
JavaScript
import path from "path";
|
|
import fs from "fs";
|
|
import { AppData } from "../data/app-data";
|
|
import grabDirNames from "../data/grab-dir-names";
|
|
import { RequiredENVs, } from "../types";
|
|
import setMariaDBClient from "./set-mariadb-client";
|
|
export default function init() {
|
|
try {
|
|
const { ROOT_DIR } = grabDirNames();
|
|
const { ConfigFileName } = AppData;
|
|
const ConfigFilePath = path.join(ROOT_DIR, ConfigFileName);
|
|
if (!fs.existsSync(ConfigFilePath)) {
|
|
console.error(`Please create a \`${ConfigFileName}\` file at the root of your project.`);
|
|
process.exit(1);
|
|
}
|
|
const ConfigImport = require(ConfigFilePath);
|
|
const Config = ConfigImport["default"];
|
|
if (!Config) {
|
|
console.error(`No default export from \`${ConfigFilePath}\`. Please export a default module.`);
|
|
process.exit(1);
|
|
}
|
|
if (!Config.db_name) {
|
|
console.error(`\`db_name\` is required in your config`);
|
|
process.exit(1);
|
|
}
|
|
RequiredENVs.forEach((env_key) => {
|
|
if (env_key == "BUN_MARIADB_SERVER_PORT" ||
|
|
env_key == "BUN_MARIADB_SERVER_SSL_KEY_PATH") {
|
|
return;
|
|
}
|
|
if (!process.env[env_key]) {
|
|
console.error(`\`${env_key}\` env variable is required.`);
|
|
process.exit(1);
|
|
}
|
|
});
|
|
if (!Config.db_dir) {
|
|
console.error(`\`db_dir\` is required in your config. This directory holds all database related configuration. Also note that a \`${AppData["DbSchemaFileName"]}\` file is also required in this directory to define your database schema`);
|
|
process.exit(1);
|
|
}
|
|
const db_dir = path.resolve(ROOT_DIR, Config.db_dir);
|
|
if (!fs.existsSync(db_dir)) {
|
|
fs.mkdirSync(db_dir, { recursive: true });
|
|
}
|
|
const DBSchemaFilePath = path.join(db_dir, AppData["DbSchemaFileName"]);
|
|
if (!fs.existsSync(DBSchemaFilePath)) {
|
|
console.error(`Please create a schema file at \`${DBSchemaFilePath}\`. Don't forget to export a default module from this file.`);
|
|
process.exit(1);
|
|
}
|
|
const DbSchemaImport = require(DBSchemaFilePath);
|
|
const DbSchema = DbSchemaImport["default"];
|
|
const backup_dir = Config.db_backup_dir || AppData["DefaultBackupDirName"];
|
|
const BackupDir = path.resolve(db_dir, backup_dir);
|
|
if (!fs.existsSync(BackupDir)) {
|
|
fs.mkdirSync(BackupDir, { recursive: true });
|
|
}
|
|
const ExportDir = path.resolve(db_dir, AppData["DefaultExportDirName"]);
|
|
if (!fs.existsSync(ExportDir)) {
|
|
fs.mkdirSync(ExportDir, { recursive: true });
|
|
}
|
|
global.CONFIG = Config;
|
|
global.DB_SCHEMA = DbSchema;
|
|
if (!global.CONFIG) {
|
|
console.error(`Couldn't grab global Config.`);
|
|
process.exit(1);
|
|
}
|
|
if (!global.DB_SCHEMA) {
|
|
console.error(`Couldn't grab Database Schema.`);
|
|
process.exit(1);
|
|
}
|
|
setMariaDBClient({ config: Config });
|
|
if (!global.MARIADB_CLIENT) {
|
|
console.error(`Couldn't set MariaDB Client.`);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.error(`Initialization ERROR => ` + error.message);
|
|
process.exit(1);
|
|
}
|
|
}
|