Skip to content

Commit b96a974

Browse files
committed
chore: add verbose on loadConfig
1 parent a7ad074 commit b96a974

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/config.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import type { Config } from './types'
22
import { existsSync, mkdirSync, readdirSync, writeFileSync } from 'node:fs'
33
import { dirname, resolve } from 'node:path'
44
import process from 'node:process'
5+
import { Logger } from '@stacksjs/clarity'
56
import { deepMerge } from './utils'
67

8+
const log = new Logger('bunfig', {
9+
showTags: true,
10+
})
11+
712
type ConfigNames = string
813

914
export async function config<T>(
@@ -71,11 +76,16 @@ export async function loadConfig<T>({
7176
name = '',
7277
cwd,
7378
defaultConfig,
79+
verbose = false,
7480
}: Config<T>): Promise<T> {
7581
// Server environment: load the config from the file system
7682
const baseDir = cwd || process.cwd()
7783
const extensions = ['.ts', '.js', '.mjs', '.cjs', '.json']
7884

85+
if (verbose) {
86+
log.info(`Loading configuration for "${name}" from ${baseDir}`)
87+
}
88+
7989
// Try loading config in order of preference
8090
const configPaths = [
8191
`${name}.config`,
@@ -89,6 +99,9 @@ export async function loadConfig<T>({
8999
const fullPath = resolve(baseDir, `${configPath}${ext}`)
90100
const config = await tryLoadConfig(fullPath, defaultConfig)
91101
if (config !== null) {
102+
if (verbose) {
103+
log.success(`Configuration loaded from: ${configPath}${ext}`)
104+
}
92105
return config
93106
}
94107
}
@@ -103,18 +116,30 @@ export async function loadConfig<T>({
103116

104117
if (pkgConfig && typeof pkgConfig === 'object' && !Array.isArray(pkgConfig)) {
105118
try {
119+
if (verbose) {
120+
log.success(`Configuration loaded from package.json: ${name}`)
121+
}
106122
return deepMerge(defaultConfig, pkgConfig) as T
107123
}
108-
catch {
124+
catch (error) {
125+
if (verbose) {
126+
log.warn(`Failed to merge package.json config for ${name}:`, error)
127+
}
109128
// If merging fails, continue to default config
110129
}
111130
}
112131
}
113132
}
114-
catch {
133+
catch (error) {
134+
if (verbose) {
135+
log.warn(`Failed to load package.json for ${name}:`, error)
136+
}
115137
// If package.json loading fails, continue to default config
116138
}
117139

140+
if (verbose) {
141+
log.info(`No configuration found for ${name}, using default configuration`)
142+
}
118143
return defaultConfig
119144
}
120145

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export interface Config<T> {
2323
endpoint?: string
2424
headers?: Record<string, string>
2525
defaultConfig: T
26+
verbose?: boolean
2627
}
2728

2829
export type SimplifyDeep<T> = T extends object

0 commit comments

Comments
 (0)