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:
@@ -0,0 +1,119 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile, access } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { API_VERSION, describeFeature, hasFeature, missingFeatures, MissingFeatureError, pluginFeatures, requireFeature, requireFeatures, type PluginApi } from "../../../packages/plugin-sdk/src/index.ts";
|
||||
import * as sdkIcons from "../../../packages/plugin-sdk/src/icons.ts";
|
||||
import * as sdkTooltip from "../../../packages/plugin-sdk/src/tooltip.ts";
|
||||
import * as hostIcons from "../../../src/lib/ui/actionIcons.ts";
|
||||
import * as hostTooltip from "../../../src/lib/ui/tooltip.ts";
|
||||
import { ExternalSession, type SessionServices } from "../../../src/lib/plugins/externalSession.ts";
|
||||
import { ContributionRegistry } from "../../../src/lib/plugins/registry.ts";
|
||||
import { ServiceHub } from "../../../src/lib/plugins/services.ts";
|
||||
import { declarationImports } from "../../../scripts/buildSdk.mjs";
|
||||
import type { ActionMenuItem as HostMenuItem } from "../../../src/lib/ui/actionMenu.ts";
|
||||
import type { MenuOptions as HostMenuOptions } from "../../../src/lib/ui/managedActionMenu.ts";
|
||||
import type { ActionMenuItem, ActionMenuOptions } from "../../../packages/plugin-sdk/src/index.ts";
|
||||
|
||||
// The host menu files must match the copies in the Terminal plugin (scripts/menuSurfaceAudit.mjs), so they keep
|
||||
// their own menu types. `pnpm check` fails when those types and the SDK types differ.
|
||||
type Same<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false;
|
||||
const sameMenuTypes: [Same<HostMenuItem, ActionMenuItem>, Same<HostMenuOptions, ActionMenuOptions>] = [true, true];
|
||||
|
||||
const base: SessionServices = {
|
||||
workspace: { list: async () => [], read: async () => "", write: async () => {} },
|
||||
settings: { read: async () => null, write: async () => {} }, assets: { read: async () => new Uint8Array() },
|
||||
};
|
||||
|
||||
async function hostApi(services: Partial<SessionServices>, views = false) {
|
||||
let api!: PluginApi;
|
||||
const session = views
|
||||
? new ExternalSession("org.test.sdk", { ...base, ...services }, () => {}, () => {}, new ContributionRegistry(), async () => {}, async () => "", () => {}, () => {}, () => {})
|
||||
: new ExternalSession("org.test.sdk", { ...base, ...services }, () => {}, () => {}, new ContributionRegistry());
|
||||
await session.start({ activate(value) { api = value; } });
|
||||
return { api, session };
|
||||
}
|
||||
|
||||
test("feature checks find optional members, and missing members give an update message", () => {
|
||||
const read = async () => new Uint8Array([1]);
|
||||
const api = { workspace: { readBytes: read }, git: { status: async () => ({}) } } as unknown as PluginApi;
|
||||
assert.equal(hasFeature(api, "workspace.readBytes"), true);
|
||||
assert.equal(hasFeature(api, "git"), true);
|
||||
assert.equal(hasFeature(api, "git.push"), false);
|
||||
assert.equal(hasFeature(api, "pty"), false);
|
||||
assert.deepEqual(missingFeatures(api, ["pty", "workspace.readBytes", "settings.subscribe"]), ["pty", "settings.subscribe"]);
|
||||
assert.equal(requireFeature(api, "git"), (api as unknown as { git: unknown }).git);
|
||||
assert.throws(() => requireFeature(api, "pty"), (error: unknown) => error instanceof MissingFeatureError
|
||||
&& error.name === "MissingFeatureError" && error.features.length === 1 && error.features[0] === "pty"
|
||||
&& error.message === "This plugin needs the terminal API (api.pty). Update RuneStone to a version that has this feature.");
|
||||
assert.throws(() => requireFeatures(api, ["pty", "files", "workspace.readBytes"]), {
|
||||
message: "This plugin needs the terminal API (api.pty) and the folder grant API (api.files). Update RuneStone to a version that has these features.",
|
||||
});
|
||||
assert.doesNotThrow(() => requireFeatures(api, ["git", "workspace.readBytes"]));
|
||||
assert.throws(() => hasFeature(api, "workspace.read" as never), TypeError);
|
||||
assert.equal(describeFeature("ui.showActionMenu"), "the shared action menu (api.ui.showActionMenu)");
|
||||
assert.equal(API_VERSION, 1);
|
||||
});
|
||||
|
||||
test("a required function stays bound to its parent object", async () => {
|
||||
const workspace = { prefix: "ws:", async readOptional(this: { prefix: string }, file: string) { return this.prefix + file; } };
|
||||
const api = { workspace } as unknown as PluginApi;
|
||||
const readOptional = requireFeature(api, "workspace.readOptional");
|
||||
assert.equal(await readOptional("a.md"), "ws:a.md");
|
||||
});
|
||||
|
||||
test("the current host gives every SDK feature when all services are present", async () => {
|
||||
const pty = { open: async () => {}, write: async () => {}, resize: async () => {}, close: async () => {}, profiles: async () => [], openWindow: async () => {}, dictation: async () => {} };
|
||||
const files = { chooseFolder: async () => null, pictureFolder: async () => ({ id: "g", path: "p", name: "n" }), grants: async () => [], revoke: async () => false, list: async () => [], readBytes: async () => new Uint8Array(), readSidecar: async () => null, writeSidecar: async () => {} };
|
||||
const git = { status: async () => ({ available: true, repository: true, branch: null, changes: [] }), diff: async () => "", stage: async () => {}, unstage: async () => {}, commit: async () => {}, push: async () => {}, pull: async () => {}, graph: async () => ({ commits: [] }), commitDetails: async () => ({ id: "", parents: [], author: "", date: "", message: "", files: [] }), commitDiff: async () => "", blame: async () => null };
|
||||
const backgrounds = { chooseFolder: async () => null, list: async () => [], read: async () => new Uint8Array() };
|
||||
const { api, session } = await hostApi({
|
||||
pty, files, git, backgrounds, serviceHub: new ServiceHub(), openFile: async () => {}, completionTransport: async () => {},
|
||||
workspace: { ...base.workspace, listMarkdown: async () => [], readMany: async () => [], readBytes: async () => new Uint8Array(), readOptional: async () => null, create: async () => {}, subscribe: () => () => {} },
|
||||
settings: { ...base.settings, subscribe: () => () => {} },
|
||||
}, true);
|
||||
assert.deepEqual(missingFeatures(api, pluginFeatures), []);
|
||||
await session.stop();
|
||||
});
|
||||
|
||||
test("the host without optional services lacks only the features that depend on them", async () => {
|
||||
const { api, session } = await hostApi({});
|
||||
assert.deepEqual(missingFeatures(api, pluginFeatures).sort(), [
|
||||
"backgrounds", "closeView", "completion.transport", "files", "git", "git.commitDetails", "git.commitDiff", "git.graph", "git.pull", "git.push",
|
||||
"openFile", "pty", "renameDocument", "replaceDocumentWithView", "replaceView", "settings.subscribe",
|
||||
"workspace.create", "workspace.listMarkdown", "workspace.readBytes", "workspace.readMany", "workspace.readOptional", "workspace.subscribe",
|
||||
]);
|
||||
await session.stop();
|
||||
});
|
||||
|
||||
test("the host uses the SDK icons and tooltip, so plugins get the same artwork and behavior", () => {
|
||||
assert.equal(hostIcons.actionIcons, sdkIcons.actionIcons);
|
||||
assert.equal(hostIcons.actionIcon, sdkIcons.actionIcon);
|
||||
assert.equal(hostTooltip.attachTooltip, sdkTooltip.attachTooltip);
|
||||
for (const [name, markup] of Object.entries(sdkIcons.actionIcons)) assert.match(markup, /^<(path|circle|rect|ellipse)\b/, name);
|
||||
});
|
||||
|
||||
test("the host menu types are the SDK menu types", () => {
|
||||
assert.deepEqual(sameMenuTypes, [true, true]);
|
||||
});
|
||||
|
||||
test("the SDK build gives declaration imports with .js paths", () => {
|
||||
assert.equal(declarationImports('export type * from "./api.ts";\nimport type { A } from \'../b.ts\';\ntype C = import("./c.ts").C;\nimport x from "node:fs";'),
|
||||
'export type * from "./api.js";\nimport type { A } from \'../b.js\';\ntype C = import("./c.js").C;\nimport x from "node:fs";');
|
||||
});
|
||||
|
||||
test("each SDK export has a source module, and the package is not for npm", async () => {
|
||||
const folder = path.resolve("packages/plugin-sdk");
|
||||
const manifest = JSON.parse(await readFile(path.join(folder, "package.json"), "utf8"));
|
||||
assert.equal(manifest.name, "@runestone/plugin-sdk");
|
||||
assert.equal(manifest.private, true);
|
||||
assert.equal(manifest.license, "Apache-2.0");
|
||||
assert.equal(manifest.dependencies, undefined);
|
||||
assert.deepEqual(manifest.files, ["dist", "README.md", "LICENSE", "NOTICE"]);
|
||||
for (const file of ["README.md", "LICENSE", "NOTICE"]) await access(path.join(folder, file));
|
||||
for (const [entry, target] of Object.entries(manifest.exports as Record<string, string | { types: string; default: string }>)) {
|
||||
if (typeof target === "string") continue;
|
||||
assert.equal(target.types.replace(/\.d\.ts$/, ""), target.default.replace(/\.js$/, ""), entry);
|
||||
await access(path.join(folder, target.default.replace(/^\.\/dist\//, "src/").replace(/\.js$/, ".ts")));
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user