@diagonal-hq/cli 0.42.19 → 0.42.20

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/README.md +1 -1
  2. package/dist/cli.mjs +64 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -218,7 +218,7 @@ The build reports each of these errors at `file:line:column`:
218
218
 
219
219
  `diagonal check --fix` rewrites `params` and the flat options to `bindings` and `seo` in place. It changes only the options object, and the rest of the file stays byte-identical. It writes in the same pass as a missing `uid`. Without `--fix`, `diagonal check` only warns. A resolver object of another type in a flat option stays in place, and the build reports an error at that option. A `text.template` or `page.binding` resolver in a flat option also stays, with the other flat options of the page, and the build warns at it. Write it as a seo function. The page builder's **Save** writes the new shape too.
220
220
 
221
- The page builder shows a function seo value as read-only, with "Set in the page file". **Save** keeps the function.
221
+ The page builder edits each `seo` value as text or as a seo function. Text that starts with `({ bindings }) =>` is a function, and **Save** writes it into the page file. The social image also takes `Assets.<Name>.key`, and its button picks an Application Asset.
222
222
 
223
223
  The change is forward-only. A file with `seo` needs a `@diagonal-hq/cli` version that has this option. An older version reports `Unknown page option "seo"`.
224
224
 
package/dist/cli.mjs CHANGED
@@ -6995,6 +6995,7 @@ var OPTION_ORDER2 = [
6995
6995
  "bindings"
6996
6996
  ];
6997
6997
  var SEO_KEEP_PREFIX = "seo.";
6998
+ var TEXT_EXPORT = "text";
6998
6999
  var DOMAIN_KEY = /^[a-z][a-zA-Z0-9]*$/;
