import { existsSync } from "fs"; import grabDirNames from "../utils/grab-dir-names"; import exitWithError from "../utils/exit-with-error"; export default async function grabConfig() { try { const { CONFIG_FILE } = grabDirNames(); if (!existsSync(CONFIG_FILE)) { exitWithError(`Config file \`${CONFIG_FILE}\` doesn't exist!`); } const config = (await import(CONFIG_FILE)).default; if (!config) { exitWithError(`Config file \`${CONFIG_FILE}\` is invalid! Please provide a valid default export in your config file.`); } return config; } catch (error) { // A config that fails to load (syntax error, bad export) must not be // silently replaced by defaults — the app would run with the wrong // configuration and nothing would explain why. const { CONFIG_FILE } = grabDirNames(); exitWithError(`Failed to load config file \`${CONFIG_FILE}\`: ${error?.message || error}`); } }