oc-tweaks 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ declare const Bun: any
2
+
3
+ import { mkdir } from "node:fs/promises"
4
+ import { dirname } from "node:path"
5
+ export interface LoggerConfig {
6
+ enabled?: boolean
7
+ maxLines?: number
8
+ }
9
+
10
+ const DEFAULT_MAX_LINES = 100
11
+
12
+ function getHome(): string {
13
+ return Bun.env?.HOME ?? ((globalThis as any)?.process?.env?.HOME ?? "") ?? ""
14
+ }
15
+
16
+ function getLogFilePath(): string {
17
+ return `${getHome()}/.config/opencode/plugins/oc-tweaks.log`
18
+ }
19
+
20
+ export async function log(
21
+ config: LoggerConfig | undefined,
22
+ level: string,
23
+ message: string
24
+ ): Promise<void> {
25
+ if (!config?.enabled) return
26
+
27
+ try {
28
+ const logFile = getLogFilePath()
29
+ const file = Bun.file(logFile)
30
+ let content = ""
31
+
32
+ if (await file.exists()) {
33
+ content = await file.text()
34
+ }
35
+
36
+ const line = `[${new Date().toISOString()}] [${level}] ${message}\n`
37
+ content += line
38
+
39
+ const maxLines = config.maxLines ?? DEFAULT_MAX_LINES
40
+ const keepLines = Math.floor(maxLines / 2)
41
+ const lines = content.split("\n").filter(Boolean)
42
+
43
+ if (lines.length > maxLines) {
44
+ content = lines.slice(-keepLines).join("\n") + "\n"
45
+ }
46
+
47
+ await mkdir(dirname(logFile), { recursive: true })
48
+ await Bun.write(logFile, content)
49
+ } catch {
50
+ // Never disrupt user workflow
51
+ }
52
+ }
@@ -0,0 +1,16 @@
1
+ import { log } from "./logger"
2
+ import type { LoggerConfig } from "./logger"
3
+
4
+ export function safeHook<T extends (...args: any[]) => any>(
5
+ name: string,
6
+ fn: T,
7
+ loggerConfig?: LoggerConfig
8
+ ): T {
9
+ return (async (...args: any[]) => {
10
+ try {
11
+ return await fn(...args)
12
+ } catch (err) {
13
+ await log(loggerConfig, "ERROR", `[oc-tweaks] ${name}: ${err}`)
14
+ }
15
+ }) as unknown as T
16
+ }