forked from Nomadcxx/opencode-cursor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-toggle.ts
More file actions
67 lines (54 loc) · 1.66 KB
/
plugin-toggle.ts
File metadata and controls
67 lines (54 loc) · 1.66 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
import { existsSync, readFileSync } from "fs";
import { homedir } from "os";
import { join, resolve } from "path";
const CURSOR_PROVIDER_ID = "cursor-acp";
type EnvLike = Record<string, string | undefined>;
export function resolveOpenCodeConfigPath(env: EnvLike = process.env): string {
if (env.OPENCODE_CONFIG && env.OPENCODE_CONFIG.length > 0) {
return resolve(env.OPENCODE_CONFIG);
}
const configHome = env.XDG_CONFIG_HOME && env.XDG_CONFIG_HOME.length > 0
? env.XDG_CONFIG_HOME
: join(homedir(), ".config");
return join(configHome, "opencode", "opencode.json");
}
export function isCursorPluginEnabledInConfig(config: unknown): boolean {
if (!config || typeof config !== "object") {
return true;
}
const configObject = config as { plugin?: unknown; provider?: unknown };
if (Array.isArray(configObject.plugin)) {
return configObject.plugin.some((entry) => entry === CURSOR_PROVIDER_ID);
}
return true;
}
export function shouldEnableCursorPlugin(env: EnvLike = process.env): {
enabled: boolean;
configPath: string;
reason: string;
} {
const configPath = resolveOpenCodeConfigPath(env);
if (!existsSync(configPath)) {
return {
enabled: true,
configPath,
reason: "config_missing",
};
}
try {
const raw = readFileSync(configPath, "utf8");
const parsed = JSON.parse(raw);
const enabled = isCursorPluginEnabledInConfig(parsed);
return {
enabled,
configPath,
reason: enabled ? "enabled_in_plugin_array_or_legacy" : "disabled_in_plugin_array",
};
} catch {
return {
enabled: true,
configPath,
reason: "config_unreadable_or_invalid",
};
}
}