diff --git a/bun.lockb b/bun.lockb index 2a6ec54..b05414f 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 9951350..7bb9d03 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,6 @@ "@stacksjs/development": "^0.67.0", "@stacksjs/eslint-config": "^3.8.1-beta.2", "@types/bun": "^1.1.11", - "c12": "^2.0.1", "tinyglobby": "^0.2.9", "vitepress": "^1.4.1" }, diff --git a/src/config.ts b/src/config.ts index 2ad3a89..c184568 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,11 +1,38 @@ import type { DtsGenerationConfig } from './types' +import { existsSync } from 'node:fs' +import { resolve } from 'node:path' import process from 'node:process' -import { loadConfig } from 'c12' +import { deepMerge } from './utils' + +interface Options { + name: string + cwd?: string + defaultConfig: T +} + +export async function loadConfig>({ name, cwd, defaultConfig }: Options): Promise { + const c = cwd ?? process.cwd() + const configPath = resolve(c, `${name}.config`) + + if (existsSync(configPath)) { + try { + const importedConfig = await import(configPath) + const loadedConfig = importedConfig.default || importedConfig + return deepMerge(defaultConfig, loadedConfig) + } + catch (error) { + console.error(`Error loading config from ${configPath}:`, error) + } + } + + return defaultConfig +} // Get loaded config // eslint-disable-next-line antfu/no-top-level-await -export const config: DtsGenerationConfig = (await loadConfig({ +export const config: DtsGenerationConfig = await loadConfig({ name: 'dts', + cwd: process.cwd(), defaultConfig: { cwd: process.cwd(), root: './src', @@ -15,4 +42,4 @@ export const config: DtsGenerationConfig = (await loadConfig({ clean: true, tsconfigPath: './tsconfig.json', }, -})).config +}) diff --git a/src/utils.ts b/src/utils.ts index c18a6e4..774171c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -86,3 +86,30 @@ export function formatComment(comment: string): string { }) .join('\n') } + +export function deepMerge(target: T, ...sources: Array>): T { + if (!sources.length) + return target + + const source = sources.shift() + + if (isObject(target) && isObject(source)) { + for (const key in source) { + if (Object.prototype.hasOwnProperty.call(source, key)) { + const sourceValue = source[key] + if (isObject(sourceValue) && isObject(target[key])) { + target[key] = deepMerge(target[key] as any, sourceValue as any) + } + else { + (target as any)[key] = sourceValue + } + } + } + } + + return deepMerge(target, ...sources) +} + +function isObject(item: unknown): item is Record { + return (item && typeof item === 'object' && !Array.isArray(item)) as boolean +}