6999
7000
  var SourceExpression2 = class {
7000
7001
  constructor(text) {
@@ -7147,25 +7148,37 @@ function writePageFile(path, options, code, previous, resources, keep = [], { br
7147
7148
  return { ok: false, diagnostic: diagnosticFromError(path, error) };
7148
7149
  }
7149
7150
  }
7150
- function writeWholePageFile(path, file, options, resources, keep = [], { branchPaths = null, permissions = [] } = {}) {
7151
+ function writeWholePageFile(path, file, options, resources, keep = [], { branchPaths = null, permissions = [], sourceOptions = {} } = {}) {
7151
7152
  try {
7152
7153
  const names = branchResourceNames(resources);
7153
7154
  const permissionEntries = declaredPermissionEntries(permissions);
7154
7155
  if (options === null) {
7155
- if (keep.length > 0) {
7156
+ if (keep.length > 0 || Object.keys(sourceOptions).length > 0) {
7156
7157
  throw new SourceError("A page file that is written as it is keeps no options", null);
7157
7158
  }
7158
7159
  checkPageName(path, branchPaths);
7159
7160
  return checkedPage(path, file, names, branchPaths, permissionEntries);
7160
7161
  }
7161
- return writeParts(path, options, () => filePartsOf(file), {
7162
+ const seoSources = Object.fromEntries(
7163
+ Object.entries(sourceOptions).map(([key, text]) => [key, seoFunctionSource(key, text)])
7164
+ );
7165
+ const readsText = Object.values(seoSources).some(({ expression }) => readsMemberOf(expression, TEXT_EXPORT));
7166
+ const parts = () => {
7167
+ const filed = filePartsOf(file);
7168
+ if (!readsText) {
7169
+ return filed;
7170
+ }
7171
+ const code = parseSource([filed.imports, filed.helpers, `(${filed.component});`, filed.trailing].join("\n"), { jsx: true });
7172
+ return !bindsName(code, TEXT_EXPORT) ? { ...filed, imports: withNamedImport(filed.imports, AUTHORING_MODULE, TEXT_EXPORT, TEXT_EXPORT) } : filed;
7173
+ };
7174
+ return writeParts(path, options, parts, {
7162
7175
  previous: file,
7163
7176
  readPath: path,
7164
7177
  names,
7165
7178
  permissionEntries,
7166
7179
  keep,
7167
7180
  branchPaths,
7168
- sourceOptions: {},
7181
+ sourceOptions: Object.fromEntries(Object.entries(seoSources).map(([key, { source }]) => [key, source])),
7169
7182
  addsCodeImports: false
7170
7183
  });
7171
7184
  } catch (error) {
@@ -7223,7 +7236,6 @@ function writeParts(path, options, parts, { previous, readPath: readPath2, names
7223
7236
  }
7224
7237
  options = withKeptOptions(options, read.options, keep);
7225
7238
  }
7226
- options = withSeoSource(options);
7227
7239
  for (const [key, text] of Object.entries(sourceOptions)) {
7228
7240
  if (key === "layout") {
7229
7241
  options = { ...options, layout: new SourceExpression2(text) };
@@ -7233,6 +7245,7 @@ function writeParts(path, options, parts, { previous, readPath: readPath2, names
7233
7245
  throw new SourceError(`Unknown source option ${key}`, null);
7234
7246
  }
7235
7247
  }
7248
+ options = withSeoSource(options);
7236
7249
  checkPageName(path, branchPaths);
7237
7250
  const written = parts();
7238
7251
  const { helpers, component, trailing } = written;
@@ -7528,6 +7541,38 @@ function withSeoSource(options) {
7528
7541
  }
7529
7542
  return Object.keys(seo).length === 0 ? omit(options, "seo") : { ...options, seo };
7530
7543
  }
7544
+ function seoFunctionSource(key, text) {
7545
+ if (!key.startsWith(SEO_KEEP_PREFIX) || !SEO_KEYS.includes(key.slice(SEO_KEEP_PREFIX.length))) {
7546
+ throw new SourceError(`Unknown source option ${key}`, null);
7547
+ }
7548
+ const source = text.trim();
7549
+ let parsed;
7550
+ try {
7551
+ parsed = parseSource(`(${source}
7552
+ );`, { jsx: false });
7553
+ } catch {
7554
+ throw new SourceError(`${key}: Write text, or a seo function: ({ bindings }) => ...`, null);
7555
+ }
7556
+ const [statement] = parsed.program.body;
7557
+ if (parsed.program.body.length !== 1 || statement.type !== "ExpressionStatement" || !isSeoFunction(statement.expression)) {
7558
+ throw new SourceError(`${key}: Write text, or a seo function: ({ bindings }) => ...`, null);
7559
+ }
7560
+ const lastComment = parsed.comments.at(-1);
7561
+ const endsInLineComment = lastComment?.type === "CommentLine" && lastComment.end === source.length + 1;
7562
+ return { expression: statement.expression, source: endsInLineComment ? `${source}
7563
+ ` : source };
7564
+ }
7565
+ function readsMemberOf(expression, name) {
7566
+ const pending = [expression];
7567
+ while (pending.length > 0) {
7568
+ const node = pending.pop();
7569
+ if ((node.type === "MemberExpression" || node.type === "OptionalMemberExpression") && node.object.type === "Identifier" && node.object.name === name) {
7570
+ return true;
7571
+ }
7572
+ pending.push(...childNodes(node));
7573
+ }
7574
+ return false;
7575
+ }
7531
7576
  function omit(object2, key) {
7532
7577
  const { [key]: _omitted, ...rest } = object2;
7533
7578
  return rest;
@@ -7745,6 +7790,13 @@ ${closing}}`;
7745
7790
  }
7746
7791
 
7747
7792
  // scripts/lib/runtime-build/source-adapters/page.mjs
7793
+ function sourceOptionsOf(value) {
7794
+ object(value, "source_options");
7795
+ if (Object.values(value).some((text) => typeof text !== "string")) {
7796
+ throw new SourceTransformRequestError("source_options must map option names to source text.");
7797
+ }
7798
+ return value;
7799
+ }
7748
7800
  function contextOf(context) {
7749
7801
  if (context.previous_path !== void 0 && context.previous_path !== null && typeof context.previous_path !== "string") {
7750
7802
  throw new SourceTransformRequestError("previous_path must be a string or null.");
@@ -7753,6 +7805,7 @@ function contextOf(context) {
7753
7805
  resources: branchResources(context),
7754
7806
  branchPaths: context.branch_paths == null ? null : strings(context.branch_paths, "branch_paths"),
7755
7807
  keep: strings(context.keep ?? [], "keep"),
7808
+ sourceOptions: sourceOptionsOf(context.source_options ?? {}),
7756
7809
  permissions: branchPermissions(context),
7757
7810
  previousPath: context.previous_path ?? null
7758
7811
  };
@@ -7767,14 +7820,15 @@ function writeFile({ path, data, context }) {
7767
7820
  if (data.options !== void 0 && data.options !== null) {
7768
7821
  object(data.options, "options");
7769
7822
  }
7770
- const { resources, branchPaths, permissions, keep } = contextOf(context);
7771
- if (keep.length && (data.options ?? null) === null) {
7772
- throw new SourceTransformRequestError("Keeping page options requires options.");
7823
+ const { resources, branchPaths, permissions, keep, sourceOptions } = contextOf(context);
7824
+ if ((keep.length || Object.keys(sourceOptions).length) && (data.options ?? null) === null) {
7825
+ throw new SourceTransformRequestError("Keeping or writing page options requires options.");
7773
7826
  }
7774
7827
  return checked(
7775
7828
  writeWholePageFile(path, data.file, data.options ?? null, resources, keep, {
7776
7829
  branchPaths,
7777
- permissions
7830
+ permissions,
7831
+ sourceOptions
7778
7832
  })
7779
7833
  ).source;
7780
7834
  }
@@ -7803,7 +7857,7 @@ function checked(result) {
7803
7857
  return result;
7804
7858
  }
7805
7859
  var pageAdapter = Object.freeze({
7806
- contextFields: ["resources", "resource_slugs", "branch_paths", "permissions", "keep", "previous_path"],
7860
+ contextFields: ["resources", "resource_slugs", "branch_paths", "permissions", "keep", "previous_path", "source_options"],
7807
7861
  // `code` is the factory body of the page function, with `options`. `file` is
7808
7862
  // the whole file, as the page builder edits it: it is written as it is, or
7809
7863
  // with `options` in place of its own. `paused` alone writes only that option.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diagonal-hq/cli",
3
- "version": "0.42.19",
3
+ "version": "0.42.20",
4
4
  "description": "The diagonal CLI and the configuration helpers for file-based Diagonal workspaces.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {