Add the versioned plugin SDK package and its release script

The new package packages/plugin-sdk (@runestone/plugin-sdk) is the one source of the public plugin types.
It contains PluginApi and its members, the manifest type, API_VERSION, and feature checks
(hasFeature, requireFeature, requireFeatures, missingFeatures). It also contains the action icons and
the tooltip helper as DOM functions, because RuneStone has no API for them. Menus stay host-drawn through
api.ui.showActionMenu, so the SDK contains only the menu types. The SDK uses the Apache License 2.0.

The host imports the SDK with relative paths in src/lib/plugins/sdk.ts, src/lib/ui/actionIcons.ts, and
src/lib/ui/tooltip.ts. The completion, Git, background, and contribution contract files export the SDK types.
The menu files keep their types, because the Terminal plugin keeps copies of them. A unit test makes sure
that those types and the SDK types are the same. The menu inventory now scans the SDK source.

pnpm sdk:build compiles the SDK to dist/. pnpm release:sdk --version <v> builds it, then commits and tags
v<v> in a local clone of the public repository. It never pushes, and it prints the push commands.
It stops before a change in these conditions:
- The clone is missing, has changes, or is not on a branch.
- The tag exists, or the version is not greater than the current version.
- The package has no LICENSE file, or the SDK source has changes that are not committed.
A failed attempt restores the version and the clone.

