@minecraftbot-com/plugin 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +177 -0
- package/README.md +45 -2
- package/dist/api.d.ts +351 -0
- package/dist/api.js +2 -0
- package/dist/cli/api.d.ts +26 -0
- package/dist/cli/api.js +61 -0
- package/dist/cli/build.d.ts +23 -0
- package/dist/cli/build.js +65 -0
- package/dist/cli/check.d.ts +10 -0
- package/dist/cli/check.js +98 -0
- package/dist/cli/config.d.ts +11 -0
- package/dist/cli/config.js +38 -0
- package/dist/cli/create.d.ts +2 -0
- package/dist/cli/create.js +39 -0
- package/dist/cli/dev-mcp.d.ts +36 -0
- package/dist/cli/dev-mcp.js +101 -0
- package/dist/cli/dev.d.ts +15 -0
- package/dist/cli/dev.js +261 -0
- package/dist/cli/fetch-guard.d.ts +7 -0
- package/dist/cli/fetch-guard.js +66 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +112 -0
- package/dist/cli/mcp.d.ts +24 -0
- package/dist/cli/mcp.js +136 -0
- package/dist/cli/output.d.ts +11 -0
- package/dist/cli/output.js +25 -0
- package/dist/cli/test.d.ts +2 -0
- package/dist/cli/test.js +39 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.js +35 -0
- package/dist/generated/block-states.d.ts +4087 -0
- package/dist/generated/block-states.js +2 -0
- package/dist/generated/blocks.d.ts +1289 -0
- package/dist/generated/blocks.js +1291 -0
- package/dist/generated/contracts.d.ts +304 -0
- package/dist/generated/contracts.js +327 -0
- package/dist/generated/effects.d.ts +43 -0
- package/dist/generated/effects.js +45 -0
- package/dist/generated/entities.d.ts +164 -0
- package/dist/generated/entities.js +166 -0
- package/dist/generated/items.d.ts +1661 -0
- package/dist/generated/items.js +1663 -0
- package/dist/generated/operations.d.ts +7 -0
- package/dist/generated/operations.js +65 -0
- package/dist/generated/permission-labels.d.ts +3 -0
- package/dist/generated/permission-labels.js +17 -0
- package/dist/generated/version.d.ts +2 -0
- package/dist/generated/version.js +3 -0
- package/dist/host/index.d.ts +7 -0
- package/dist/host/index.js +5 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +13 -0
- package/dist/permissions.d.ts +11 -0
- package/dist/permissions.js +18 -0
- package/dist/plugin.d.ts +86 -0
- package/dist/plugin.js +26 -0
- package/dist/protocol.d.ts +167 -0
- package/dist/protocol.js +4 -0
- package/dist/runtime/connection.d.ts +52 -0
- package/dist/runtime/connection.js +132 -0
- package/dist/runtime/handles.d.ts +95 -0
- package/dist/runtime/handles.js +289 -0
- package/dist/runtime/memory-transport.d.ts +6 -0
- package/dist/runtime/memory-transport.js +39 -0
- package/dist/runtime/runtime.d.ts +57 -0
- package/dist/runtime/runtime.js +265 -0
- package/dist/runtime/scope.d.ts +25 -0
- package/dist/runtime/scope.js +94 -0
- package/dist/runtime/storage-runtime.d.ts +10 -0
- package/dist/runtime/storage-runtime.js +131 -0
- package/dist/settings.d.ts +111 -0
- package/dist/settings.js +78 -0
- package/dist/storage.d.ts +78 -0
- package/dist/storage.js +24 -0
- package/dist/testing/fake-container.d.ts +175 -0
- package/dist/testing/fake-container.js +483 -0
- package/dist/testing/index.d.ts +37 -0
- package/dist/testing/index.js +54 -0
- package/dist/types.d.ts +65 -0
- package/dist/types.js +54 -0
- package/llms.txt +11 -0
- package/package.json +55 -4
- package/src/api.ts +334 -0
- package/src/cli/api.ts +79 -0
- package/src/cli/build.ts +76 -0
- package/src/cli/check.ts +103 -0
- package/src/cli/config.ts +48 -0
- package/src/cli/create.ts +38 -0
- package/src/cli/dev-mcp.ts +131 -0
- package/src/cli/dev.ts +272 -0
- package/src/cli/fetch-guard.ts +61 -0
- package/src/cli/main.ts +113 -0
- package/src/cli/mcp.ts +168 -0
- package/src/cli/output.ts +30 -0
- package/src/cli/test.ts +38 -0
- package/src/errors.ts +34 -0
- package/src/generated/block-states.ts +892 -0
- package/src/generated/blocks.ts +1291 -0
- package/src/generated/contracts.ts +329 -0
- package/src/generated/effects.ts +45 -0
- package/src/generated/entities.ts +166 -0
- package/src/generated/items.ts +1663 -0
- package/src/generated/operations.ts +69 -0
- package/src/generated/permission-labels.ts +19 -0
- package/src/generated/version.ts +3 -0
- package/src/host/index.ts +9 -0
- package/src/index.ts +34 -0
- package/src/permissions.ts +23 -0
- package/src/plugin.ts +105 -0
- package/src/protocol.ts +107 -0
- package/src/runtime/connection.ts +167 -0
- package/src/runtime/handles.ts +337 -0
- package/src/runtime/memory-transport.ts +41 -0
- package/src/runtime/runtime.ts +281 -0
- package/src/runtime/scope.ts +101 -0
- package/src/runtime/storage-runtime.ts +138 -0
- package/src/settings.ts +118 -0
- package/src/storage.ts +82 -0
- package/src/testing/fake-container.ts +571 -0
- package/src/testing/index.ts +70 -0
- package/src/types.ts +88 -0
- package/templates/basic/AGENTS.md +31 -0
- package/templates/basic/README.md +13 -0
- package/templates/basic/gitignore +3 -0
- package/templates/basic/package.json +18 -0
- package/templates/basic/src/index.ts +20 -0
- package/templates/basic/test/plugin.test.ts +14 -0
- package/templates/basic/tsconfig.json +14 -0
package/src/cli/main.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { pollDevice, startDevice } from "./api.js";
|
|
3
|
+
import { build, check } from "./check.js";
|
|
4
|
+
import { apiUrl, deleteCredentials, readCredentials, saveCredentials } from "./config.js";
|
|
5
|
+
import { create } from "./create.js";
|
|
6
|
+
import { dev } from "./dev.js";
|
|
7
|
+
import { bold, cyan, dim, fail, green, info } from "./output.js";
|
|
8
|
+
import { test } from "./test.js";
|
|
9
|
+
import { SDK_VERSION } from "../runtime/runtime.js";
|
|
10
|
+
|
|
11
|
+
const HELP = `minebot ${SDK_VERSION}: write and run MineBot plugins
|
|
12
|
+
|
|
13
|
+
minebot create <folder> new plugin project
|
|
14
|
+
minebot login sign in (approve on the dashboard)
|
|
15
|
+
minebot logout forget the sign-in on this machine
|
|
16
|
+
minebot dev [options] run the plugin on your running profile, restarting on changes
|
|
17
|
+
--profile <name|id> which profile (default: your only running one)
|
|
18
|
+
--bot <name> run on this bot only (repeat for more; default: every bot)
|
|
19
|
+
--settings <file.json> setting values to use
|
|
20
|
+
--once don't watch for changes
|
|
21
|
+
--mcp-port <port> port of the MCP endpoint for AI agents (default 7788)
|
|
22
|
+
--no-mcp no MCP endpoint
|
|
23
|
+
minebot test run test/*.test.ts against a fake bot
|
|
24
|
+
minebot check what users will be asked to allow, bundle size, problems
|
|
25
|
+
minebot build write dist/plugin.js and dist/manifest.json
|
|
26
|
+
|
|
27
|
+
Environment: MINEBOT_API_URL overrides the API (default ${apiUrl()}).`;
|
|
28
|
+
|
|
29
|
+
function option(args: string[], name: string): string | undefined {
|
|
30
|
+
const i = args.indexOf(name);
|
|
31
|
+
if (i < 0) return undefined;
|
|
32
|
+
const value = args[i + 1];
|
|
33
|
+
if (value === undefined || value.startsWith("--")) fail(`${name} needs a value.`);
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function options(args: string[], name: string): string[] {
|
|
38
|
+
const values: string[] = [];
|
|
39
|
+
args.forEach((a, i) => {
|
|
40
|
+
if (a === name && args[i + 1] && !args[i + 1].startsWith("--")) values.push(args[i + 1]);
|
|
41
|
+
});
|
|
42
|
+
return values;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function port(value: string): number {
|
|
46
|
+
const n = Number(value);
|
|
47
|
+
if (!Number.isInteger(n) || n < 1 || n > 65535) fail(`${value} isn't a port.`);
|
|
48
|
+
return n;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function login(): Promise<void> {
|
|
52
|
+
const device = await startDevice();
|
|
53
|
+
info(`Open ${cyan(device.verificationUrl)}`);
|
|
54
|
+
info(`and approve the code ${bold(device.userCode)}\n`);
|
|
55
|
+
const deadline = Date.now() + device.expiresIn * 1000;
|
|
56
|
+
while (Date.now() < deadline) {
|
|
57
|
+
await new Promise(r => setTimeout(r, device.interval * 1000));
|
|
58
|
+
const result = await pollDevice(device.deviceCode);
|
|
59
|
+
if (result.status === "approved" && result.token) {
|
|
60
|
+
const file = saveCredentials({ apiUrl: apiUrl(), token: result.token });
|
|
61
|
+
info(`${green("Signed in.")} ${dim(`(saved to ${file})`)}`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (result.status === "denied") fail("The sign-in was denied on the dashboard.");
|
|
65
|
+
if (result.status === "expired") break;
|
|
66
|
+
}
|
|
67
|
+
fail("The code expired.", "Run `minebot login` again.");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function main(argv: string[]): Promise<void> {
|
|
71
|
+
const [command, ...args] = argv;
|
|
72
|
+
switch (command) {
|
|
73
|
+
case "create":
|
|
74
|
+
return create(args[0], option(args, "--template"));
|
|
75
|
+
case "login":
|
|
76
|
+
return login();
|
|
77
|
+
case "logout":
|
|
78
|
+
deleteCredentials();
|
|
79
|
+
info("Signed out on this machine. Revoke the sign-in on the dashboard to end it everywhere.");
|
|
80
|
+
return;
|
|
81
|
+
case "whoami":
|
|
82
|
+
info(readCredentials() ? `Signed in to ${apiUrl()}` : "Not signed in.");
|
|
83
|
+
return;
|
|
84
|
+
case "dev":
|
|
85
|
+
return dev({
|
|
86
|
+
profile: option(args, "--profile"),
|
|
87
|
+
bots: options(args, "--bot"),
|
|
88
|
+
settingsFile: option(args, "--settings"),
|
|
89
|
+
watch: !args.includes("--once"),
|
|
90
|
+
mcpPort: args.includes("--no-mcp") ? null : port(option(args, "--mcp-port") ?? "7788"),
|
|
91
|
+
});
|
|
92
|
+
case "test":
|
|
93
|
+
return test(args);
|
|
94
|
+
case "check":
|
|
95
|
+
return check();
|
|
96
|
+
case "build":
|
|
97
|
+
return build();
|
|
98
|
+
case undefined:
|
|
99
|
+
case "help":
|
|
100
|
+
case "--help":
|
|
101
|
+
case "-h":
|
|
102
|
+
info(HELP);
|
|
103
|
+
return;
|
|
104
|
+
case "--version":
|
|
105
|
+
case "-v":
|
|
106
|
+
info(SDK_VERSION);
|
|
107
|
+
return;
|
|
108
|
+
default:
|
|
109
|
+
fail(`Unknown command ${command}.`, "Run `minebot help`.");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
main(process.argv.slice(2)).catch(e => fail((e as Error).stack ?? String(e)));
|
package/src/cli/mcp.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
|
|
2
|
+
import type { AddressInfo } from "node:net";
|
|
3
|
+
|
|
4
|
+
/** A tool the MCP server offers. `run` returns JSON-able data; a thrown error becomes a tool error the agent sees. */
|
|
5
|
+
export interface McpTool {
|
|
6
|
+
name: string;
|
|
7
|
+
description: string;
|
|
8
|
+
inputSchema: Record<string, unknown>;
|
|
9
|
+
run(args: Record<string, unknown>): unknown;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface McpServerOptions {
|
|
13
|
+
/** First port to try; the next nine are tried if it is taken. 0 picks a free one. */
|
|
14
|
+
port: number;
|
|
15
|
+
name: string;
|
|
16
|
+
version: string;
|
|
17
|
+
instructions: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface McpServer {
|
|
21
|
+
readonly url: string;
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Newest first; an agent asking for one of these gets it, anything else gets the newest. */
|
|
26
|
+
const PROTOCOL_VERSIONS = ["2025-06-18", "2025-03-26", "2024-11-05"];
|
|
27
|
+
const MAX_BODY_BYTES = 1024 * 1024;
|
|
28
|
+
|
|
29
|
+
interface RpcMessage {
|
|
30
|
+
jsonrpc?: unknown;
|
|
31
|
+
id?: string | number | null;
|
|
32
|
+
method?: unknown;
|
|
33
|
+
params?: Record<string, unknown>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const rpcError = (id: RpcMessage["id"], code: number, message: string) => ({ jsonrpc: "2.0", id: id ?? null, error: { code, message } });
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A tools-only MCP server over streamable HTTP with JSON replies (no server-sent events): what `minebot dev`
|
|
40
|
+
* offers AI agents. It listens on 127.0.0.1 only and refuses a request whose Host or Origin names anything
|
|
41
|
+
* else, so a web page can't reach it through the browser (DNS rebinding, cross-site requests).
|
|
42
|
+
*/
|
|
43
|
+
export async function startMcpServer(tools: McpTool[], options: McpServerOptions): Promise<McpServer> {
|
|
44
|
+
const byName = new Map(tools.map(t => [t.name, t]));
|
|
45
|
+
let port = 0;
|
|
46
|
+
|
|
47
|
+
const dispatch = async (message: RpcMessage): Promise<unknown> => {
|
|
48
|
+
if (!message || typeof message !== "object" || message.jsonrpc !== "2.0" || typeof message.method !== "string")
|
|
49
|
+
return rpcError(message?.id, -32600, "Invalid request");
|
|
50
|
+
const notification = message.id === undefined;
|
|
51
|
+
let result: unknown;
|
|
52
|
+
switch (message.method) {
|
|
53
|
+
case "initialize": {
|
|
54
|
+
const requested = message.params?.protocolVersion;
|
|
55
|
+
result = {
|
|
56
|
+
protocolVersion: PROTOCOL_VERSIONS.includes(requested as string) ? requested : PROTOCOL_VERSIONS[0],
|
|
57
|
+
capabilities: { tools: {} },
|
|
58
|
+
serverInfo: { name: options.name, version: options.version },
|
|
59
|
+
instructions: options.instructions,
|
|
60
|
+
};
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
case "ping":
|
|
64
|
+
result = {};
|
|
65
|
+
break;
|
|
66
|
+
case "tools/list":
|
|
67
|
+
result = { tools: tools.map(({ name, description, inputSchema }) => ({ name, description, inputSchema })) };
|
|
68
|
+
break;
|
|
69
|
+
case "tools/call": {
|
|
70
|
+
const tool = byName.get(String(message.params?.name));
|
|
71
|
+
if (!tool) return rpcError(message.id, -32602, `Unknown tool ${String(message.params?.name)}`);
|
|
72
|
+
const args = message.params?.arguments;
|
|
73
|
+
try {
|
|
74
|
+
const value = await tool.run(args && typeof args === "object" ? (args as Record<string, unknown>) : {});
|
|
75
|
+
result = { content: [{ type: "text", text: typeof value === "string" ? value : JSON.stringify(value, null, 2) }] };
|
|
76
|
+
} catch (error) {
|
|
77
|
+
result = { content: [{ type: "text", text: (error as Error)?.message ?? String(error) }], isError: true };
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
default:
|
|
82
|
+
if (notification) return null;
|
|
83
|
+
return rpcError(message.id, -32601, `Unknown method ${message.method}`);
|
|
84
|
+
}
|
|
85
|
+
return notification ? null : { jsonrpc: "2.0", id: message.id, result };
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const reply = (res: ServerResponse, status: number, body?: unknown) => {
|
|
89
|
+
if (body === undefined) {
|
|
90
|
+
res.writeHead(status).end();
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
res.writeHead(status, { "Content-Type": "application/json" }).end(JSON.stringify(body));
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const readBody = (req: IncomingMessage) =>
|
|
97
|
+
new Promise<string>((resolve, reject) => {
|
|
98
|
+
const chunks: Buffer[] = [];
|
|
99
|
+
let size = 0;
|
|
100
|
+
req.on("data", (chunk: Buffer) => {
|
|
101
|
+
size += chunk.length;
|
|
102
|
+
if (size > MAX_BODY_BYTES) {
|
|
103
|
+
reject(new Error("Body too large"));
|
|
104
|
+
req.destroy();
|
|
105
|
+
} else chunks.push(chunk);
|
|
106
|
+
});
|
|
107
|
+
req.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
|
|
108
|
+
req.on("error", reject);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const handle = async (req: IncomingMessage, res: ServerResponse) => {
|
|
112
|
+
const allowed = [`127.0.0.1:${port}`, `localhost:${port}`];
|
|
113
|
+
const origin = req.headers.origin;
|
|
114
|
+
if (!allowed.includes(req.headers.host ?? "") || (origin !== undefined && !allowed.some(a => origin === `http://${a}`)))
|
|
115
|
+
return reply(res, 403, { error: "Only local MCP clients may connect" });
|
|
116
|
+
if ((req.url ?? "").split("?")[0] !== "/mcp") return reply(res, 404, { error: "The MCP endpoint is /mcp" });
|
|
117
|
+
if (req.method !== "POST") {
|
|
118
|
+
res.writeHead(405, { Allow: "POST" }).end();
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
let body: unknown;
|
|
123
|
+
try {
|
|
124
|
+
body = JSON.parse(await readBody(req));
|
|
125
|
+
} catch {
|
|
126
|
+
return reply(res, 400, rpcError(null, -32700, "Parse error"));
|
|
127
|
+
}
|
|
128
|
+
const messages = (Array.isArray(body) ? body : [body]) as RpcMessage[];
|
|
129
|
+
const replies = (await Promise.all(messages.map(dispatch))).filter(r => r !== null);
|
|
130
|
+
if (replies.length === 0) return reply(res, 202);
|
|
131
|
+
reply(res, 200, Array.isArray(body) ? replies : replies[0]);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const server: Server = createServer((req, res) => {
|
|
135
|
+
handle(req, res).catch(error => {
|
|
136
|
+
if (!res.headersSent) reply(res, 500, rpcError(null, -32603, (error as Error).message));
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const listen = (p: number) =>
|
|
141
|
+
new Promise<number>((resolve, reject) => {
|
|
142
|
+
const onError = (error: Error) => {
|
|
143
|
+
server.off("listening", onListening);
|
|
144
|
+
reject(error);
|
|
145
|
+
};
|
|
146
|
+
const onListening = () => {
|
|
147
|
+
server.off("error", onError);
|
|
148
|
+
resolve((server.address() as AddressInfo).port);
|
|
149
|
+
};
|
|
150
|
+
server.once("error", onError);
|
|
151
|
+
server.once("listening", onListening);
|
|
152
|
+
server.listen(p, "127.0.0.1");
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
for (let attempt = 0; ; attempt++) {
|
|
156
|
+
try {
|
|
157
|
+
port = await listen(options.port === 0 ? 0 : options.port + attempt);
|
|
158
|
+
break;
|
|
159
|
+
} catch (error) {
|
|
160
|
+
if ((error as NodeJS.ErrnoException).code !== "EADDRINUSE" || options.port === 0 || attempt >= 9) throw error;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
url: `http://127.0.0.1:${port}/mcp`,
|
|
166
|
+
close: () => new Promise<void>(resolve => server.close(() => resolve())),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Terminal output: colours when the terminal supports them, plain text otherwise (and for AI agents reading logs).
|
|
2
|
+
|
|
3
|
+
const useColor = process.stdout.isTTY && !process.env.NO_COLOR;
|
|
4
|
+
const wrap = (code: number) => (text: string) => (useColor ? `\x1b[${code}m${text}\x1b[0m` : text);
|
|
5
|
+
|
|
6
|
+
export const dim = wrap(2);
|
|
7
|
+
export const bold = wrap(1);
|
|
8
|
+
export const red = wrap(31);
|
|
9
|
+
export const green = wrap(32);
|
|
10
|
+
export const yellow = wrap(33);
|
|
11
|
+
export const cyan = wrap(36);
|
|
12
|
+
|
|
13
|
+
export function info(message: string): void {
|
|
14
|
+
console.log(message);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function warn(message: string): void {
|
|
18
|
+
console.warn(yellow(`warning: ${message}`));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Prints an error with what to do next and exits with code 1. */
|
|
22
|
+
export function fail(message: string, fix?: string): never {
|
|
23
|
+
console.error(red(`error: ${message}`));
|
|
24
|
+
if (fix) console.error(` ${fix}`);
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function timestamp(): string {
|
|
29
|
+
return dim(new Date().toLocaleTimeString());
|
|
30
|
+
}
|
package/src/cli/test.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, rmSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import * as esbuild from "esbuild";
|
|
5
|
+
import { findProject, formatErrors } from "./build.js";
|
|
6
|
+
import { fail } from "./output.js";
|
|
7
|
+
|
|
8
|
+
/** `minebot test`: bundles test/*.test.ts (TypeScript, no setup needed) and runs them with node's test runner. */
|
|
9
|
+
export async function test(extra: string[]): Promise<void> {
|
|
10
|
+
const project = findProject();
|
|
11
|
+
const testDir = join(project.root, "test");
|
|
12
|
+
if (!existsSync(testDir)) fail("No test folder.", "Put tests in test/*.test.ts; see the one `minebot create` writes.");
|
|
13
|
+
const entries = readdirSync(testDir).filter(f => f.endsWith(".test.ts")).map(f => join(testDir, f));
|
|
14
|
+
if (entries.length === 0) fail("No test/*.test.ts files.");
|
|
15
|
+
|
|
16
|
+
const outDir = join(project.root, ".minebot", "test");
|
|
17
|
+
rmSync(outDir, { recursive: true, force: true });
|
|
18
|
+
mkdirSync(outDir, { recursive: true });
|
|
19
|
+
try {
|
|
20
|
+
await esbuild.build({
|
|
21
|
+
entryPoints: entries,
|
|
22
|
+
outdir: outDir,
|
|
23
|
+
outExtension: { ".js": ".mjs" },
|
|
24
|
+
bundle: true,
|
|
25
|
+
format: "esm",
|
|
26
|
+
platform: "node",
|
|
27
|
+
target: "node22",
|
|
28
|
+
sourcemap: "inline",
|
|
29
|
+
external: ["@minecraftbot-com/plugin", "@minecraftbot-com/plugin/*"],
|
|
30
|
+
logLevel: "silent",
|
|
31
|
+
});
|
|
32
|
+
} catch (e) {
|
|
33
|
+
fail(`Build failed:\n${formatErrors((e as esbuild.BuildFailure).errors ?? [])}`);
|
|
34
|
+
}
|
|
35
|
+
const files = readdirSync(outDir).filter(f => f.endsWith(".mjs")).map(f => join(outDir, f));
|
|
36
|
+
const result = spawnSync(process.execPath, ["--enable-source-maps", "--test", ...extra, ...files], { stdio: "inherit" });
|
|
37
|
+
process.exit(result.status ?? 1);
|
|
38
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown for mistakes in plugin code: a missing permission, bad arguments, a call after the bot stopped.
|
|
3
|
+
* Expected failures (no path, out of reach) are never thrown; they come back as `{ ok: false, reason }`.
|
|
4
|
+
*/
|
|
5
|
+
export class PluginError extends Error {
|
|
6
|
+
constructor(readonly code: string, message: string) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "PluginError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** The plugin called something its manifest doesn't declare. The message names the permission to add. */
|
|
13
|
+
export class PermissionDeniedError extends PluginError {
|
|
14
|
+
constructor(message: string) {
|
|
15
|
+
super("PermissionDenied", message);
|
|
16
|
+
this.name = "PermissionDeniedError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The bot (or the whole plugin) stopped while this call was waiting. The SDK swallows it when it reaches
|
|
22
|
+
* `run`, so you never need to catch it; it only stops the code that was waiting.
|
|
23
|
+
*/
|
|
24
|
+
export class StoppedError extends PluginError {
|
|
25
|
+
constructor(readonly reason: string) {
|
|
26
|
+
super("Stopped", `Stopped: ${reason}`);
|
|
27
|
+
this.name = "StoppedError";
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** True for the error the SDK uses to unwind code when a bot stops. */
|
|
32
|
+
export function isStopped(e: unknown): e is StoppedError {
|
|
33
|
+
return e instanceof StoppedError;
|
|
34
|
+
}
|