UNPKG

@react-gnome/core

Version:

## Getting Started

39 lines (38 loc) 1.31 kB
// src/config/parse-config.ts import { assertDataType } from "dilswer"; import fs from "fs/promises"; import path from "path"; import { ConfigSchema } from "./config-schema.mjs"; import { evalJsConfigFile } from "./eval-js-config/eval-js-config.mjs"; async function parseJsonConfig(filePath) { const fileData = await fs.readFile(filePath, "utf-8"); const config = JSON.parse(fileData); assertDataType(ConfigSchema, config); return config; } async function parseJsConfig(filePath, context) { const getConfig = await evalJsConfigFile(filePath); const config = getConfig(context); assertDataType(ConfigSchema, config); return config; } function parseConfig(filePath, context) { const p = path.parse(filePath); if (p.ext === ".json") { return parseJsonConfig(filePath); } else if ([".ts", ".cts", ".mts"].includes(p.ext)) { if (process[Symbol.for("ts-node.register.instance")] == null) { throw new Error( "Invalid config file: To use TypeScript for the config file ts-node package must be installed." ); } return parseJsConfig(filePath, context); } else if ([".js", ".cjs", ".mjs"].includes(p.ext)) { return parseJsConfig(filePath, context); } else { throw new Error(`Unsupported config file type: '${p.ext}'.`); } } export { parseConfig };