-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.ts
More file actions
71 lines (60 loc) · 2.2 KB
/
plugin.ts
File metadata and controls
71 lines (60 loc) · 2.2 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
import type { Plugin } from "@opencode-ai/plugin";
import { mkdir, readdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
const VERSION: string = JSON.parse(
await Bun.file(`${import.meta.dirname}/package.json`).text()
).version;
async function copyDir(src: string, dest: string): Promise<void> {
await mkdir(dest, { recursive: true });
for (const entry of await readdir(src, { withFileTypes: true })) {
const s = `${src}/${entry.name}`;
const d = `${dest}/${entry.name}`;
entry.isDirectory() ? await copyDir(s, d) : await Bun.write(d, Bun.file(s));
}
}
async function ensureSkillPermission(directory: string): Promise<void> {
const configPath = join(directory, ".opencode", "opencode.json");
let config: Record<string, unknown>;
try {
const content = await readFile(configPath, "utf-8");
config = JSON.parse(content);
} catch {
config = {};
}
if (!config.permission) {
config.permission = {};
}
if (!(config.permission as Record<string, unknown>).skill) {
(config.permission as Record<string, unknown>).skill = {};
}
const skillPerms = (config.permission as Record<string, unknown>).skill as Record<string, unknown>;
if (skillPerms.intellisearch !== "allow") {
skillPerms.intellisearch = "allow";
await mkdir(join(directory, ".opencode"), { recursive: true });
await writeFile(configPath, JSON.stringify(config, null, 2));
}
}
const plugin: Plugin = async ({ directory }) => ({
config: async () => {
const targetDir = `${directory}/.opencode`;
const marker = `${targetDir}/skills/intellisearch/.version`;
try {
if ((await Bun.file(marker).text()).trim() === VERSION) return;
} catch {
// not installed
}
const pkgDir = import.meta.dirname;
await copyDir(
`${pkgDir}/assets/skills/intellisearch`,
`${targetDir}/skills/intellisearch`,
);
await mkdir(`${targetDir}/commands`, { recursive: true });
await Bun.write(
`${targetDir}/commands/search-intelligently.md`,
Bun.file(`${pkgDir}/assets/commands/search-intelligently.md`),
);
await Bun.write(marker, VERSION);
await ensureSkillPermission(directory);
},
});
export default plugin;