@fluid-app/fluid-cli-theme-dev 0.1.304 → 0.1.305

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.
package/dist/index.mjs CHANGED
@@ -1634,6 +1634,14 @@ var ThemeRoot = class {
1634
1634
  files() {
1635
1635
  return this.glob(this.root).filter((f) => isThemeResourceKey(f.relativePath) && !this.ignore.ignore(f.relativePath));
1636
1636
  }
1637
+ /**
1638
+ * Files attached to a template (`<type>/<name>/assets/…`). They are not
1639
+ * resource keys, so files() leaves them out; callers that cannot carry
1640
+ * them use this to say so rather than drop them silently.
1641
+ */
1642
+ templateAttachedFiles() {
1643
+ return this.glob(this.root).filter((f) => /^[^/]+\/.+\/assets\//.test(f.relativePath) && !this.ignore.ignore(f.relativePath));
1644
+ }
1637
1645
  isResourcePath(pathOrFile) {
1638
1646
  return isThemeResourceKey(this.file(pathOrFile).relativePath);
1639
1647
  }
@@ -12289,6 +12297,43 @@ function requestStaging(api, themeId, key) {
12289
12297
  async function readOperation(api, themeId, operationId) {
12290
12298
  return (await call(api.get(`${base(themeId)}/branch-operations/${operationId}`))).operation;
12291
12299
  }
12300
+ function declarePublication(api, themeId, slug, key, expectedRevision, entries) {
12301
+ return call(api.request(`${base(themeId)}/branches/${slug}/publications`, {
12302
+ method: "POST",
12303
+ headers: { "X-Operation-Key": key },
12304
+ body: {
12305
+ expected_revision: expectedRevision,
12306
+ entries
12307
+ }
12308
+ }));
12309
+ }
12310
+ /**
12311
+ * Upload one declared content as raw bytes. The shared client always sends
12312
+ * JSON, so this goes through fetch with the same base URL and token.
12313
+ */
12314
+ async function uploadContent(themeId, operationId, sha256, content) {
12315
+ const response = await fetch(`${getApiBase()}${base(themeId)}/branch-operations/${operationId}/content/${sha256}`, {
12316
+ method: "PUT",
12317
+ headers: {
12318
+ Authorization: `Bearer ${getAuthToken() ?? ""}`,
12319
+ "Content-Type": "application/octet-stream",
12320
+ Accept: "application/json"
12321
+ },
12322
+ body: new Uint8Array(content)
12323
+ });
12324
+ const text = await response.text();
12325
+ const parsed = text ? JSON.parse(text) : null;
12326
+ if (response.ok) return parsed;
12327
+ const envelope = parsed;
12328
+ const details = envelope?.error?.details ?? {};
12329
+ throw new BranchApiError(envelope?.error?.message ?? `Upload failed with HTTP ${response.status}`, response.status, typeof details["type"] === "string" ? details["type"] : null, details, response.headers.get("x-request-id") ?? void 0);
12330
+ }
12331
+ function applyPublication(api, themeId, operationId, expectedRevision) {
12332
+ return call(api.request(`${base(themeId)}/branch-operations/${operationId}/publication`, {
12333
+ method: "POST",
12334
+ body: { expected_revision: expectedRevision }
12335
+ }));
12336
+ }
12292
12337
  //#endregion
12293
12338
  //#region src/theme/staging.ts
12294
12339
  /**
@@ -12345,26 +12390,111 @@ function describeStaging(status) {
12345
12390
  return "No staging yet; request it with `fluid theme staging request`.";
12346
12391
  }
12347
12392
  //#endregion
12393
+ //#region src/theme/staging-publish.ts
12394
+ /**
12395
+ * Publish local theme files to the theme's staging branch.
12396
+ *
12397
+ * Every local file is declared by SHA-256; the server answers which digests
12398
+ * it has not stored yet, only those are uploaded, and the publication is then
12399
+ * applied in one step, so a staging viewer sees all of it or none. Paths the
12400
+ * branch planner refuses by design are left out and reported, never sent.
12401
+ * Files deleted locally stay on staging: a manifest changes only the paths
12402
+ * it names.
12403
+ */
12404
+ const MAX_ENTRIES = 5e3;
12405
+ /** Why the branch planner refuses this path, or null when it accepts it. */
12406
+ function unpublishableReason(path) {
12407
+ if (path === "members_sites.json") return "member configuration is not published to branches yet";
12408
+ if (isRetiredTemplateKey(path)) return "account and member pages are not theme files";
12409
+ if (/~\d+(\/|$)/.test(path)) return "numbered duplicate template; rename it first";
12410
+ if (/^[^/]+\/.+\/assets\//.test(path)) return "files attached to a template are not published to branches yet";
12411
+ return null;
12412
+ }
12413
+ function planPublication(root) {
12414
+ const entries = [];
12415
+ const contents = /* @__PURE__ */ new Map();
12416
+ const skipped = [];
12417
+ for (const file of root.templateAttachedFiles()) skipped.push({
12418
+ path: file.relativePath,
12419
+ reason: "files attached to a template are not published to branches yet"
12420
+ });
12421
+ for (const file of root.files()) {
12422
+ const path = file.relativePath;
12423
+ const reason = unpublishableReason(path);
12424
+ if (reason) {
12425
+ skipped.push({
12426
+ path,
12427
+ reason
12428
+ });
12429
+ continue;
12430
+ }
12431
+ const bytes = file.isText ? Buffer.from(file.read(), "utf-8") : file.readBinary();
12432
+ const sha256 = createHash("sha256").update(bytes).digest("hex");
12433
+ entries.push({
12434
+ path,
12435
+ kind: file.isText ? "text" : "binary",
12436
+ sha256,
12437
+ byte_size: bytes.byteLength
12438
+ });
12439
+ contents.set(sha256, bytes);
12440
+ }
12441
+ return {
12442
+ entries,
12443
+ contents,
12444
+ skipped
12445
+ };
12446
+ }
12447
+ async function publishToStaging(api, themeId, root, onProgress = () => {}) {
12448
+ const branch = (await readStagingStatus(api, themeId)).branch;
12449
+ if (branch?.state !== "ready") throw new Error("Staging is not ready. Run `fluid theme staging request` and wait for it first.");
12450
+ const plan = planPublication(root);
12451
+ if (plan.entries.length === 0) throw new Error("No theme files to publish.");
12452
+ if (plan.entries.length > 5e3) throw new Error(`A publication holds at most ${MAX_ENTRIES} files; this theme has ${plan.entries.length}.`);
12453
+ onProgress(`Declaring ${plan.entries.length} files…`);
12454
+ const declared = await declarePublication(api, themeId, branch.slug, operationKey("cli-publish"), branch.revision, plan.entries);
12455
+ const missing = declared.publication.missing_digests;
12456
+ for (const [index, sha256] of missing.entries()) {
12457
+ onProgress(`Uploading ${index + 1} of ${missing.length}…`);
12458
+ const content = plan.contents.get(sha256);
12459
+ if (!content) throw new Error(`The server asked for content this publication did not declare (${sha256}).`);
12460
+ await uploadContent(themeId, declared.operation.id, sha256, content);
12461
+ }
12462
+ onProgress("Applying to staging…");
12463
+ return {
12464
+ branch: (await applyPublication(api, themeId, declared.operation.id, branch.revision)).branch,
12465
+ published: plan.entries.length,
12466
+ uploaded: missing.length,
12467
+ skipped: plan.skipped
12468
+ };
12469
+ }
12470
+ //#endregion
12348
12471
  //#region src/commands/staging.ts
12349
12472
  /**
12350
12473
  * The production theme staging belongs to: --theme, else the theme this
12351
12474
  * project is bound to in .fluid-theme.json.
12352
12475
  */
12353
12476
  function resolveThemeId(opts) {
12477
+ const root = resolveRoot(opts.root);
12354
12478
  if (opts.theme) {
12355
12479
  const id = Number(opts.theme);
12356
12480
  if (!Number.isInteger(id) || id <= 0) throw new Error(`--theme must be a theme ID, got "${opts.theme}".`);
12357
12481
  return id;
12358
12482
  }
12359
- let root = opts.root;
12360
- if (root === ".") {
12361
- const workspace = findWorkspace();
12362
- if (workspace) root = resolveThemeRootFromCwd(workspace) ?? root;
12363
- }
12364
12483
  const config = readThemeConfig(root);
12365
12484
  if (!config) throw new Error("No .fluid-theme.json here. Run `fluid theme pull` to bind this project to a theme, or pass --theme <id>.");
12366
12485
  return config.themeId;
12367
12486
  }
12487
+ function resolveRoot(root) {
12488
+ if (root !== ".") return root;
12489
+ const workspace = findWorkspace();
12490
+ return (workspace && resolveThemeRootFromCwd(workspace)) ?? root;
12491
+ }
12492
+ /** The refusal's message plus any per-path errors the server listed. */
12493
+ function refusalMessage(error) {
12494
+ const message = error.message;
12495
+ if (!(error instanceof BranchApiError)) return message;
12496
+ return [message, ...(Array.isArray(error.details["errors"]) ? error.details["errors"] : []).map((line) => ` - ${line}`)].join("\n");
12497
+ }
12368
12498
  /** JSON shape Desktop reads; stable across CLI versions. */
12369
12499
  function stagingJson(status) {
12370
12500
  return {
@@ -12382,16 +12512,18 @@ async function run(opts, action) {
12382
12512
  status: error.status,
12383
12513
  type: error.type,
12384
12514
  message: error.message,
12515
+ errors: Array.isArray(error.details["errors"]) ? error.details["errors"] : [],
12385
12516
  request_id: error.requestId ?? null
12386
12517
  } : {
12387
12518
  status: null,
12388
12519
  type: null,
12389
12520
  message: error.message,
12521
+ errors: [],
12390
12522
  request_id: null
12391
12523
  };
12392
12524
  console.log(JSON.stringify({ error: refusal }));
12393
- console.error(error.message);
12394
- } else console.error(chalk.red(error.message));
12525
+ console.error(refusalMessage(error));
12526
+ } else console.error(chalk.red(refusalMessage(error)));
12395
12527
  process.exitCode = 1;
12396
12528
  }
12397
12529
  }
@@ -12422,7 +12554,20 @@ function createStagingCommand() {
12422
12554
  report(settled, opts.json);
12423
12555
  if (settled.branch?.state !== "ready") process.exitCode = 1;
12424
12556
  }));
