-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostinstall.mjs
More file actions
61 lines (54 loc) · 1.92 KB
/
Copy pathpostinstall.mjs
File metadata and controls
61 lines (54 loc) · 1.92 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
#!/usr/bin/env node
// postinstall.mjs — copies bundled agents/, commands/, skills/ markdown into the
// user's global opencode config directory (~/.config/opencode/). Idempotent: never
// overwrites existing files so user edits are preserved. Mirrors the plannotator
// postinstall pattern for opencode plugin distribution.
//
// Run automatically on `npm install` / `bun install` of this package. Pass --quiet
// to suppress the summary line (used by the package.json postinstall script).
import path from "node:path";
import fs from "node:fs";
import os from "node:os";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const quiet = process.argv.includes("--quiet");
const home = os.homedir();
const globalConfigDir =
process.env.XDG_CONFIG_HOME
? path.join(process.env.XDG_CONFIG_HOME, "opencode")
: path.join(home, ".config", "opencode");
// Respect the opencode custom config directory if set.
const target = process.env.OPENCODE_CONFIG_DIR
? process.env.OPENCODE_CONFIG_DIR
: globalConfigDir;
const categories = ["agents", "commands", "skills", "scripts"];
const srcRoot = __dirname;
let copied = 0;
let skipped = 0;
function copyRecursive(src, dst) {
const stat = fs.statSync(src);
if (stat.isDirectory()) {
fs.mkdirSync(dst, { recursive: true });
for (const entry of fs.readdirSync(src)) {
copyRecursive(path.join(src, entry), path.join(dst, entry));
}
} else {
if (fs.existsSync(dst)) {
skipped++;
return;
}
fs.mkdirSync(path.dirname(dst), { recursive: true });
fs.copyFileSync(src, dst);
copied++;
}
}
for (const cat of categories) {
const srcDir = path.join(srcRoot, cat);
if (!fs.existsSync(srcDir)) continue;
copyRecursive(srcDir, path.join(target, cat));
}
if (!quiet) {
process.stdout.write(
`[opencode-plugins] installed ${copied} file(s) into ${target} (${skipped} already present, skipped).\n`
);
}