pnpm check type checks the SDK and the example plugin. The example plugin now has JSDoc types from the SDK.
pnpm test covers the feature checks, the host and SDK parity, and the release script with temporary
Git repositories. The plugin development, release, development, and completion guides describe the SDK.
This commit is contained in:
Knotty Wyvern
2026-10-28 02:33:16 -07:00
parent 9591c251e9
commit bb0b8f88b6
37 changed files with 1710 additions and 438 deletions
+196
View File
@@ -0,0 +1,196 @@
import test from "node:test";
import assert from "node:assert/strict";
import { mkdtemp, mkdir, readFile, writeFile, copyFile, rm, access, readdir } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { clonePath, describePlan, git, parseArguments, planSdkRelease, releasePackageJson, runSdkRelease } from "../../../scripts/releaseSdk.mjs";
// These tests use temporary Git repositories. They need no network and never push.
const repository = path.resolve(".");
const sdk = "packages/plugin-sdk";
const exists = (file: string) => access(file).then(() => true, () => false);
const identity = async (folder: string) => {
await git(folder, ["config", "user.name", "SDK test"]);
await git(folder, ["config", "user.email", "[email protected]"]);
};
type Setup = { base: string; root: string; clone: string; remote: string; env: Record<string, string> };
// A host repository with the SDK package files, a clone of the public repository, and a bare remote for that clone.
async function fixture(options: { empty?: boolean; origin?: boolean } = {}): Promise<Setup> {
const base = await mkdtemp(path.join(tmpdir(), "runestone-sdk-release-"));
const root = path.join(base, "RuneStone");
await mkdir(path.join(root, sdk), { recursive: true });
for (const file of ["package.json", "README.md", "LICENSE", "NOTICE"]) await copyFile(path.join(repository, sdk, file), path.join(root, sdk, file));
const source = JSON.parse(await readFile(path.join(root, sdk, "package.json"), "utf8"));
await writeFile(path.join(root, sdk, "package.json"), JSON.stringify({ ...source, version: "0.0.0", scripts: { build: "tsc" }, devDependencies: { typescript: "6" } }, null, 2) + "\n");
await writeFile(path.join(root, ".gitignore"), "dist/\n");
await git(root, ["init", "-q", "-b", "master"]);
await identity(root);
await git(root, ["add", "-A"]);
await git(root, ["commit", "-q", "-m", "host"]);
const remote = path.join(base, "remote.git");
await git(base, ["init", "-q", "--bare", remote]);
const clone = path.join(base, "RuneStone_Plugin_SDK");
await mkdir(clone);
await git(clone, ["init", "-q", "-b", "main"]);
await identity(clone);
if (options.origin !== false) await git(clone, ["remote", "add", "origin", remote]);
if (!options.empty) {
await writeFile(path.join(clone, ".gitignore"), "dist/\nnode_modules/\n");
await git(clone, ["add", "-A"]);
await git(clone, ["commit", "-q", "-m", "Start"]);
}
return { base, root, clone, remote, env: { RUNESTONE_PLUGIN_REPOS: base } };
}
async function withFixture(options: { empty?: boolean; origin?: boolean }, body: (setup: Setup) => Promise<void>) {
const setup = await fixture(options);
try { await body(setup); } finally { await rm(setup.base, { recursive: true, force: true }); }
}
// A build that writes a small dist/ folder in place of the TypeScript compiler.
const fakeBuild = async (root: string) => {
const dist = path.join(root, sdk, "dist");
await mkdir(path.join(dist, "nested"), { recursive: true });
await writeFile(path.join(dist, "index.js"), "export const API_VERSION = 1;\n");
await writeFile(path.join(dist, "index.d.ts"), "export declare const API_VERSION = 1;\n");
await writeFile(path.join(dist, "nested", "extra.js"), "export {};\n");
return dist;
};
const failingBuild = async () => { throw Error("The compiler failed."); };
async function state(setup: Setup) {
return {
source: await readFile(path.join(setup.root, sdk, "package.json"), "utf8"),
head: await git(setup.clone, ["rev-parse", "--verify", "--quiet", "HEAD"]).catch(() => null),
status: await git(setup.clone, ["status", "--porcelain", "--untracked-files=all"]),
tags: await git(setup.clone, ["tag", "--list"]),
files: (await readdir(setup.clone)).sort(),
};
}
const quiet = async <T>(work: () => Promise<T>) => {
const error = console.error;
console.error = () => {};
try { return await work(); } finally { console.error = error; }
};
test("the SDK release script reads --version and --dry-run", () => {
assert.deepEqual(parseArguments(["--version", "0.1.0"]), { version: "0.1.0", dryRun: false });
assert.deepEqual(parseArguments(["--dry-run", "--version", "0.1.0a"]), { version: "0.1.0a", dryRun: true });
for (const args of [[], ["--dry-run"], ["--version"], ["0.1.0"], ["--version", "0.1.0", "--version", "0.1.1"], ["--push"]]) assert.throws(() => parseArguments(args));
});
test("the clone path comes from RUNESTONE_SDK_REPO, then RUNESTONE_PLUGIN_REPOS, then the parent folder", () => {
const root = path.resolve("/work/RuneStone");
assert.equal(clonePath(root, { RUNESTONE_SDK_REPO: "/elsewhere/sdk", RUNESTONE_PLUGIN_REPOS: "/plugins" }), path.resolve("/elsewhere/sdk"));
assert.equal(clonePath(root, { RUNESTONE_PLUGIN_REPOS: "/plugins" }), path.join(path.resolve("/plugins"), "RuneStone_Plugin_SDK"));
assert.equal(clonePath(root, {}), path.join(path.resolve("/work"), "RuneStone_Plugin_SDK"));
});
test("the release package.json has the version and no build scripts or development dependencies", () => {
const source = { name: "@runestone/plugin-sdk", version: "0.0.0", types: "./dist/index.d.ts", exports: {}, files: ["dist"], license: "Apache-2.0", scripts: { build: "tsc" }, devDependencies: { typescript: "6" } };
assert.deepEqual(releasePackageJson(source, "0.1.0"), { name: "@runestone/plugin-sdk", version: "0.1.0", types: "./dist/index.d.ts", exports: {}, files: ["dist"], license: "Apache-2.0" });
assert.throws(() => releasePackageJson({ ...source, license: undefined }, "0.1.0"), /license/);
});
test("a dry run plans the release, prints the push commands, and changes no file", () => withFixture({}, async setup => {
const before = await state(setup);
const plan = await planSdkRelease(setup.root, "0.1.0", setup.env);
const text = describePlan(plan);
assert.match(text, /SDK version: 0\.1\.0 \(current 0\.0\.0\)/);
assert.match(text, /Replace dist, package\.json, README\.md, LICENSE, NOTICE in the clone/);
assert.match(text, /create the tag v0\.1\.0/);
assert.deepEqual(plan.pushCommands, [`git -C "${setup.clone}" push origin main`, `git -C "${setup.clone}" push origin v0.1.0`]);
assert.equal(plan.packageJson.version, "0.1.0");
assert.equal(plan.packageJson.scripts, undefined);
assert.equal(plan.packageJson.devDependencies, undefined);
assert.deepEqual(await state(setup), before);
}));
test("a clone without an origin remote gets the command that adds it", () => withFixture({ origin: false }, async setup => {
const plan = await planSdkRelease(setup.root, "0.1.0", setup.env);
assert.match(plan.pushCommands[0], /remote add origin https:\/\/git\.knottywyvern\.com\/kwyvern\/RuneStone_Plugin_SDK\.git$/);
}));
test("the release refuses a missing, unrelated, dirty, or detached clone", () => withFixture({}, async setup => {
await assert.rejects(planSdkRelease(setup.root, "0.1.0", { RUNESTONE_SDK_REPO: path.join(setup.base, "missing") }), /clone is missing/);
const plain = path.join(setup.root, sdk);
await assert.rejects(planSdkRelease(setup.root, "0.1.0", { RUNESTONE_SDK_REPO: plain }), /not the root of a Git repository/);
await writeFile(path.join(setup.clone, "notes.txt"), "draft");
await assert.rejects(planSdkRelease(setup.root, "0.1.0", setup.env), /clone has changes/);
await rm(path.join(setup.clone, "notes.txt"));
await git(setup.clone, ["checkout", "-q", "--detach"]);
await assert.rejects(planSdkRelease(setup.root, "0.1.0", setup.env), /not on a branch/);
}));
test("the release never replaces a tag and never goes back to an earlier version", () => withFixture({}, async setup => {
await git(setup.clone, ["tag", "v0.1.0"]);
await assert.rejects(planSdkRelease(setup.root, "0.1.0", setup.env), /The tag v0\.1\.0 exists/);
await assert.rejects(planSdkRelease(setup.root, "0.0.0", setup.env), /greater than the current version 0\.0\.0/);
await assert.rejects(planSdkRelease(setup.root, "1.0", setup.env), /release version/);
await writeFile(path.join(setup.clone, "package.json"), JSON.stringify({ version: "0.2.0" }));
await git(setup.clone, ["add", "-A"]);
await git(setup.clone, ["commit", "-q", "-m", "Earlier release"]);
await assert.rejects(planSdkRelease(setup.root, "0.1.1", setup.env), /greater than the version 0\.2\.0 in the clone/);
}));
test("the release refuses a package without a license file or with source changes that are not committed", () => withFixture({}, async setup => {
await rm(path.join(setup.root, sdk, "LICENSE"));
await assert.rejects(planSdkRelease(setup.root, "0.1.0", setup.env), /no LICENSE file/);
await git(setup.root, ["checkout", "-q", "--", sdk]);
await writeFile(path.join(setup.root, sdk, "README.md"), "changed");
await assert.rejects(planSdkRelease(setup.root, "0.1.0", setup.env), /not committed/);
}));
test("a completed release commits and tags in the clone, sets the source version, and pushes nothing", () => withFixture({}, async setup => {
const plan = await planSdkRelease(setup.root, "0.1.0", setup.env);
const result = await runSdkRelease(plan, fakeBuild);
assert.equal(result.tag, "v0.1.0");
assert.equal(await git(setup.clone, ["status", "--porcelain", "--untracked-files=all"]), "");
assert.equal(await git(setup.clone, ["cat-file", "-t", "v0.1.0"]), "tag");
assert.equal(await git(setup.clone, ["rev-parse", "v0.1.0^{commit}"]), result.commit);
const tracked = (await git(setup.clone, ["ls-files"])).split("\n").sort();
assert.deepEqual(tracked, [".gitignore", "LICENSE", "NOTICE", "README.md", "dist/index.d.ts", "dist/index.js", "dist/nested/extra.js", "package.json"]);
const published = JSON.parse(await readFile(path.join(setup.clone, "package.json"), "utf8"));
assert.equal(published.version, "0.1.0");
assert.equal(published.license, "Apache-2.0");
assert.equal(published.scripts, undefined);
assert.equal(await readFile(path.join(setup.clone, "LICENSE"), "utf8"), await readFile(path.join(setup.root, sdk, "LICENSE"), "utf8"));
assert.match(await git(setup.clone, ["log", "-1", "--format=%B"]), new RegExp(`^Release @runestone/plugin-sdk 0\\.1\\.0\\n\\nBuilt from RuneStone ${plan.hostCommit}\\.`));
assert.equal(JSON.parse(await readFile(path.join(setup.root, sdk, "package.json"), "utf8")).version, "0.1.0");
assert.equal(await git(setup.remote, ["for-each-ref"]), "", "The release must not push.");
// The next release replaces dist/ and removes files that the new build does not contain.
await git(setup.root, ["commit", "-q", "-am", "Record 0.1.0"]);
const next = await planSdkRelease(setup.root, "0.1.1", setup.env);
await runSdkRelease(next, async root => { const dist = await fakeBuild(root); await rm(path.join(dist, "nested"), { recursive: true }); return dist; });
assert.equal(await exists(path.join(setup.clone, "dist/nested/extra.js")), false);
assert.deepEqual((await git(setup.clone, ["tag", "--list"])).split("\n"), ["v0.1.0", "v0.1.1"]);
}));
test("a failed build restores the version and leaves the clone unchanged", () => withFixture({}, async setup => {
const before = await state(setup);
const plan = await planSdkRelease(setup.root, "0.1.0", setup.env);
await quiet(() => assert.rejects(runSdkRelease(plan, failingBuild), /compiler failed/));
assert.deepEqual(await state(setup), before);
}));
test("a failed commit restores the clone, also in a clone without commits", async () => {
for (const empty of [false, true]) {
await withFixture({ empty }, async setup => {
const before = await state(setup);
await writeFile(path.join(setup.clone, ".git/hooks/pre-commit"), "#!/bin/sh\nexit 1\n");
const plan = await planSdkRelease(setup.root, "0.1.0", setup.env);
assert.equal(plan.head, empty ? null : before.head);
await quiet(() => assert.rejects(runSdkRelease(plan, fakeBuild), /git commit/));
assert.deepEqual(await state(setup), before);
await rm(path.join(setup.clone, ".git/hooks/pre-commit"));
if (empty) {
await runSdkRelease(await planSdkRelease(setup.root, "0.1.0", setup.env), fakeBuild);
assert.equal(await git(setup.clone, ["rev-list", "--count", "HEAD"]), "1");
}
});
}
});