36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import { join, resolve } from "node:path";
|
|
import type { MuddleConfig, ResolvedConfig } from "../contracts/index.ts";
|
|
|
|
/** Applies the defaults of a project configuration. */
|
|
export function resolveConfig(
|
|
config: MuddleConfig = {},
|
|
root: string = process.cwd(),
|
|
): ResolvedConfig {
|
|
const resolvedRoot = resolve(config.root ?? root);
|
|
const views = config.views ?? "views";
|
|
return {
|
|
...config,
|
|
root: resolvedRoot,
|
|
views: resolve(resolvedRoot, views),
|
|
components: resolve(
|
|
resolvedRoot,
|
|
config.components ?? join(views, "components"),
|
|
),
|
|
content: resolve(resolvedRoot, config.content ?? "content"),
|
|
styles: resolve(resolvedRoot, config.styles ?? "styles"),
|
|
publicDir: resolve(resolvedRoot, config.publicDir ?? "public"),
|
|
viewRoots: (config.viewRoots ?? [views]).map((entry) =>
|
|
resolve(resolvedRoot, entry),
|
|
),
|
|
loaders: config.loaders ?? {},
|
|
collections: config.collections ?? [],
|
|
integrations: config.integrations ?? [],
|
|
};
|
|
}
|
|
|
|
/** Reads the configuration of a project from its `ct.config.ts`. */
|
|
export async function loadConfig(root: string): Promise<MuddleConfig> {
|
|
const configPath = join(root, "ct.config.ts");
|
|
const module = (await import(configPath)) as { default?: MuddleConfig };
|
|
return module.default ?? {};
|
|
}
|