mirrorbook 0.2.0 → 0.4.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.
Files changed (3) hide show
  1. package/SKILL.md +1 -1
  2. package/dist/cli.js +145 -12
  3. package/package.json +1 -1
package/SKILL.md CHANGED
@@ -32,4 +32,4 @@ Password protection: `--password <pw> [--expires 7d|12h|2026-12-31|never]` on `d
32
32
 
33
33
  Install this skill for Claude Code with `npx mirrorbook skill --install` (or `--dir ./.claude/skills` for one project); `mirrorbook skill` prints it.
34
34
 
35
- Other commands: `access <slug>`, `ls`, `status <slug>` (lists versions), `rollback <slug> <version>` (alias `set-default`; version is `3`, `#3`, or a deployment id), `rm-version <slug> <version> --yes`, `rm <slug> --yes`, `whoami`. Add `--json` to any of them.
35
+ Other commands: `pull <slug> [version] [-o file.zip]` (download a version's files as a zip; owner only, defaults to the live version), `access <slug>`, `ls`, `status <slug>` (lists versions), `rollback <slug> <version>` (alias `set-default`; version is `3`, `#3`, or a deployment id), `rm-version <slug> <version> --yes`, `rm <slug> --yes`, `whoami`. Add `--json` to any of them.
package/dist/cli.js CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  // src/cli.ts
4
4
  import { spawn } from "child_process";
5
- import { cp, mkdir as mkdir2, readFile as readFile3 } from "fs/promises";
6
- import { homedir as homedir2, hostname, platform } from "os";
7
- import path3 from "path";
8
- import { fileURLToPath } from "url";
5
+ import { cp, mkdir as mkdir3, readFile as readFile4, writeFile as writeFile3 } from "fs/promises";
6
+ import { homedir as homedir3, hostname, platform } from "os";
7
+ import path4 from "path";
8
+ import { fileURLToPath as fileURLToPath2 } from "url";
9
9
  import { createInterface } from "readline/promises";
10
10
  import { Command, Option } from "commander";
11
11
 
@@ -108,7 +108,24 @@ function createClient(apiUrl, token) {
108
108
  }
109
109
  return json;
110
110
  }
