Skip to content

Commit e9d6a1b

Browse files
committed
chore: remove c12 dep
1 parent 6c74f4d commit e9d6a1b

4 files changed

Lines changed: 57 additions & 4 deletions

File tree

bun.lockb

-10.5 KB
Binary file not shown.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
"@stacksjs/development": "^0.67.0",
6767
"@stacksjs/eslint-config": "^3.8.1-beta.2",
6868
"@types/bun": "^1.1.11",
69-
"c12": "^2.0.1",
7069
"tinyglobby": "^0.2.9",
7170
"vitepress": "^1.4.1"
7271
},

src/config.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
import type { DtsGenerationConfig } from './types'
2+
import { existsSync } from 'node:fs'
3+
import { resolve } from 'node:path'
24
import process from 'node:process'
3-
import { loadConfig } from 'c12'
5+
import { deepMerge } from './utils'
6+
7+
interface Options<T> {
8+
name: string
9+
cwd?: string
10+
defaultConfig: T
11+
}
12+
13+
export async function loadConfig<T extends Record<string, unknown>>({ name, cwd, defaultConfig }: Options<T>): Promise<T> {
14+
const c = cwd ?? process.cwd()
15+
const configPath = resolve(c, `${name}.config`)
16+
17+
if (existsSync(configPath)) {
18+
try {
19+
const importedConfig = await import(configPath)
20+
const loadedConfig = importedConfig.default || importedConfig
21+
return deepMerge(defaultConfig, loadedConfig)
22+
}
23+
catch (error) {
24+
console.error(`Error loading config from ${configPath}:`, error)
25+
}
26+
}
27+
28+
return defaultConfig
29+
}
430

531
// Get loaded config
632
// eslint-disable-next-line antfu/no-top-level-await
7-
export const config: DtsGenerationConfig = (await loadConfig({
33+
export const config: DtsGenerationConfig = await loadConfig({
834
name: 'dts',
35+
cwd: process.cwd(),
936
defaultConfig: {
1037
cwd: process.cwd(),
1138
root: './src',
@@ -15,4 +42,4 @@ export const config: DtsGenerationConfig = (await loadConfig({
1542
clean: true,
1643
tsconfigPath: './tsconfig.json',
1744
},
18-
})).config
45+
})

src/utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,30 @@ export function formatComment(comment: string): string {
8686
})
8787
.join('\n')
8888
}
89+
90+
export function deepMerge<T extends object>(target: T, ...sources: Array<Partial<T>>): T {
91+
if (!sources.length)
92+
return target
93+
94+
const source = sources.shift()
95+
96+
if (isObject(target) && isObject(source)) {
97+
for (const key in source) {
98+
if (Object.prototype.hasOwnProperty.call(source, key)) {
99+
const sourceValue = source[key]
100+
if (isObject(sourceValue) && isObject(target[key])) {
101+
target[key] = deepMerge(target[key] as any, sourceValue as any)
102+
}
103+
else {
104+
(target as any)[key] = sourceValue
105+
}
106+
}
107+
}
108+
}
109+
110+
return deepMerge(target, ...sources)
111+
}
112+
113+
function isObject(item: unknown): item is Record<string, unknown> {
114+
return (item && typeof item === 'object' && !Array.isArray(item)) as boolean
115+
}

0 commit comments

Comments
 (0)