12425
- return new Command("staging").description("Use the theme's staging branch, previewed by a signed link (needs branch environments)").addCommand(status).addCommand(request).addCommand(wait);
12557
+ const publish = withThemeOptions(new Command("publish").description("Publish this directory's theme files to staging; production is not touched")).action((opts) => run(opts, async () => {
12558
+ requireToken();
12559
+ const root = new ThemeRoot(resolveRoot(opts.root));
12560
+ if (!root.isValid()) throw new Error(`'${root.root}' does not look like a theme directory.`);
12561
+ const outcome = await publishToStaging(createApiClient(), resolveThemeId(opts), root, opts.json ? void 0 : (message) => console.error(chalk.dim(message)));
12562
+ if (opts.json) {
12563
+ console.log(JSON.stringify(outcome));
12564
+ return;
12565
+ }
12566
+ console.log(`Published ${outcome.published} files to staging (revision ${outcome.branch.revision}, ${outcome.uploaded} uploaded).`);
12567
+ for (const { path, reason } of outcome.skipped) console.log(chalk.yellow(` skipped ${path}: ${reason}`));
12568
+ if (outcome.branch.preview_url) console.log(`Preview: ${chalk.cyan(outcome.branch.preview_url)}`);
12569
+ }));
12570
+ return new Command("staging").description("Use the theme's staging branch, previewed by a signed link (needs branch environments)").addCommand(status).addCommand(request).addCommand(wait).addCommand(publish);
12426
12571
  }
12427
12572
  //#endregion
12428
12573
  //#region src/commands/theme.ts