Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions app/scripts/integration-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ import { resolve } from "node:path";

const root = resolve(import.meta.dirname, "../..");
const directory = resolve(process.env.TOKAMAK_PLUGIN_DIR ?? `${root}/app/.dev/integration/plugins`);
for (const plugin of [
"plugins/chat",
process.env.TOKAMAK_FIXTURE_SOURCE ?? "app/tests/fixtures/integration",
]) {
const fixture = process.env.TOKAMAK_FIXTURE_SOURCE ?? "app/tests/fixtures/integration";
if (process.argv.includes("--reload")) {
if (!process.env.TOKAMAK_SERVER_DIR)
throw new Error("Set TOKAMAK_SERVER_DIR to the disposable local server directory");
for (const args of [
["add", fixture],
["reload", "integration-fixture"],
]) {
const result = spawnSync("proto", ["run", "bun", "--", "run", "--silent", "tokamak", "plugin", ...args], {
cwd: root,
env: { ...process.env, TOKAMAK_SERVER: undefined, TOKAMAK_TOKEN: undefined },
stdio: "inherit",
});
if (result.error) throw result.error;
if (result.status !== 0) process.exit(result.status ?? 1);
}
process.exit(0);
}
for (const plugin of ["plugins/chat", fixture]) {
const result = spawnSync("proto", ["run", "bun", "--", "packages/build/src/build.ts", plugin], {
cwd: root,
stdio: "inherit",
Expand All @@ -18,15 +33,4 @@ for (const plugin of [
await mkdir(`${directory}/${id}`, { recursive: true });
await cp(resolve(root, plugin, "dist"), `${directory}/${id}/dist`, { recursive: true });
}
if (process.argv.includes("--reload")) {
const result = spawnSync(
`${root}/target/debug/tokamak-server`,
["plugin", "reload", "integration-fixture"],
{
stdio: "inherit",
},
);
if (result.error) throw result.error;
process.exitCode = result.status ?? 1;
}
console.log(`Integration plugins: ${directory}`);
32 changes: 11 additions & 21 deletions e2e/browser/app.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect, type Page } from "@playwright/test";
import { spawnSync } from "node:child_process";
import { cp, readFile, writeFile } from "node:fs/promises";
import { readFile, writeFile } from "node:fs/promises";

const connected = (page: Page) =>
expect(page.getByRole("status", { name: "Connection status" })).toHaveText("127.0.0.1:43123");
Expand All @@ -14,7 +14,6 @@ const fixtureHome = "/p/integration-fixture";
test("live threads, compiler views, regions, per-plugin reload and error containment", async ({
page,
context,
request,
}) => {
await page.goto(fixtureHome);
await connected(page);
Expand Down Expand Up @@ -54,27 +53,18 @@ test("live threads, compiler views, regions, per-plugin reload and error contain
}, sentinel);
const source = `${process.env.TOKAMAK_FIXTURE_SOURCE}/src/client/api.tsx`;
const revision = `Fixture revision ${crypto.randomUUID()}`;
await writeFile(source, (await readFile(source, "utf8")).replace(/Fixture revision [^<]+/, revision));
const built = spawnSync(
"proto",
["run", "bun", "--", "packages/build/src/build.ts", process.env.TOKAMAK_FIXTURE_SOURCE!],
{ stdio: "inherit" },
// The new contribution name proves the other window applied the catalog.
const throwingTitle = `ThrowingView${crypto.randomUUID().replaceAll("-", "")}`;
await writeFile(
source,
(await readFile(source, "utf8"))
.replace(/Fixture revision [^<]+/, revision)
.replace(/export const ThrowingView\w* = view/, `export const ${throwingTitle} = view`),
);
expect(built.status).toBe(0);
await cp(`${process.env.TOKAMAK_FIXTURE_SOURCE}/dist`, `${process.env.TOKAMAK_PLUGIN_DIR}/fixture/dist`, {
recursive: true,
const reload = spawnSync("proto", ["run", "bun", "--", "app/scripts/integration-fixture.ts", "--reload"], {
encoding: "utf8",
});
const manifestPath = `${process.env.TOKAMAK_PLUGIN_DIR}/fixture/dist/manifest.json`;
const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
// This link shows that the other window has applied the new catalog.
const throwingTitle = `ThrowingView ${crypto.randomUUID()}`;
manifest.plugin.contributes.view.find((view: { id: string }) => view.id === "ThrowingView").title =
throwingTitle;
await writeFile(manifestPath, JSON.stringify(manifest));
const reload = await request.post("http://127.0.0.1:43123/plugins/integration-fixture/reload", {
headers: { Authorization: `Bearer ${process.env.VITE_TOKAMAK_TOKEN}` },
});
expect(reload.ok()).toBe(true);
expect(reload.status, reload.stderr + reload.stdout).toBe(0);
await expect(fixture.getByRole("heading", { name: revision, exact: true })).toBeVisible();
expect(
await page.evaluate(() => (window as unknown as { documentSentinel: string }).documentSentinel),
Expand Down
28 changes: 19 additions & 9 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ proto run bun -- run --silent tokamak plugin build /path/to/my-plugin --json
proto run bun -- run --silent tokamak plugin add /path/to/my-plugin
proto run bun -- run --silent tokamak plugin list --json
proto run bun -- run --silent tokamak plugin remove my-plugin
proto run bun -- run --silent tokamak plugin reload my-plugin --json
```

`build` runs the artifact build once, defaults to the current folder, and
doesn't need a server. `add` reads the plugin's static identity from `plugin.ts`
(or the build's `package.json` convention), and doesn't build it. `add` and
`remove` only edit the index; the local server picks up membership changes on
its next start. Both commands serialize index updates with an exclusive owner
`remove` edit the index. Run `tokamak plugin reload [id]` to build and apply indexed plugins, or omit the id to also apply removals. Both commands serialize index updates with an exclusive owner
file: a dead owner can be removed, and a live owner is never displaced. An
update that cannot get the lock within 15 seconds fails. Indexed builds must use
`<folder>/dist`; both build and add reject a Vite configuration that changes
Expand Down Expand Up @@ -46,20 +46,30 @@ With `--json`, stdout contains one JSON value and human text goes to stderr.
Without it, normal output goes to stdout and errors go to stderr. Exit status is zero on success and
nonzero on failure. The CLI never prompts. Success has `ok: true` and:

| Command | Additional result fields |
| --------------- | -------------------------------------------------------------- |
| `plugin build` | `id`, `outputFolder` |
| `plugin add` | `id`, `folder`, `restartRequired: true`, `precedence: "index"` |
| `plugin remove` | `id`, `restartRequired: true` |
| `plugin list` | `plugins`: an array of `{ id, version, fromIndex }` |
| `--help` | `help` |
| Command | Additional result fields |
| --------------- | ------------------------------------------------------------- |
| `plugin build` | `id`, `outputFolder` |
| `plugin add` | `id`, `folder`, `reloadRequired: true`, `precedence: "index"` |
| `plugin remove` | `id`, `reloadRequired: true` |
| `plugin list` | `plugins`: an array of `{ id, version, fromIndex }` |
| `--help` | `help` |

Failures return `{ ok: false, error: { message, next } }`. Artifact build and
configuration failures include `error.diagnostics`, an array with a full
`message` and, when available, `file` (absolute), `line`, `column`, and `code`.
Import failures can point to a bundled dependency. Lockfile disagreements point
to the lockfile.

Reload returns `plugins`, `removed` (ids removed from the running index), and
`skipped` (a map of unrequested plugin ids to reasons). Each plugin result has
`id`, `buildId` (SHA-256 of the built manifest, or null when no build was made),
and `status`: `loaded`, `unchanged`, `build-failed`, `load-failed`, or
`unknown`. Failed or uncertain entries carry `error.message` and may include
`error.diagnostics`. The CLI locks each source folder through build and
submission. Any unrelated installed manifest errors found after a removal are
listed under `warnings` without failing the command. If a reload reply is lost, it asks the server which build is
running; when that cannot be confirmed, run `tokamak plugin list`.

To add a command, create a file under `src/commands/` that satisfies `Command`,
then register it in `src/commands.ts`. The entry point handles flags, help,
output and exit codes. The CLI test walks the registry to check that contract.
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class CommandError extends Error {
message: string,
readonly next: string,
readonly diagnostics?: object[],
readonly data?: object,
) {
super(message);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import build from "./commands/build";
import add from "./commands/add";
import remove from "./commands/remove";
import list from "./commands/list";
import reload from "./commands/reload";

export const commands: readonly Command[] = [build, add, remove, list];
export const commands: readonly Command[] = [build, add, remove, list, reload];
4 changes: 2 additions & 2 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export default {
return true;
});
return {
data: { id, folder, restartRequired: true, precedence: "index" },
text: `Indexed ${id} at ${folder}. The local server picks this up at its next start. The indexed build takes precedence over any ${id} from --plugins.`,
data: { id, folder, reloadRequired: true, precedence: "index" },
text: `Indexed ${id} at ${folder}. Run tokamak plugin reload ${id} to apply it now. The indexed build takes precedence over any ${id} from --plugins.`,
};
},
} satisfies Command;
44 changes: 29 additions & 15 deletions packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
import { readFile } from "node:fs/promises";
import { readFile, realpath } from "node:fs/promises";
import { resolve } from "node:path";
import { CommandError, type Command } from "../command";
import { runBuilder } from "../plugin-build";
import { acquireLock } from "../plugin-index";

export default {
path: "plugin build",
summary: "Build a plugin once.",
args: { name: "folder", optional: true },
example: "tokamak plugin build . --json",
async run([folder]) {
const { outputFolder } = await runBuilder(resolve(folder ?? "."), "--result-json");
if (!outputFolder) throw new Error("The build did not report its output folder");
const id = await readFile(resolve(outputFolder, "manifest.json"), "utf8")
.then((source) => {
const id: unknown = JSON.parse(source)?.plugin?.id;
if (typeof id !== "string" || !id) throw new Error("The build manifest has no plugin id");
return id;
})
.catch((error: Error) => {
throw new CommandError("Plugin build failed", "tokamak plugin build --help", [
{ message: `Could not read the build manifest: ${error.message}` },
]);
});
return { data: { id, outputFolder }, text: `Built ${id}: ${outputFolder}` };
const root = await realpath(resolve(folder ?? ".")).catch(() => resolve(folder ?? "."));
// Let the builder report missing folders using its structured diagnostics.
const release = await acquireLock(root, ".tokamak-build").catch((error) => {
if (error.code !== "ENOENT") throw error;
});
try {
return await buildInFolder(root);
} finally {
await release?.();
}
},
} satisfies Command;

export async function buildInFolder(folder: string) {
const { outputFolder } = await runBuilder(folder, "--result-json");
if (!outputFolder) throw new Error("The build did not report its output folder");
const id = await readFile(resolve(outputFolder, "manifest.json"), "utf8")
.then((source) => {
const id: unknown = JSON.parse(source)?.plugin?.id;
if (typeof id !== "string" || !id) throw new Error("The build manifest has no plugin id");
return id;
})
.catch((error: Error) => {
throw new CommandError("Plugin build failed", "tokamak plugin build --help", [
{ message: `Could not read the build manifest: ${error.message}` },
]);
});
return { data: { id, outputFolder }, text: `Built ${id}: ${outputFolder}` };
}
Loading