Repository navigation
Expand file tree
/
Copy pathconfig.js
More file actions
87 lines (72 loc) · 2.63 KB
/
Copy pathconfig.js
File metadata and controls
87 lines (72 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { existsSync } from "node:fs"
import * as fs from "node:fs/promises"
import os from "node:os"
import path from "node:path"
function parseDurationMs(input) {
const raw = String(input ?? "").trim()
if (!raw) return 60 * 60 * 1000
const m = raw.match(/^(\d+(?:\.\d+)?)(ms|s|m|h|d)$/)
if (!m) return 60 * 60 * 1000
const value = Number(m[1])
const unit = m[2]
if (!Number.isFinite(value) || value < 0) return 60 * 60 * 1000
if (unit === "ms") return value
if (unit === "s") return value * 1000
if (unit === "m") return value * 60 * 1000
if (unit === "h") return value * 60 * 60 * 1000
if (unit === "d") return value * 24 * 60 * 60 * 1000
return 60 * 60 * 1000
}
function readJson(filepath) {
return fs
.readFile(filepath, "utf8")
.then((s) => JSON.parse(s))
.catch(() => null)
}
function normalizeConfig(raw) {
const cfg = raw && typeof raw === "object" ? raw : {}
const enabled = Boolean(cfg.enabled)
const debug = Boolean(cfg.debug)
const prefix = typeof cfg.placeholder_prefix === "string" && cfg.placeholder_prefix ? cfg.placeholder_prefix : "__VG_"
const session = cfg.session && typeof cfg.session === "object" ? cfg.session : {}
const ttlMs = parseDurationMs(session.ttl ?? "1h")
const maxMappings =
Number.isFinite(session.max_mappings) && Number(session.max_mappings) > 0 ? Number(session.max_mappings) : 100000
const patterns = cfg.patterns && typeof cfg.patterns === "object" ? cfg.patterns : {}
return {
enabled,
debug,
prefix,
ttlMs,
maxMappings,
patterns,
}
}
export function getConfigCandidates(directory) {
const dir = String(directory ?? process.cwd())
const home = os.homedir()
const globalConfig = path.join(home, ".config", "opencode", "vibeguard.config.json")
const projectRoot = path.join(dir, "vibeguard.config.json")
const projectLocal = path.join(dir, ".opencode", "vibeguard.config.json")
const env = process.env.OPENCODE_VIBEGUARD_CONFIG
if (env) return [path.resolve(dir, env), projectRoot, projectLocal, globalConfig]
return [projectRoot, projectLocal, globalConfig]
}
/**
* Load plugin config:
* - Missing or unparsable config: enabled=false (plugin is a no-op)
* - Light validation only, no extra dependencies
*/
export async function loadConfig(directory) {
const candidates = getConfigCandidates(directory)
for (const file of candidates) {
if (!file) continue
if (!existsSync(file)) continue
const raw = await readJson(file)
if (!raw) continue
const cfg = normalizeConfig(raw)
return { ...cfg, loadedFrom: file }
}
const cfg = normalizeConfig({ enabled: false })
return { ...cfg, loadedFrom: "" }
}