import { resolve } from "node:path"; import { loadConfig, setupDotenv } from "c12"; import { getLastGitTag, getCurrentGitRef } from "./git"; import { resolveRepoConfig, RepoProvider } from "./repo"; import type { SemverBumpType } from "./semver"; import type { RepoConfig } from "./repo"; export interface ChangelogConfig { cwd: string; types: Record; scopeMap: Record; repo?: RepoConfig; tokens: Partial>; from: string; to: string; newVersion?: string; output: string | boolean; templates: { commitMessage?: string; tagMessage?: string; tagBody?: string; }; } const getDefaultConfig = () => { types: { feat: { title: "🚀 Enhancements", semver: "minor" }, perf: { title: "🔥 Performance", semver: "patch" }, fix: { title: "🩹 Fixes", semver: "patch" }, refactor: { title: "💅 Refactors", semver: "patch" }, docs: { title: "📖 Documentation", semver: "patch" }, build: { title: "📦 Build", semver: "patch" }, types: { title: "🌊 Types", semver: "patch" }, chore: { title: "🏡 Chore" }, examples: { title: "🏀 Examples" }, test: { title: "✅ Tests" }, style: { title: "🎨 Styles" }, other: { title: "🤷‍♂️ Other" }, ci: { title: "🤖 CI" }, }, cwd: null, from: "", to: "", output: "CHANGELOG.md", scopeMap: {}, tokens: { github: process.env.CHANGELOGEN_TOKENS_GITHUB || process.env.GITHUB_TOKEN || process.env.GH_TOKEN, }, templates: { commitMessage: "chore(release): {{newVersion}}", tagMessage: "{{newVersion}}", tagBody: "{{newVersion}}", }, }; export async function loadChangelogConfig( cwd: string, overrides?: Partial, ): Promise { await setupDotenv({ cwd }); const defaults = getDefaultConfig(); const { config } = await loadConfig({ cwd, name: "changelog", packageJson: true, defaults, overrides: { cwd, ...(overrides as ChangelogConfig), }, }); if (!config.from) { config.from = await getLastGitTag(); } if (!config.to) { config.to = await getCurrentGitRef(); } if (!config.output) { config.output = false; } else if (config.output) { config.output = config.output === true ? defaults.output : resolve(cwd, config.output); } if (!config.repo) { config.repo = await resolveRepoConfig(cwd); } return config; }