-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
32 lines (31 loc) · 1.58 KB
/
Copy pathindex.ts
File metadata and controls
32 lines (31 loc) · 1.58 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
import { Plugin } from "@opencode/plugin";
import { readFile, realpath } from "node:fs/promises";
import { join } from "node:path";
import github from "./plugins/github.js";
import scheduler from "./plugins/scheduler.js";
import { registerConfigured } from "./repositories.js";
import { checkout, resolveEasy } from "./easy.js";
export default Plugin.define({
id: "automation",
async setup(ctx) {
let location;
try { location = await checkout(ctx.location.directory); }
catch { return; } // A global installation remains inactive outside Git repositories.
if (!location.primary || location.root !== await realpath(ctx.location.directory)) return;
let options: unknown = ctx.options;
if (!Object.keys(ctx.options).length) {
try { options = JSON.parse(await readFile(join(location.root, ".opencode", "automation.json"), "utf8")); }
catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return; throw error; }
}
await registerConfigured(location.root, Object.keys(ctx.options).length ? options : undefined)
.catch(() => console.error("Repository registration failed. Use list --discover to retry standard configurations."));
const resolved = await resolveEasy(location.root, options);
const stopGithub = await github.setup({ ...ctx, options: resolved.github });
try {
const stopScheduler = await scheduler.setup({ ...ctx, options: resolved.scheduler });
return async () => {
try { await stopScheduler?.(); } finally { await stopGithub?.(); }
};
} catch (error) { await stopGithub?.(); throw error; }
},
});