43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
// @ts-check
|
|
import path from "node:path";
|
|
import fs from "node:fs";
|
|
import Ajv from "ajv";
|
|
|
|
const ajv = new Ajv();
|
|
|
|
/**
|
|
* @returns {import("../../schema/config.ts").IConfiguration}
|
|
*/
|
|
export function parseConfiguration() {
|
|
const configSchemaPath = path.resolve(
|
|
__dirname,
|
|
"..",
|
|
"..",
|
|
"schema",
|
|
"config.schema.json"
|
|
);
|
|
const configPath = path.resolve(__dirname, "..", "..", "config.json");
|
|
const configFileSchemaContent = fs.readFileSync(configSchemaPath, {
|
|
encoding: "utf8",
|
|
});
|
|
const configFileContent = fs.readFileSync(configPath, { encoding: "utf8" });
|
|
const configSchema = JSON.parse(configFileSchemaContent);
|
|
/**
|
|
* @type {import("../../schema/config.ts").IConfiguration}
|
|
*/
|
|
const config = JSON.parse(configFileContent);
|
|
|
|
const isValid = ajv.validate(configSchema, config);
|
|
|
|
if (!isValid) {
|
|
/**
|
|
* @type {import("ajv").ErrorObject<string, Record<string, any>, unknown>[]}
|
|
*/
|
|
// @ts-expect-error
|
|
const errors = ajv.errors
|
|
throw new AggregateError(errors, "Failed to validate the config.")
|
|
}
|
|
|
|
return config;
|
|
}
|