-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.mjs
More file actions
164 lines (148 loc) · 5.28 KB
/
build.mjs
File metadata and controls
164 lines (148 loc) · 5.28 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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 wrappers — POSIX shebang entry + Windows .cmd wrapper. Shipping
// both means install.sh/install.ps1 can place them side-by-side on any
// platform; the one that matches the shell wins.
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);
// Windows CMD wrapper — forwards all args to node + axme-code.js. %~dp0
// resolves to the directory of the .cmd at runtime so this works regardless
// of cwd or PATH entry style.
writeFileSync("dist/axme-code.cmd", "@echo off\r\nnode \"%~dp0axme-code.js\" %*\r\n");
// --- 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",
// @cursor/sdk is an optional dep with native binaries + workspace-internal
// sub-package imports (@anysphere/*) that esbuild cannot resolve from the
// plugin bundle — keep it external alongside @anthropic-ai/claude-agent-sdk.
// Plugin runtime: cursor_sdk path is unsupported in plugin distribution
// (Claude Code marketplace users use claude_agent_sdk anyway), so leaving
// the import as a dynamic external is safe — the await import() throws
// MODULE_NOT_FOUND inside the plugin and the AgentSdk factory falls back
// to the Claude wrapper, exactly as on win-arm64.
external: ["@anthropic-ai/claude-agent-sdk", "@cursor/sdk"],
outfile: "dist/plugin/cli.mjs",
sourcemap: true,
banner: { js: "" },
define,
});
// Plugin bin wrappers — POSIX bash script + Windows .cmd. Both forward to
// node + the plugin's bundled cli.mjs, located one directory up from bin/.
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);
writeFileSync("dist/plugin/bin/axme-code.cmd", "@echo off\r\nnode \"%~dp0..\\cli.mjs\" %*\r\n");
// 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. All commands quote the
// ${CLAUDE_PLUGIN_ROOT} expansion so paths with spaces survive sh -c and
// cmd.exe /c unchanged. The SessionStart hook used to shell out to `test -d
// ... || (cd ... && npm install)` which was POSIX-only; the lazy SDK
// install is now inside the `check-init` subcommand so this command is a
// plain Node invocation and works on Windows natively.
writeFileSync("dist/plugin/hooks/hooks.json", JSON.stringify({
description: "AXME Code safety enforcement and session tracking",
hooks: {
SessionStart: [{
hooks: [{
type: "command",
command: '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.");