Updates
This commit is contained in:
Vendored
+2
@@ -6,5 +6,7 @@ declare global {
|
||||
var CONFIG: BunMariaDBConfig;
|
||||
var DB_SCHEMA: BUN_MARIADB_DatabaseSchemaType;
|
||||
var MARIADB_CLIENT: Bun.SQL;
|
||||
var CONFIG_FILE_PATH: string;
|
||||
var SCHEMA_FILE_PATH: string;
|
||||
}
|
||||
export default function init(): void;
|
||||
|
||||
Vendored
+18
-13
@@ -4,19 +4,19 @@ import { AppData } from "../data/app-data";
|
||||
import grabDirNames from "../data/grab-dir-names";
|
||||
import { RequiredENVs, } from "../types";
|
||||
import setMariaDBClient from "./set-mariadb-client";
|
||||
import { resolveAndLoadDataFile, supportedDataFileNames, } from "../utils/resolve-and-load-data-file";
|
||||
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.`);
|
||||
const loadedConfig = resolveAndLoadDataFile(ROOT_DIR, ConfigFileName);
|
||||
if (!loadedConfig) {
|
||||
console.error(`Please create a ${supportedDataFileNames(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.`);
|
||||
const Config = loadedConfig.data;
|
||||
if (!Config || typeof Config !== "object") {
|
||||
console.error(`Invalid config in \`${loadedConfig.path}\`. Expected a config object${loadedConfig.format === "ts" || loadedConfig.format === "js" ? " (export default)" : ""}.`);
|
||||
process.exit(1);
|
||||
}
|
||||
if (!Config.db_name) {
|
||||
@@ -34,20 +34,23 @@ export default function init() {
|
||||
}
|
||||
});
|
||||
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`);
|
||||
console.error(`\`db_dir\` is required in your config. This directory holds all database related configuration. Also note that a ${supportedDataFileNames(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.`);
|
||||
const loadedSchema = resolveAndLoadDataFile(db_dir, AppData.DbSchemaFileName);
|
||||
if (!loadedSchema) {
|
||||
console.error(`Please create a schema file (${supportedDataFileNames(AppData.DbSchemaFileName)}) in \`${db_dir}\`${loadedConfig.format === "ts" || loadedConfig.format === "js" ? ". Don't forget to export a default module from .ts/.js files." : "."}`);
|
||||
process.exit(1);
|
||||
}
|
||||
const DbSchema = loadedSchema.data;
|
||||
if (!DbSchema || typeof DbSchema !== "object") {
|
||||
console.error(`Invalid schema in \`${loadedSchema.path}\`. Expected a schema object${loadedSchema.format === "ts" || loadedSchema.format === "js" ? " (export default)" : ""}.`);
|
||||
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)) {
|
||||
@@ -59,6 +62,8 @@ export default function init() {
|
||||
}
|
||||
global.CONFIG = Config;
|
||||
global.DB_SCHEMA = DbSchema;
|
||||
global.CONFIG_FILE_PATH = loadedConfig.path;
|
||||
global.SCHEMA_FILE_PATH = loadedSchema.path;
|
||||
if (!global.CONFIG) {
|
||||
console.error(`Couldn't grab global Config.`);
|
||||
process.exit(1);
|
||||
|
||||
Reference in New Issue
Block a user