Skip to content

Commit ceef101

Browse files
committed
chore: wip
1 parent 099cb8f commit ceef101

7 files changed

+75
-113
lines changed

build.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { dts } from 'bun-plugin-dtsx'
33
await Bun.build({
44
entrypoints: ['src/index.ts'],
55
outdir: './dist',
6+
target: 'bun',
7+
plugins: [dts()],
8+
})
9+
10+
await Bun.build({
11+
entrypoints: ['src/browser.ts'],
12+
outdir: './dist',
613
target: 'browser',
7-
external: ['node:process'],
814
plugins: [dts()],
915
})

bun.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"@types/bun": "^1.2.3",
1111
"bumpp": "^10.0.3",
1212
"bun-plugin-dtsx": "^0.21.9",
13-
"bunfig": "^0.7.0",
13+
"bunfig": "^0.8.0",
1414
"changelogen": "^0.5.7",
1515
"lint-staged": "^15.4.3",
1616
"simple-git-hooks": "^2.11.1",
@@ -687,7 +687,7 @@
687687

688688
"bundle-name": ["[email protected]", "", { "dependencies": { "run-applescript": "^7.0.0" } }, "sha512-tjwM5exMg6BGRI+kNmTntNsvdZS1X8BFYS6tnJ2hdH0kVxM6/eVZ2xy+FqStSWvYmtfFMDLIxurorHwDKfDz5Q=="],
689689

690-
"bunfig": ["bunfig@0.7.0", "", {}, "sha512-q9F/r97pSepONQOnvvtU3WZ3kt+bhZYLbQsI2JnhqQVGuv3M/rP661QuK7fCDVmaGM5BOnd1H9qCBa4l2Y1fnA=="],
690+
"bunfig": ["bunfig@0.8.0", "", { "bin": { "bunfig": "bin/cli.js" } }, "sha512-MFOLWu/PD5WcM8L09dnicwNtii+5rW5oMD5rEPOdbuSVikYGAgzmnMKzgEJpkIdhszDHS/EpQBZAJ7VQAXUwKg=="],
691691

692692
"c12": ["[email protected]", "", { "dependencies": { "chokidar": "^4.0.1", "confbox": "^0.1.7", "defu": "^6.1.4", "dotenv": "^16.4.5", "giget": "^1.2.3", "jiti": "^2.3.0", "mlly": "^1.7.1", "ohash": "^1.1.4", "pathe": "^1.1.2", "perfect-debounce": "^1.0.0", "pkg-types": "^1.2.0", "rc9": "^2.1.2" }, "peerDependencies": { "magicast": "^0.3.5" }, "optionalPeers": ["magicast"] }, "sha512-Z4JgsKXHG37C6PYUtIxCfLJZvo6FyhHJoClwwb9ftUkLpPSkuYqn6Tr+vnaN8hymm0kIbcg6Ey3kv/Q71k5w/A=="],
693693

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"@types/bun": "^1.2.3",
6868
"bumpp": "^10.0.3",
6969
"bun-plugin-dtsx": "^0.21.9",
70-
"bunfig": "^0.7.0",
70+
"bunfig": "^0.8.0",
7171
"changelogen": "^0.5.7",
7272
"lint-staged": "^15.4.3",
7373
"simple-git-hooks": "^2.11.1",

src/browser.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const wip = true

src/config.ts

+44-18
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import type { ClarityConfig } from './types'
2-
import { loadConfig } from 'bunfig'
2+
import { homedir } from 'node:os'
3+
import { join } from 'node:path'
4+
import process from 'node:process'
5+
import { loadConfig as bunfigLoadConfig } from 'bunfig'
36

4-
const defaultRotationConfig: ClarityConfig['rotation'] = {
5-
frequency: 'daily',
6-
maxSize: 10 * 1024 * 1024,
7-
maxFiles: 5,
8-
compress: false,
9-
rotateHour: 0,
10-
rotateMinute: 0,
11-
rotateDayOfWeek: 0,
12-
rotateDayOfMonth: 1,
13-
encrypt: false,
14-
}
7+
// Default log directory is in user's home directory
8+
const defaultLogDirectory = process.env.CLARITY_LOG_DIR || join(homedir(), '.clarity', 'logs')
159

1610
export const defaultConfig: ClarityConfig = {
1711
level: 'info',
@@ -21,14 +15,46 @@ export const defaultConfig: ClarityConfig = {
2115
format: 'text',
2216
maxLogSize: 10 * 1024 * 1024,
2317
logDatePattern: 'YYYY-MM-DD',
24-
logDirectory: '',
25-
rotation: defaultRotationConfig,
18+
logDirectory: defaultLogDirectory,
19+
rotation: {
20+
frequency: 'daily',
21+
maxSize: 10 * 1024 * 1024,
22+
maxFiles: 5,
23+
compress: false,
24+
rotateHour: 0,
25+
rotateMinute: 0,
26+
rotateDayOfWeek: 0,
27+
rotateDayOfMonth: 1,
28+
encrypt: false,
29+
},
2630
verbose: false,
2731
}
2832

33+
// Wrapper around bunfig's loadConfig that handles verbose mode
34+
async function loadConfig() {
35+
const isVerbose = process.env.CLARITY_VERBOSE === 'true' || defaultConfig.verbose
36+
37+
// Temporarily override console.error if not in verbose mode
38+
const originalError = console.error
39+
if (!isVerbose) {
40+
console.error = () => {}
41+
}
42+
43+
try {
44+
return await bunfigLoadConfig({
45+
name: 'clarity',
46+
defaultConfig,
47+
cwd: process.cwd(),
48+
endpoint: '',
49+
headers: {},
50+
})
51+
}
52+
finally {
53+
// Always restore console.error
54+
console.error = originalError
55+
}
56+
}
57+
2958
// @ts-expect-error there is a current dtsx issue
3059
// eslint-disable-next-line antfu/no-top-level-await
31-
export const config: ClarityConfig = await loadConfig({
32-
name: 'clarity',
33-
defaultConfig,
34-
})
60+
export const config: ClarityConfig = await loadConfig()

src/logger.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,31 @@ export class Logger {
8383
delete configOptions.timestamp // Remove from config to avoid type error
8484
}
8585

86+
// Merge with default config
8687
this.config = {
8788
...defaultConfig,
8889
...configOptions,
8990
timestamp: hasTimestamp || defaultConfig.timestamp,
9091
}
9192

93+
// Ensure log directory exists
94+
if (!this.config.logDirectory) {
95+
this.config.logDirectory = defaultConfig.logDirectory
96+
}
97+
98+
// Suppress config loading message if not in verbose mode
99+
if (!this.config.verbose && !process.env.CLARITY_VERBOSE) {
100+
const originalConsoleError = console.error
101+
console.error = () => {}
102+
try {
103+
// Force config loading here to suppress messages
104+
void loadConfig({ name: 'clarity', defaultConfig })
105+
}
106+
finally {
107+
console.error = originalConsoleError
108+
}
109+
}
110+
92111
this.timers = new Map()
93112
this.subLoggers = new Map()
94113
this.formatter = this.config.format === 'json'
@@ -738,7 +757,7 @@ export class Logger {
738757
}
739758
}
740759

741-
private async log(level: LogLevel, message: string, ...args: any[]): Promise<void> {
760+
async log(level: LogLevel, message: string, ...args: any[]): Promise<void> {
742761
if (!this.shouldLog(level)) {
743762
return
744763
}

test-formatter.ts

-90
This file was deleted.

0 commit comments

Comments
 (0)