UNPKG

@paroicms/internal-server-lib

Version:

Common utilitaries for the paroicms server.

27 lines 1.13 kB
import { messageOf } from "@paroicms/public-anywhere-lib"; import { readFileSync } from "node:fs"; import { isAbsolute, join } from "node:path"; import { cwd } from "node:process"; export function readConfigFileSync({ jsonTypeValidator, typeName, defaultFileName, }) { const paramIndex = process.argv.indexOf("--config"); const hasParam = paramIndex !== -1 && paramIndex + 1 < process.argv.length; let confFile = hasParam ? process.argv[paramIndex + 1] : defaultFileName; if (!confFile) throw new Error("Missing configuration file parameter"); if (!isAbsolute(confFile)) { confFile = join(cwd(), confFile); } let data; try { const content = readFileSync(confFile, "utf8"); data = JSON.parse(content); } catch (err) { throw new Error(`Cannot load the configuration file '${confFile}': ${messageOf(err)}`); } const result = jsonTypeValidator.validate(typeName, data); if (!result.valid) throw new Error(`Invalid config file: ${result.error ?? "(missing message)"}`); return data; } //# sourceMappingURL=read-configuration.js.map