UNPKG

@withstudiocms/config-utils

Version:

Utilities for managing configuration files

59 lines (58 loc) 1.97 kB
import { defineUtility } from "astro-integration-kit"; import { loadConfigFile } from "./loader.js"; import { findConfig } from "./watcher.js"; import { parseAndMerge, parseConfig } from "./zod-utils.js"; const configResolverBuilder = ({ configPaths, label, zodSchema }) => defineUtility("astro:config:setup")( async ({ logger: l, config: { root: astroRoot } }, options) => { let inlineConfig = {}; const logger = l.fork(`${label}:config`); const inlineConfigExists = options !== void 0; if (zodSchema) { inlineConfig = parseConfig(zodSchema, options); } const loadedConfigFile = await loadConfigFile(astroRoot, configPaths, label); if (!loadedConfigFile) { logger.info("No config file found. Using inline config only."); return inlineConfig; } if (inlineConfigExists) { logger.warn( "Both an inline config and a config file were found. The config file will override the inline config during merging." ); } const mergedConfig = parseAndMerge(zodSchema, inlineConfig, loadedConfigFile); let logMessage = "Config file loaded and merged successfully."; if (inlineConfigExists) { logMessage += ` Warning: Inline config will be overridden by the config file, if you face any issues, try migrating your config to only use the ${label} config file.`; } logger.info(logMessage); return mergedConfig; } ); const watchConfigFileBuilder = ({ configPaths, _test_report }) => defineUtility("astro:config:setup")( ({ addWatchFile, config: { root: { pathname } } }) => { const configFileUrl = findConfig(pathname, configPaths); if (configFileUrl) { try { addWatchFile(configFileUrl); _test_report?.logs.push(configFileUrl); } catch (error) { _test_report?.errors.push(`Error watching config file: ${error}`); } } return; } ); export { configResolverBuilder, watchConfigFileBuilder };