111
+ async function download(p) {
112
+ const headers = { "user-agent": "mirrorbook-cli/0.1" };
113
+ if (token) headers.authorization = `Bearer ${token}`;
114
+ let res;
115
+ try {
116
+ res = await fetch(`${apiUrl}/api${p}`, { headers });
117
+ } catch (e) {
118
+ throw new CliError("network", `Can't reach ${apiUrl}: ${e.message}`, 1);
119
+ }
120
+ if (!res.ok) {
121
+ const json = await res.json().catch(() => ({}));
122
+ const code = json.error?.code ?? "http_error";
123
+ throw new CliError(code, json.error?.message ?? `HTTP ${res.status}`, exitFor(res.status, code), res.status);
124
+ }
125
+ return Buffer.from(await res.arrayBuffer());
126
+ }
111
127
  return {
128
+ download: (slug, id) => download(`/sites/${encodeURIComponent(slug)}/deployments/${encodeURIComponent(id)}/download`),
112
129
  me: () => call("GET", "/me"),
113
130
  deviceStart: (clientName) => call(
114
131
  "POST",
@@ -220,10 +237,16 @@ async function pack(input, format) {
220
237
  return { blob: new Blob([buf]), name: path2.basename(input), format: f };
221
238
  }
222
239
 
240
+ // src/update.ts
241
+ import { readFile as readFile3, mkdir as mkdir2, writeFile as writeFile2 } from "fs/promises";
242
+ import { homedir as homedir2 } from "os";
243
+ import path3 from "path";
244
+ import { fileURLToPath } from "url";
245
+
223
246
  // package.json
224
247
  var package_default = {
225
248
  name: "mirrorbook",
226
- version: "0.1.2",
249
+ version: "0.4.0",
227
250
  description: "Publish HTML to a shareable URL. Agent-friendly: non-interactive, --json output, stable exit codes.",
228
251
  type: "module",
229
252
  bin: {
@@ -240,7 +263,8 @@ var package_default = {
240
263
  scripts: {
241
264
  dev: "tsx src/cli.ts",
242
265
  build: "tsup",
243
- typecheck: "tsc -p ."
266
+ typecheck: "tsc -p .",
267
+ prepack: "tsup"
244
268
  },
245
269
  devDependencies: {
246
270
  "@mirrorbook/shared": "workspace:*"
@@ -251,6 +275,93 @@ var package_default = {
251
275
  }
252
276
  };
253
277
 
278
+ // src/update.ts
279
+ var CACHE = path3.join(path3.dirname(configPath), "update.json");
280
+ var DAY = 24 * 60 * 60 * 1e3;
281
+ var UPDATE_COMMAND = "npm i -g mirrorbook@latest";
282
+ var SKILL_COMMAND = "mirrorbook skill --install";
283
+ function isNewer(latest, current) {
284
+ const n = (v) => (v.split("-")[0] ?? "").split(".").map((x) => Number.parseInt(x, 10) || 0);
285
+ const [a, b] = [n(latest), n(current)];
286
+ for (let i = 0; i < 3; i++) if ((a[i] ?? 0) !== (b[i] ?? 0)) return (a[i] ?? 0) > (b[i] ?? 0);
287
+ return false;
288
+ }
289
+ async function readCache() {
290
+ try {
291
+ return JSON.parse(await readFile3(CACHE, "utf8"));
292
+ } catch {
293
+ return void 0;
294
+ }
295
+ }
296
+ async function writeCache(c) {
297
+ try {
298
+ await mkdir2(path3.dirname(CACHE), { recursive: true, mode: 448 });
299
+ await writeFile2(CACHE, JSON.stringify(c));
300
+ } catch {
301
+ }
302
+ }
303
+ async function fetchLatest(timeoutMs) {
304
+ try {
305
+ const res = await fetch("https://registry.npmjs.org/mirrorbook/latest", { signal: AbortSignal.timeout(timeoutMs) });
306
+ if (!res.ok) return void 0;
307
+ return (await res.json()).version;
308
+ } catch {
309
+ return void 0;
310
+ }
311
+ }
312
+ async function staleSkills() {
313
+ let bundled;
314
+ try {
315
+ bundled = await readFile3(fileURLToPath(new URL("../SKILL.md", import.meta.url)), "utf8");
316
+ } catch {
317
+ return { installed: [], stale: [] };
318
+ }
319
+ const candidates = [path3.join(homedir2(), ".claude", "skills", "mirrorbook", "SKILL.md"), path3.resolve(".claude", "skills", "mirrorbook", "SKILL.md")];
320
+ const installed = [];
321
+ const stale = [];
322
+ for (const p of candidates) {
323
+ try {
324
+ const text = await readFile3(p, "utf8");
325
+ installed.push(p);
326
+ if (text !== bundled) stale.push(p);
327
+ } catch {
328
+ }
329
+ }
330
+ return { installed, stale };
331
+ }
332
+ async function checkUpdate() {
333
+ const latest = await fetchLatest(5e3);
334
+ if (latest) await writeCache({ ...await readCache(), checkedAt: Date.now(), latest });
335
+ const skill = await staleSkills();
336
+ return {
337
+ current: package_default.version,
338
+ latest: latest ?? null,
339
+ updateAvailable: latest ? isNewer(latest, package_default.version) : false,
340
+ updateCommand: UPDATE_COMMAND,
341
+ skill: { installed: skill.installed, stale: skill.stale, updateCommand: SKILL_COMMAND },
342
+ error: latest ? null : "could not reach the npm registry"
343
+ };
344
+ }
345
+ async function passiveNotice() {
346
+ if (process.env.MIRRORBOOK_NO_UPDATE_CHECK === "1") return;
347
+ const now = Date.now();
348
+ let cache = await readCache() ?? { checkedAt: 0 };
349
+ if (now - cache.checkedAt > DAY) {
350
+ const latest = await fetchLatest(1500);
351
+ cache = { ...cache, checkedAt: now, latest: latest ?? cache.latest };
352
+ await writeCache(cache);
353
+ }
354
+ const lines = [];
355
+ if (cache.latest && isNewer(cache.latest, package_default.version)) lines.push(`mirrorbook ${cache.latest} is available (you have ${package_default.version}). Update: ${UPDATE_COMMAND}`);
356
+ const { stale } = await staleSkills();
357
+ if (stale.length) lines.push(`The installed mirrorbook skill is out of date (${stale.join(", ")}). Refresh: ${SKILL_COMMAND}`);
358
+ if (!lines.length || cache.notifiedAt && now - cache.notifiedAt < DAY) return;
359
+ await writeCache({ ...cache, notifiedAt: now });
360
+ process.stderr.write(`
361
+ ${lines.map((l) => `notice: ${l}`).join("\n")}
362
+ `);
363
+ }
364
+
254
365
  // src/cli.ts
255
366
  var program = new Command().name("mirrorbook").description("Publish HTML to a shareable URL on mirrorbook.mixdao.world").version(package_default.version).addOption(new Option("--json", "print one JSON object to stdout (also MIRRORBOOK_JSON=1)").default(process.env.MIRRORBOOK_JSON === "1")).option("-n, --server <name>", "use a saved server for this command (env MIRRORBOOK_SERVER); see `mirrorbook servers`").option("--api-url <url>", "server URL, overrides the saved one (env MIRRORBOOK_API_URL)").option("--token <token>", "API token, overrides the saved one (env MIRRORBOOK_TOKEN)").showHelpAfterError().addHelpText(
256
367
  "after",
@@ -261,6 +372,7 @@ Recommended workflow:
261
372
  Share privately deploy <path> --slug <slug> --password <pw> --expires 7d (or: access <slug> ...)
262
373
  Went wrong status <slug> \u2192 rollback <slug> <version>
263
374
  Use with AI agents mirrorbook skill --install (installs the usage guide into Claude Code)
375
+ After upgrading mirrorbook update (new CLI version? skill out of date? prints the fix commands)
264
376
 
265
377
  Always run \`mirrorbook ls\` before picking a slug: re-deploying an existing slug publishes a new
266
378
  version of that site, a new slug creates a new site.
@@ -401,17 +513,27 @@ program.command("use").description("Switch the current server").argument("<name>
401
513
  out(`Now using ${name} (${cfg.servers[name].url})`, { ok: true, server: name, url: cfg.servers[name].url });
402
514
  });
403
515
  program.command("skill").description("Print the agent skill (SKILL.md) for this CLI, or install it for Claude Code with --install").option("--install", "copy SKILL.md to ~/.claude/skills/mirrorbook/ (Claude Code loads it automatically)").option("--dir <dir>", "install into <dir>/mirrorbook/ instead, e.g. ./.claude/skills for this project only").action(async (o) => {
404
- const src = fileURLToPath(new URL("../SKILL.md", import.meta.url));
516
+ const src = fileURLToPath2(new URL("../SKILL.md", import.meta.url));
405
517
  if (!o.install && !o.dir) {
406
- const text = await readFile3(src, "utf8");
518
+ const text = await readFile4(src, "utf8");
407
519
  if (g().json) out("", { path: src, content: text });
408
520
  else process.stdout.write(text);
409
521
  return;
410
522
  }
411
- const dest = path3.join(o.dir ? path3.resolve(o.dir) : path3.join(homedir2(), ".claude", "skills"), "mirrorbook");
412
- await mkdir2(dest, { recursive: true });
413
- await cp(src, path3.join(dest, "SKILL.md"));
414
- out(`Installed skill to ${dest}/SKILL.md. Restart Claude Code to pick it up.`, { ok: true, path: path3.join(dest, "SKILL.md") });
523
+ const dest = path4.join(o.dir ? path4.resolve(o.dir) : path4.join(homedir3(), ".claude", "skills"), "mirrorbook");
524
+ await mkdir3(dest, { recursive: true });
525
+ await cp(src, path4.join(dest, "SKILL.md"));
526
+ out(`Installed skill to ${dest}/SKILL.md. Restart Claude Code to pick it up.`, { ok: true, path: path4.join(dest, "SKILL.md") });
527
+ });
528
+ program.command("update").description("Check npm for a newer mirrorbook and whether the installed Claude Code skill is out of date").action(async () => {
529
+ const r = await checkUpdate();
530
+ const lines = [`mirrorbook ${r.current}${r.latest ? ` (latest ${r.latest})` : ""}`];
531
+ if (r.error) lines.push(`! ${r.error}`);
532
+ else lines.push(r.updateAvailable ? `Update available: ${r.updateCommand}` : "Up to date");
533
+ if (r.skill.stale.length) lines.push(`Skill out of date: ${r.skill.stale.join(", ")} \u2192 ${r.skill.updateCommand}`);
534
+ else if (r.skill.installed.length) lines.push("Skill up to date");
535
+ else lines.push(`Skill not installed \u2192 ${r.skill.updateCommand}`);
536
+ out(lines.join("\n"), r);
415
537
  });
416
538
  program.command("whoami").description("Show the current server and account").action(async () => {
417
539
  const { name, apiUrl } = await auth();
@@ -504,6 +626,16 @@ program.command("rollback").alias("set-default").description("Make an approved v
504
626
  await api.rollback(slug, d.id);
505
627
  out(`${slug} now serves #${d.number}`, { ok: true, slug, deploymentId: d.id, number: d.number });
506
628
  });
629
+ program.command("pull").description("Download a version's files as a zip (owner only; defaults to the live version)").argument("<slug>").argument("[version]", "version number (3 or #3) or deployment id; default: the live version").option("-o, --output <file>", "where to save the zip (default: ./<slug>-v<N>.zip)").action(async (slug, ref, o) => {
630
+ const api = await client();
631
+ const { site, deployments } = await api.site(slug);
632
+ const d = ref ? await resolveVersion(api, slug, ref) : deployments.find((x) => x.id === site.activeDeploymentId);
633
+ if (!d) throw new CliError("not_found", `${slug} has no live version. Pass a version number`, EXIT.usage);
634
+ const file = path4.resolve(o.output ?? `${slug}-v${d.number}.zip`);
635
+ const buf = await api.download(slug, d.id);
636
+ await writeFile3(file, buf);
637
+ out(`Saved ${slug} #${d.number} to ${file} (${buf.length} bytes)`, { ok: true, slug, deploymentId: d.id, number: d.number, file, bytes: buf.length });
638
+ });
507
639
  program.command("rm-version").description("Delete one version (not the default one)").argument("<slug>").argument("<version>", "version number (3 or #3) or deployment id").option("-y, --yes", "skip the confirmation (required when not a TTY)").action(async (slug, ref, o) => {
508
640
  const api = await client();
509
641
  const d = await resolveVersion(api, slug, ref);
@@ -530,6 +662,7 @@ program.command("rm").description("Delete a site and all its versions").argument
530
662
  });
531
663
  try {
532
664
  await program.parseAsync();
665
+ if (process.argv[2] !== "update") await passiveNotice();
533
666
  } catch (e) {
534
667
  const err = e instanceof CliError ? e : new CliError("internal", e.message, EXIT.error);
535
668
  if (g().json) process.stdout.write(`${JSON.stringify({ error: { code: err.code, message: err.message } })}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mirrorbook",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Publish HTML to a shareable URL. Agent-friendly: non-interactive, --json output, stable exit codes.",
5
5
  "type": "module",
6
6
  "bin": {