@fluid-app/fluid-cli-theme-dev 0.1.869 → 0.1.871

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
@@ -5092,7 +5092,7 @@ async function startDevServer(api, theme, themeRoot, opts, onReady) {
5092
5092
  }
5093
5093
  //#endregion
5094
5094
  //#region src/theme/dev-remote-baseline.ts
5095
- const BASELINE_VERSION = 1;
5095
+ const BASELINE_VERSION$1 = 1;
5096
5096
  const BASELINE_FILE = join(".fluid-theme", "dev-baseline.json");
5097
5097
  function readDevRemoteBaseline(themeRoot, themeId, assetManifestSha) {
5098
5098
  try {
@@ -5108,7 +5108,7 @@ function readDevRemoteBaseline(themeRoot, themeId, assetManifestSha) {
5108
5108
  }
5109
5109
  function writeDevRemoteBaseline(themeRoot, state) {
5110
5110
  const document = {
5111
- version: BASELINE_VERSION,
5111
+ version: BASELINE_VERSION$1,
5112
5112
  ...state
5113
5113
  };
5114
5114
  parseDocument(document);
@@ -5169,7 +5169,7 @@ async function devRemoteStateFromSourceShadow(themeRoot, shadow, themeId, remote
5169
5169
  }
5170
5170
  }
5171
5171
  function parseDocument(document) {
5172
- if (!isRecord$1(document) || document["version"] !== BASELINE_VERSION) throw new Error(`expected version ${BASELINE_VERSION}`);
5172
+ if (!isRecord$1(document) || document["version"] !== BASELINE_VERSION$1) throw new Error(`expected version ${BASELINE_VERSION$1}`);
5173
5173
  const themeId = document["themeId"];
5174
5174
  const remoteSha = document["remoteSha"];
5175
5175
  const assetManifestSha = document["assetManifestSha"];
@@ -5948,6 +5948,69 @@ function createDevCommand() {
5948
5948
  });
5949
5949
  }
5950
5950
  //#endregion
5951
+ //#region src/theme/staging-baseline.ts
5952
+ /**
5953
+ * What staging held the last time this folder was in step with it: after
5954
+ * this folder published to staging, or after a comparison found the folder
5955
+ * and staging the same. A later publish compares staging with this record
5956
+ * to learn which files someone else published since, so it can refuse to
5957
+ * replace them (see publishToStaging).
5958
+ *
5959
+ * Files are recorded as a comparison declares them (see compareWithStaging):
5960
+ * by digest, plus the text of the member configuration and the files it
5961
+ * names a menu in, which staging rewrites with its own menu copies. The
5962
+ * record lives under `.fluid-theme/`, which is never version controlled.
5963
+ */
5964
+ const BASELINE_VERSION = 1;
5965
+ const STAGING_BASELINE_FILE = join(".fluid-theme", "staging-baseline.json");
5966
+ /** Member configuration text kept in a baseline, at most this many bytes. */
5967
+ const MAX_MEMBER_TEXT_BYTES = 2 * 1024 * 1024;
5968
+ function readStagingBaseline(themeRoot, themeId) {
5969
+ try {
5970
+ const raw = readInputText(join(themeRoot, STAGING_BASELINE_FILE));
5971
+ if (raw === null) return null;
5972
+ const parsed = JSON.parse(raw);
5973
+ if (parsed.version !== BASELINE_VERSION) return null;
5974
+ if (parsed.themeId !== themeId) return null;
5975
+ if (!Number.isInteger(parsed.revision)) return null;
5976
+ if (!Array.isArray(parsed.entries) || !Array.isArray(parsed.memberTexts)) return null;
5977
+ return {
5978
+ themeId,
5979
+ revision: parsed.revision,
5980
+ entries: parsed.entries,
5981
+ memberTexts: parsed.memberTexts
5982
+ };
5983
+ } catch {
5984
+ return null;
5985
+ }
5986
+ }
5987
+ /**
5988
+ * Record the folder's step with staging. Member texts beyond the cap are
5989
+ * dropped, so those files are compared by digest instead. A failed write is
5990
+ * ignored: the next publish then runs without the check, as before.
5991
+ */
5992
+ function writeStagingBaseline(themeRoot, baseline) {
5993
+ const size = baseline.memberTexts.reduce((sum, text) => sum + Buffer.byteLength(text.content, "utf-8"), 0);
5994
+ const document = {
5995
+ version: BASELINE_VERSION,
5996
+ ...baseline,
5997
+ memberTexts: size <= MAX_MEMBER_TEXT_BYTES ? baseline.memberTexts : []
5998
+ };
5999
+ const path = join(themeRoot, STAGING_BASELINE_FILE);
6000
+ markTouched(path);
6001
+ const tempPath = `${path}.${randomBytes(6).toString("hex")}.tmp`;
6002
+ try {
6003
+ mkdirSync(dirname(path), { recursive: true });
6004
+ writeFileSync(tempPath, `${JSON.stringify(document)}\n`, {
6005
+ encoding: "utf-8",
6006
+ mode: 384
6007
+ });
6008
+ renameSync(tempPath, path);
6009
+ } catch {
6010
+ rmSync(tempPath, { force: true });
6011
+ }
6012
+ }
6013
+ //#endregion
5951
6014
  //#region src/command-inputs.ts
5952
6015
  /** Resolvers probe which config files exist; those probes are not inputs. */
5953
6016
  function resolver(fn) {
@@ -5979,7 +6042,11 @@ const stagingPublishInputs = resolver((opts) => {
5979
6042
  const { root, workspaceFile } = rootAndWorkspace(opts["root"]);
5980
6043
  return themeInputSpecs(root, {
5981
6044
  tree: true,
5982
- files: [".fluid-theme.json", ".fluidignore"],
6045
+ files: [
6046
+ ".fluid-theme.json",
6047
+ ".fluidignore",
6048
+ STAGING_BASELINE_FILE
6049
+ ],
5983
6050
  workspaceFile
5984
6051
  });
5985
6052
  });
@@ -13881,6 +13948,15 @@ function describeStaging(status) {
13881
13948
  * `members_sites.json` goes with the files, so staging gets the theme's
13882
13949
  * member sites, pages and menus along with their templates; the server
13883
13950
  * imports it with the files it names, as a push does.
13951
+ *
13952
+ * A publish sends the whole folder, so it would quietly put back any file
13953
+ * someone else published since this folder was last in step with staging.
13954
+ * When staging has moved past that point (see staging-baseline.ts), the
13955
+ * files they changed are compared with this folder, and the publish is
13956
+ * refused with a `staging_moved` error naming the ones it would replace,
13957
+ * unless `force` is set. Files they changed that this folder agrees with, or
13958
+ * does not have, are not in the way. A folder with no record yet publishes
13959
+ * as before and is recorded from then on.
13884
13960
  */
13885
13961
  const MAX_ENTRIES = 5e3;
13886
13962
  /** Why the branch planner refuses this path, or null when it accepts it. */
@@ -13943,12 +14019,15 @@ function publishedText(file) {
13943
14019
  const { document, removed } = withoutRetiredTemplates(parsed);
13944
14020
  return removed.length > 0 ? `${JSON.stringify(document, null, 2)}\n` : text;
13945
14021
  }
13946
- async function publishToStaging(api, themeId, root, onProgress = () => {}) {
14022
+ async function publishToStaging(api, themeId, root, onProgress = () => {}, options = {}) {
13947
14023
  const branch = (await readStagingStatus(api, themeId)).branch;
13948
14024
  if (branch?.state !== "ready") throw new Error("Staging is not ready. Run `fluid theme staging request` and wait for it first.");
13949
14025
  const plan = planPublication(root);
13950
14026
  if (plan.entries.length === 0) throw new Error("No theme files to publish.");
13951
14027
  if (plan.entries.length > 5e3) throw new Error(`A publication holds at most ${MAX_ENTRIES} files; this theme has ${plan.entries.length}.`);
14028
+ const comparable = comparisonDeclaration(plan);
14029
+ const reviewed = options.replaceAt !== void 0 && options.replaceAt === branch.revision;
14030
+ if (!options.force && !reviewed) await refuseReplacingOthers(api, themeId, branch, root, comparable, onProgress);
13952
14031
  onProgress(`Declaring ${plan.entries.length} files…`);
13953
14032
  const declared = await declarePublication(api, themeId, branch.slug, operationKey("cli-publish"), branch.revision, plan.entries);
13954
14033
  const missing = declared.publication.missing_digests;
@@ -13959,13 +14038,43 @@ async function publishToStaging(api, themeId, root, onProgress = () => {}) {
13959
14038
  await uploadContent(themeId, declared.operation.id, sha256, content);
13960
14039
  }
13961
14040
  onProgress("Applying to staging…");
14041
+ const applied = await applyPublication(api, themeId, declared.operation.id, branch.revision);
14042
+ writeStagingBaseline(root.root, {
14043
+ themeId,
14044
+ revision: applied.branch.revision,
14045
+ ...comparable
14046
+ });
13962
14047
  return {
13963
- branch: (await applyPublication(api, themeId, declared.operation.id, branch.revision)).branch,
14048
+ branch: applied.branch,
13964
14049
  published: plan.entries.length,
13965
14050
  uploaded: missing.length,
13966
14051
  skipped: plan.skipped
13967
14052
  };
13968
14053
  }
14054
+ /**
14055
+ * Refuse when staging moved past this folder's record and the files that
14056
+ * changed there since differ from this folder's. Throws a `staging_moved`
14057
+ * BranchApiError listing them; returns when nothing would be replaced.
14058
+ */
14059
+ async function refuseReplacingOthers(api, themeId, branch, root, declared, onProgress) {
14060
+ const baseline = readStagingBaseline(root.root, themeId);
14061
+ if (!baseline || baseline.revision === branch.revision) return;
14062
+ onProgress("Checking what changed on staging since you last published…");
14063
+ const since = await compareDeclaration(api, themeId, branch.slug, root, baseline);
14064
+ const theirs = new Set([...since.changed, ...since.branchOnly].map(resourceIdentity));
14065
+ if (theirs.size === 0) return;
14066
+ const local = await compareDeclaration(api, themeId, branch.slug, root, declared);
14067
+ const replaced = local.changed.filter((path) => theirs.has(resourceIdentity(path))).sort();
14068
+ if (replaced.length === 0) return;
14069
+ throw new BranchApiError(`Staging has changes you don't have (revision ${local.revision}). Publishing would replace them. Get those changes first, or publish with --force to replace them.`, 409, "staging_moved", {
14070
+ paths: replaced,
14071
+ revision: local.revision
14072
+ });
14073
+ }
14074
+ /** One theme resource however its path is spelled. */
14075
+ function resourceIdentity(path) {
14076
+ return normalizeThemeResourceKey(path).toLowerCase();
14077
+ }
13969
14078
  //#endregion
13970
14079
  //#region src/theme/staging-compare.ts
13971
14080
  /**
@@ -14009,22 +14118,37 @@ function sorted(value) {
14009
14118
  async function compareWithStaging(api, themeId, root) {
14010
14119
  const branch = (await readStagingStatus(api, themeId)).branch;
14011
14120
  if (branch?.state !== "ready") throw new Error("Staging is not ready. Run `fluid theme staging request` and wait for it first.");
14012
- const plan = planPublication(root);
14013
- const entries = plan.entries.flatMap((entry) => {
14014
- if (!("sha256" in entry)) return [];
14015
- if (entry.path !== "members_sites.json") return [{
14016
- path: entry.path,
14017
- sha256: entry.sha256
14018
- }];
14019
- const sent = plan.contents.get(entry.sha256);
14020
- const digest = sent && jsonDigest(Buffer.from(sent).toString("utf-8"));
14021
- return [{
14022
- path: entry.path,
14023
- sha256: digest ?? entry.sha256
14024
- }];
14121
+ const declared = comparisonDeclaration(planPublication(root));
14122
+ if (declared.entries.length === 0) throw new Error("No theme files to compare.");
14123
+ const comparison = await compareDeclaration(api, themeId, branch.slug, root, declared);
14124
+ if (comparison.matches) writeStagingBaseline(root.root, {
14125
+ themeId,
14126
+ revision: comparison.revision,
14127
+ ...declared
14025
14128
  });
14026
- if (entries.length === 0) throw new Error("No theme files to compare.");
14027
- const compared = await compareBranch(api, themeId, branch.slug, entries, memberConfigurationTexts(plan));
14129
+ return comparison;
14130
+ }
14131
+ function comparisonDeclaration(plan) {
14132
+ return {
14133
+ entries: plan.entries.flatMap((entry) => {
14134
+ if (!("sha256" in entry)) return [];
14135
+ if (entry.path !== "members_sites.json") return [{
14136
+ path: entry.path,
14137
+ sha256: entry.sha256
14138
+ }];
14139
+ const sent = plan.contents.get(entry.sha256);
14140
+ const digest = sent && jsonDigest(Buffer.from(sent).toString("utf-8"));
14141
+ return [{
14142
+ path: entry.path,
14143
+ sha256: digest ?? entry.sha256
14144
+ }];
14145
+ }),
14146
+ memberTexts: memberConfigurationTexts(plan)
14147
+ };
14148
+ }
14149
+ /** Compare declared files with what the staging branch serves now. */
14150
+ async function compareDeclaration(api, themeId, slug, root, declared) {
14151
+ const compared = await compareBranch(api, themeId, slug, declared.entries, declared.memberTexts);
14028
14152
  const keptRemote = keptRemoteAssets(root);
14029
14153
  const branchOnly = compared.branch_only.filter((path) => unpublishableReason(path) === null && !keptRemote.has(path));
14030
14154
  return {
@@ -14122,6 +14246,13 @@ function resolveRoot(root) {
14122
14246
  const workspace = findWorkspace();
14123
14247
  return (workspace && resolveThemeRootFromCwd(workspace)) ?? root;
14124
14248
  }
14249
+ /** The --replace-at revision, or undefined when not given. */
14250
+ function replaceAtRevision(value) {
14251
+ if (value === void 0) return void 0;
14252
+ const revision = Number(value);
14253
+ if (!Number.isInteger(revision) || revision < 0) throw new Error(`--replace-at must be a staging revision, got "${value}".`);
14254
+ return revision;
14255
+ }
14125
14256
  /** A list the server put under one key of a refusal's details. */
14126
14257
  function detailList(error, key) {
14127
14258
  const listed = error.details[key];
@@ -14232,11 +14363,14 @@ function createStagingCommand() {
14232
14363
  report(settled, opts.json);
14233
14364
  if (settled.branch?.state !== "ready") process.exitCode = 1;
14234
14365
  }));
14235
- const publish = withThemeOptions$1(new Command("publish").description("Publish this directory's theme files to staging; production is not touched")).option("-y, --yes", "Accepted for consistency; publishing never asks").action((opts) => run$1(opts, async () => {
14366
+ const publish = withThemeOptions$1(new Command("publish").description("Publish this directory's theme files to staging; production is not touched")).option("-y, --yes", "Accepted for consistency; publishing never asks").option("--force", "Publish even when it would replace files someone else published to staging").option("--replace-at <revision>", "Replace others' files only if staging is still at this revision; otherwise check again").action((opts) => run$1(opts, async () => {
14236
14367
  requireToken();
14237
14368
  const root = new ThemeRoot(resolveRoot(opts.root));
14238
14369
  if (!root.isValid()) throw new Error(`'${root.root}' does not look like a theme directory.`);
14239
- const outcome = await publishToStaging(createApiClient(), resolveThemeId(opts), root, opts.json ? void 0 : (message) => console.error(chalk.dim(message)));
14370
+ const outcome = await publishToStaging(createApiClient(), resolveThemeId(opts), root, opts.json ? void 0 : (message) => console.error(chalk.dim(message)), {
14371
+ force: opts.force,
14372
+ replaceAt: replaceAtRevision(opts.replaceAt)
14373
+ });
14240
14374
  if (opts.json) {
14241
14375
  console.log(JSON.stringify(outcome));
14242
14376
  return;