-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate-plugin.mjs
More file actions
192 lines (172 loc) · 5.4 KB
/
Copy pathcreate-plugin.mjs
File metadata and controls
192 lines (172 loc) · 5.4 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { mkdir, writeFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import {
pluginSdkArchive,
pluginSdkVersion,
} from "./plugin-sdk-provenance.mjs";
const defaultRepositoryRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
function command(executable, args, repositoryRoot) {
const result = spawnSync(executable, args, {
cwd: repositoryRoot,
stdio: "inherit",
});
if (result.error) throw result.error;
if (result.status !== 0) {
throw new Error(`${executable} ${args.join(" ")} exited ${result.status}`);
}
}
function parseArguments(argv) {
const options = {};
for (let index = 0; index < argv.length; index += 1) {
const token = argv[index];
if (token === "--skip-install") options.skipInstall = true;
else if (token === "--skip-verify") options.skipVerify = true;
else {
const value = argv[index + 1];
if (!token.startsWith("--") || !value || value.startsWith("--")) {
throw new Error(`invalid argument ${token}`);
}
index += 1;
const key = token.slice(2);
options[key] = value;
}
}
return options;
}
function validateOptions(options) {
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(options.slug ?? "")) {
throw new Error("--slug must use lowercase letters, numbers, and dashes");
}
for (const key of ["name", "description"]) {
if (typeof options[key] !== "string" || options[key].trim() === "") {
throw new Error(`--${key} is required`);
}
}
}
function filesFor(options) {
const packageName = `bb-plugin-${options.slug}`;
const installRef = `plugin/${options.slug}`;
const loadedMessage = JSON.stringify(`${options.name} loaded`);
const testName = JSON.stringify(`${options.name} plugin`);
const manifest = {
name: packageName,
version: "0.1.0",
description: options.description,
type: "module",
license: "UNLICENSED",
files: ["dist", "README.md"],
scripts: {
build: "node ../../tooling/build-plugin.mjs",
check: "npm run typecheck && npm run build && npm test",
test: "vitest run",
typecheck: "tsc --noEmit",
},
engines: { bb: ">=0.0.34", bbPluginSdk: `^${pluginSdkVersion}` },
bb: {
name: options.name,
description: options.description,
branding: { icon: "Puzzle" },
server: "./server.ts",
skills: [],
},
devDependencies: {
"@get-bb/plugin-sdk": `file:../../tooling/vendor/${pluginSdkArchive}`,
"@types/better-sqlite3": "^7.6.12",
"@types/node": "^22.0.0",
"better-sqlite3": "^12.10.0",
"cron-parser": "^5.5.0",
hono: "^4.11.9",
typescript: "^5.7.0",
vitest: "^4.1.8",
zod: "^4.3.6",
},
};
const readme = `# ${options.name}
${options.description}
## Install
\`\`\`bash
bb plugin install git:https://github.com/brsbl/bb-plugins.git@${installRef} --yes
\`\`\`
## Use
${options.when ?? `Use ${options.name} when its focused capability is useful in bb.`}
## Develop
From the monorepo root:
\`\`\`bash
npm ci
npm run check --workspace=${packageName}
bb plugin install "path:$PWD/plugins/${options.slug}" --yes
\`\`\`
`;
const server = `import type { BbPluginApi } from "@get-bb/plugin-sdk";
export default function plugin(bb: BbPluginApi): void {
bb.log.info(${loadedMessage});
}
`;
const test = `import { createFakePluginHost } from "@get-bb/plugin-sdk/testing";
import { describe, expect, it } from "vitest";
import plugin from "./server";
describe(${testName}, () => {
it("loads through the bb plugin harness", async () => {
const { bb, harness } = createFakePluginHost({ pluginId: "${options.slug}" });
plugin(bb);
expect(harness.inspection.logEntries.at(-1)?.message).toBe(${loadedMessage});
await harness.lifecycle.dispose();
});
});
`;
const tsconfig = {
compilerOptions: {
target: "ES2022",
lib: ["ES2022"],
module: "ESNext",
moduleResolution: "Bundler",
strict: true,
noEmit: true,
skipLibCheck: true,
types: ["node", "vitest/globals"],
},
include: ["*.ts"],
};
return {
"package.json": `${JSON.stringify(manifest, null, 2)}\n`,
"README.md": readme,
"server.ts": server,
"server.test.ts": test,
"tsconfig.json": `${JSON.stringify(tsconfig, null, 2)}\n`,
};
}
export async function scaffoldPlugin(rawOptions) {
const options = {
skipInstall: false,
skipVerify: false,
...rawOptions,
};
validateOptions(options);
const repositoryRoot = resolve(options.repositoryRoot ?? defaultRepositoryRoot);
const directory = resolve(
options.output ?? resolve(repositoryRoot, "plugins", options.slug),
);
await mkdir(directory);
for (const [name, contents] of Object.entries(filesFor(options))) {
await writeFile(resolve(directory, name), contents);
}
if (!options.skipInstall) command("npm", ["install"], repositoryRoot);
if (!options.skipVerify) {
command(
"npm",
["run", "check", `--workspace=bb-plugin-${options.slug}`],
repositoryRoot,
);
}
return { directory, packageName: `bb-plugin-${options.slug}` };
}
async function main() {
const options = parseArguments(process.argv.slice(2));
const result = await scaffoldPlugin(options);
console.log(`created ${result.packageName} at ${result.directory}`);
}
if (resolve(process.argv[1] ?? "") === fileURLToPath(import.meta.url)) {
await main();
}