mirror of
https://github.com/versia-pub/server.git
synced 2026-04-27 20:59:15 +02:00
refactor: 🚚 Organize code into sub-packages, instead of a single large package
This commit is contained in:
parent
79742f47dc
commit
a6d3ebbeef
366 changed files with 942 additions and 833 deletions
42
packages/config/index.ts
Normal file
42
packages/config/index.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { env, file } from "bun";
|
||||
import chalk from "chalk";
|
||||
import { parseTOML } from "confbox";
|
||||
import type { z } from "zod";
|
||||
import { fromZodError } from "zod-validation-error";
|
||||
import { ConfigSchema } from "./schema.ts";
|
||||
|
||||
const CONFIG_LOCATION = env.CONFIG_LOCATION ?? "./config/config.toml";
|
||||
const configFile = file(CONFIG_LOCATION);
|
||||
|
||||
if (!(await configFile.exists())) {
|
||||
throw new Error(
|
||||
`config file at "${CONFIG_LOCATION}" does not exist or is not accessible.`,
|
||||
);
|
||||
}
|
||||
|
||||
const configText = await configFile.text();
|
||||
const config = await parseTOML<z.infer<typeof ConfigSchema>>(configText);
|
||||
|
||||
const parsed = await ConfigSchema.safeParseAsync(config);
|
||||
|
||||
if (!parsed.success) {
|
||||
console.error(
|
||||
`⚠ Error encountered while loading ${chalk.gray(CONFIG_LOCATION)}.`,
|
||||
);
|
||||
console.error(
|
||||
"⚠ This is due to invalid, missing or incorrect values in the configuration file.",
|
||||
);
|
||||
console.error(
|
||||
"⚠ Here is the error message, please fix the configuration file accordingly:",
|
||||
);
|
||||
const errorMessage = fromZodError(parsed.error).message;
|
||||
|
||||
console.info(errorMessage);
|
||||
|
||||
throw new Error("Configuration file is invalid.");
|
||||
}
|
||||
|
||||
const exportedConfig = parsed.data;
|
||||
|
||||
export { ProxiableUrl } from "./url.ts";
|
||||
export { exportedConfig as config };
|
||||
Loading…
Add table
Add a link
Reference in a new issue