-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.mjs
More file actions
143 lines (127 loc) · 3.87 KB
/
build.mjs
File metadata and controls
143 lines (127 loc) · 3.87 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { build } from "esbuild";
import { readFileSync } from "fs";
const pkg = JSON.parse(readFileSync("package.json", "utf-8"));
const define = { __VERSION__: JSON.stringify(pkg.version) };
// MCP server (main entry)
await build({
entryPoints: ["src/server.ts"],
bundle: true,
platform: "node",
target: "node20",
format: "esm",
packages: "external",
outfile: "dist/server.js",
sourcemap: true,
define,
});
// CLI entry (setup command)
await build({
entryPoints: ["src/cli.ts"],
bundle: true,
platform: "node",
target: "node20",
format: "esm",
packages: "external",
outfile: "dist/cli.mjs",
sourcemap: true,
banner: { js: "" },
define,
});
// Create bin wrapper
import { writeFileSync, chmodSync, mkdirSync } from "fs";
writeFileSync("dist/axme-code.js", '#!/usr/bin/env node\nimport("./cli.mjs");\n');
chmodSync("dist/axme-code.js", 0o755);
// --- Plugin bundled builds (self-contained, zero external deps) ---
await build({
entryPoints: ["src/server.ts"],
bundle: true,
platform: "node",
target: "node20",
format: "esm",
packages: "bundle",
outfile: "dist/plugin/server.mjs",
sourcemap: true,
define,
});
await build({
entryPoints: ["src/cli.ts"],
bundle: true,
platform: "node",
target: "node20",
format: "esm",
packages: "bundle",
external: ["@anthropic-ai/claude-agent-sdk"],
outfile: "dist/plugin/cli.mjs",
sourcemap: true,
banner: { js: "" },
define,
});
// Plugin bin wrapper — sets NODE_PATH so SDK can be found from CLAUDE_PLUGIN_DATA
mkdirSync("dist/plugin/bin", { recursive: true });
writeFileSync("dist/plugin/bin/axme-code", `#!/bin/bash
PLUGIN_DIR="\$(cd "\$(dirname "\$0")/.." && pwd)"
exec node "\$PLUGIN_DIR/cli.mjs" "\$@"
`);
chmodSync("dist/plugin/bin/axme-code", 0o755);
// Plugin package.json — only SDK for npm install in CLAUDE_PLUGIN_DATA
writeFileSync("dist/plugin/package.json", JSON.stringify({
name: "@axme/code-plugin",
private: true,
dependencies: {
"@anthropic-ai/claude-agent-sdk": pkg.dependencies["@anthropic-ai/claude-agent-sdk"],
},
}, null, 2) + "\n");
// --- Assemble plugin directory ---
import { cpSync, existsSync } from "fs";
mkdirSync("dist/plugin/.claude-plugin", { recursive: true });
mkdirSync("dist/plugin/hooks", { recursive: true });
// Plugin manifest
cpSync(".claude-plugin/plugin.json", "dist/plugin/.claude-plugin/plugin.json");
// Plugin .mcp.json — uses ${CLAUDE_PLUGIN_ROOT} for self-contained execution
writeFileSync("dist/plugin/.mcp.json", JSON.stringify({
mcpServers: {
axme: {
command: "node",
args: ["${CLAUDE_PLUGIN_ROOT}/server.mjs"],
},
},
}, null, 2) + "\n");
// Plugin hooks — safety enforcement via bundled CLI
writeFileSync("dist/plugin/hooks/hooks.json", JSON.stringify({
description: "AXME Code safety enforcement and session tracking",
hooks: {
SessionStart: [{
hooks: [{
type: "command",
command: "test -d ${CLAUDE_PLUGIN_ROOT}/node_modules/@anthropic-ai/claude-agent-sdk || (cd ${CLAUDE_PLUGIN_ROOT} && npm install --omit=dev --ignore-scripts 2>/dev/null) ; node ${CLAUDE_PLUGIN_ROOT}/cli.mjs check-init",
timeout: 30,
}],
}],
PreToolUse: [{
hooks: [{
type: "command",
command: "node ${CLAUDE_PLUGIN_ROOT}/cli.mjs hook pre-tool-use",
timeout: 5,
}],
}],
PostToolUse: [{
matcher: "Edit|Write|NotebookEdit",
hooks: [{
type: "command",
command: "node ${CLAUDE_PLUGIN_ROOT}/cli.mjs hook post-tool-use",
timeout: 10,
}],
}],
SessionEnd: [{
hooks: [{
type: "command",
command: "node ${CLAUDE_PLUGIN_ROOT}/cli.mjs hook session-end",
timeout: 120,
}],
}],
},
}, null, 2) + "\n");
// Copy LICENSE and README
if (existsSync("LICENSE")) cpSync("LICENSE", "dist/plugin/LICENSE");
cpSync("templates/plugin-README.md", "dist/plugin/README.md");
console.log("Build complete.");