@marktiderman/genesis-cli 1.12.1 → 1.13.1

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/README.md CHANGED
@@ -343,10 +343,16 @@ comment on column public.customers.notes is
343
343
  'Private to staff. @genesis {"label": "Internal notes", "widget": "textarea"}';
344
344
  ```
345
345
 
346
- The text before `@genesis` stays the description. Tables take `label`, `labelField` and
347
- `description`; columns take `label`, `description` and `widget`. Anything else is an error that
348
- names the table or column. These settings are generator input only: the checked-in file is what
349
- ships.
346
+ The text before `@genesis` stays the description. Tables take `label`, `labelField`, `noun`,
347
+ `nounPlural` and `description` (strings). Columns take `label`, `description` and `widget`
348
+ (strings), plus `readonly` (true for a column only the server writes: a UI shows it read-only) and
349
+ `sensitive` (true to keep it out of lists, exports and previews unless a screen asks). Anything
350
+ else is an error that names the table or column. These settings are generator input only: the
351
+ checked-in file is what ships.
352
+
353
+ Each resource is emitted as its own `as const satisfies ResourceContract<Row>` constant (so a bad
354
+ setting, like an unknown `widget`, fails to compile), with a row type and a field-name union
355
+ (`PublicBrandsField`), and `contracts` collects them by `schema.table`.
350
356
 
351
357
  ## genesis doctor
352
358
 
package/dist/cli.js CHANGED
@@ -1682,13 +1682,17 @@ function parse(comment, keys, where) {
1682
1682
  }
1683
1683
  const settings2 = {};
1684
1684
  for (const [key2, v] of Object.entries(value)) {
1685
- if (!keys.includes(key2)) {
1686
- throw new CommentSettingsError(`${where}: unknown ${MARKER} setting "${key2}". Allowed: ${keys.join(", ")}.`);
1685
+ const kind = keys[key2];
1686
+ if (!kind) {
1687
+ throw new CommentSettingsError(`${where}: unknown ${MARKER} setting "${key2}". Allowed: ${Object.keys(keys).join(", ")}.`);
1687
1688
  }
1688
- if (typeof v !== "string") throw new CommentSettingsError(`${where}: ${MARKER} "${key2}" must be a string.`);
1689
+ if (typeof v !== kind) throw new CommentSettingsError(`${where}: ${MARKER} "${key2}" must be a ${kind}.`);
1689
1690
  settings2[key2] = v;
1690
1691
  }
1691
- return { ...description ? { description } : {}, ...Object.keys(settings2).length ? { settings: settings2 } : {} };
1692
+ return {
1693
+ ...description ? { description } : {},
1694
+ ...Object.keys(settings2).length ? { settings: settings2 } : {}
1695
+ };
1692
1696
  }
1693
1697
  function parseTableComment(comment, table) {
1694
1698
  return parse(comment, TABLE_KEYS, table);
@@ -1700,8 +1704,8 @@ var MARKER, TABLE_KEYS, FIELD_KEYS, CommentSettingsError;
1700
1704
  var init_comments = __esm({
1701
1705
  "src/introspect/comments.ts"() {
1702
1706
  MARKER = "@genesis";
1703
- TABLE_KEYS = ["label", "labelField", "description"];
1704
- FIELD_KEYS = ["label", "description", "widget"];
1707
+ TABLE_KEYS = { label: "string", labelField: "string", description: "string", noun: "string", nounPlural: "string" };
1708
+ FIELD_KEYS = { label: "string", description: "string", widget: "string", readonly: "boolean", sensitive: "boolean" };
1705
1709
  CommentSettingsError = class extends Error {
1706
1710
  };
1707
1711
  }
@@ -1881,12 +1885,16 @@ function typeName(table) {
1881
1885
  const pascal = `${table.schema}_${table.table}`.split(/[^A-Za-z0-9]+/).filter(Boolean).map((p) => p[0].toUpperCase() + p.slice(1)).join("");
1882
1886
  return `${/^[0-9]/.test(pascal) ? "T" : ""}${pascal}Row`;
1883
1887
  }
1888
+ function constName(table) {
1889
+ const row = typeName(table).replace(/Row$/, "");
1890
+ return row[0].toLowerCase() + row.slice(1);
1891
+ }
1884
1892
  function tsType(column) {
1885
1893
  const base = column.enumValues?.length ? column.enumValues.map(quote).join(" | ") : TS_TYPE[column.type];
1886
1894
  const element = column.array ? base.includes("|") ? `(${base})[]` : `${base}[]` : base;
1887
1895
  return column.nullable ? `${element} | null` : element;
1888
1896
  }
1889
- function field(column) {
1897
+ function field(column, indent) {
1890
1898
  const parts = [`name: ${quote(column.name)}`, `type: ${quote(column.type)}`, `dbType: ${quote(column.dbType)}`];
1891
1899
  if (column.array) parts.push("array: true");
1892
1900
  if (column.nullable) parts.push("nullable: true");
@@ -1902,10 +1910,23 @@ function field(column) {
1902
1910
  if (label) parts.push(`label: ${quote(label)}`);
1903
1911
  if (description) parts.push(`description: ${quote(description)}`);
1904
1912
  if (column.settings?.widget) parts.push(`widget: ${quote(column.settings.widget)}`);
1905
- return ` { ${parts.join(", ")} },`;
1913
+ if (column.settings?.readonly) parts.push("readOnly: true");
1914
+ if (column.settings?.sensitive) parts.push("sensitive: true");
1915
+ return `${indent}{ ${parts.join(", ")} },`;
1906
1916
  }
1907
1917
  function emitContracts(tables, options) {
1908
1918
  const sorted = [...tables].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
1919
+ const seen = /* @__PURE__ */ new Map();
1920
+ for (const table of sorted) {
1921
+ const id = typeName(table);
1922
+ const other = seen.get(id);
1923
+ if (other) throw new EmitError(`${other} and ${table.name} would both be named ${id}; rename one, or introspect them separately.`);
1924
+ seen.set(id, table.name);
1925
+ const labelField = table.settings?.labelField;
1926
+ if (labelField && !table.columns.some((c) => c.name === labelField)) {
1927
+ throw new EmitError(`${table.name}: @genesis labelField "${labelField}" isn't one of its columns.`);
1928
+ }
1929
+ }
1909
1930
  const out = [
1910
1931
  "// Generated by `genesis introspect` \u2014 do not edit by hand.",
1911
1932
  `// Source: ${options.source}`,
@@ -1914,31 +1935,42 @@ function emitContracts(tables, options) {
1914
1935
  "// Labels, widgets and descriptions come from `@genesis {...}` settings in",
1915
1936
  "// table and column comments. Pass `contracts` to createStudioHandler: it",
1916
1937
  "// wins over discovery for presentation, while types, keys and every",
1917
- "// permission still come from the live database.",
1938
+ "// permission still come from the live database. Each contract goes through",
1939
+ "// defineResource, so a bad @genesis setting fails to compile and views read the",
1940
+ "// row type with RowOf<typeof contract>.",
1918
1941
  "",
1919
- "export const contracts = {"
1942
+ 'import { defineResource } from "@marktiderman/genesis-core";',
1943
+ ""
1920
1944
  ];
1921
1945
  for (const table of sorted) {
1922
- out.push(` ${quote(table.name)}: {`);
1923
- out.push(` name: ${quote(table.name)},`);
1924
- out.push(` key: ${table.key.length === 1 ? quote(table.key[0]) : `[${table.key.map(quote).join(", ")}]`},`);
1925
- if (table.settings?.label) out.push(` label: ${quote(table.settings.label)},`);
1926
- if (table.settings?.labelField) out.push(` labelField: ${quote(table.settings.labelField)},`);
1946
+ const row = typeName(table);
1947
+ out.push(`/** \`${table.name}\`. */`);
1948
+ out.push(`export const ${constName(table)} = defineResource<${row}>()({`);
1949
+ out.push(` name: ${quote(table.name)},`);
1950
+ out.push(` key: ${table.key.length === 1 ? quote(table.key[0]) : `[${table.key.map(quote).join(", ")}]`},`);
1951
+ if (table.settings?.label) out.push(` label: ${quote(table.settings.label)},`);
1952
+ if (table.settings?.labelField) out.push(` labelField: ${quote(table.settings.labelField)},`);
1953
+ if (table.settings?.noun) out.push(` noun: ${quote(table.settings.noun)},`);
1954
+ if (table.settings?.nounPlural) out.push(` nounPlural: ${quote(table.settings.nounPlural)},`);
1927
1955
  const description = table.settings?.description ?? table.description;
1928
- if (description) out.push(` description: ${quote(description)},`);
1929
- out.push(" fields: [");
1930
- for (const column of table.columns) out.push(field(column));
1931
- out.push(" ],");
1932
- out.push(" },");
1933
- }
1956
+ if (description) out.push(` description: ${quote(description)},`);
1957
+ out.push(" fields: [");
1958
+ for (const column of table.columns) out.push(field(column, " "));
1959
+ out.push(" ],");
1960
+ out.push("});", "");
1961
+ }
1962
+ out.push("export const contracts = {");
1963
+ for (const table of sorted) out.push(` ${quote(table.name)}: ${constName(table)},`);
1934
1964
  out.push("} as const;", "");
1935
1965
  out.push("/** Every resource in this contract, `schema.table`. */");
1936
1966
  out.push("export type GenesisResource = keyof typeof contracts;", "");
1937
1967
  for (const table of sorted) {
1938
1968
  out.push(`/** A row of \`${table.name}\`, as the studio handler returns it. */`);
1939
- out.push(`export interface ${typeName(table)} {`);
1969
+ out.push(`export type ${typeName(table)} = {`);
1940
1970
  for (const column of table.columns) out.push(` ${propKey(column.name)}: ${tsType(column)};`);
1941
- out.push("}", "");
1971
+ out.push("};", "");
1972
+ out.push(`/** The field names of \`${table.name}\`. */`);
1973
+ out.push(`export type ${typeName(table).replace(/Row$/, "Field")} = keyof ${typeName(table)};`, "");
1942
1974
  }
1943
1975
  out.push("/** Row types by resource. */");
1944
1976
  out.push("export interface GenesisRows {");
@@ -1946,7 +1978,7 @@ function emitContracts(tables, options) {
1946
1978
  out.push("}", "");
1947
1979
  return out.join("\n");
1948
1980
  }
1949
- var TS_TYPE;
1981
+ var TS_TYPE, EmitError;
1950
1982
  var init_emit = __esm({
1951
1983
  "src/introspect/emit.ts"() {
1952
1984
  TS_TYPE = {
@@ -1957,6 +1989,8 @@ var init_emit = __esm({
1957
1989
  datetime: "string",
1958
1990
  json: "unknown"
1959
1991
  };
1992
+ EmitError = class extends Error {
1993
+ };
1960
1994
  }
1961
1995
  });
1962
1996
 
@@ -1964,6 +1998,7 @@ var init_emit = __esm({
1964
1998
  var introspect_exports = {};
1965
1999
  __export(introspect_exports, {
1966
2000
  CommentSettingsError: () => CommentSettingsError,
2001
+ EmitError: () => EmitError,
1967
2002
  emitContracts: () => emitContracts,
1968
2003
  introspectOpenApi: () => introspectOpenApi,
1969
2004
  introspectPostgres: () => introspectPostgres,
@@ -3107,7 +3142,7 @@ var init_native = __esm({
3107
3142
  init_source();
3108
3143
  SCRIPT_EXTS3 = /* @__PURE__ */ new Set([".ts", ".tsx", ".js", ".jsx", ".mts", ".cts", ".mjs", ".cjs"]);
3109
3144
  JSX_EXTS3 = /* @__PURE__ */ new Set([".tsx", ".jsx"]);
3110
- LAYOUT_NAME = /(?:Page|Screen|ScreenContainer)$/;
3145
+ LAYOUT_NAME = /(?:Page|Screen|ScreenContainer)$|^NativeResourceView$/;
3111
3146
  NON_SCREEN_ROUTE = /(?:^|\/)(?:_layout|\+html|\+native-intent|\+middleware)\.[jt]sx?$|\+api\.[jt]sx?$/;
3112
3147
  PART_HEADER = /(?:Section|List|Card|Dialog|Table|Group|Column|Row|Cell|Accordion|Sheet|Drawer|Modal|Popover|Menu|Panel|Form)Header$/;
3113
3148
  HIDES_HEADER = /\bheaderShown\s*(?::|=\s*\{)\s*false\b/;
@@ -4774,7 +4809,7 @@ function checkPageLayouts(ctx) {
4774
4809
  const rendersLayout = (code) => {
4775
4810
  const names = new Set(layouts);
4776
4811
  for (const n of namedImportsFrom(code, GENESIS_PACKAGE2)) if (PACKAGE_SCREEN.test(n)) names.add(n);
4777
- return rendersRe(names)?.test(code) ?? false;
4812
+ return rendersRe(names)?.test(code.replace(EMBEDDED_RESOURCE_VIEW, "")) ?? false;
4778
4813
  };
4779
4814
  const cache2 = /* @__PURE__ */ new Map();
4780
4815
  const provides = (r) => {
@@ -4794,7 +4829,7 @@ function checkPageLayouts(ctx) {
4794
4829
  });
4795
4830
  }
4796
4831
  }
4797
- var SUB_NAV_DOCS, CONTAINER, SWITCHER, SHELL, GENESIS_PACKAGE2, PAGE_LAYOUT, PACKAGE_SCREEN, isNative, rendersRe, routeVia;
4832
+ var SUB_NAV_DOCS, CONTAINER, SWITCHER, SHELL, GENESIS_PACKAGE2, PAGE_LAYOUT, EMBEDDED_RESOURCE_VIEW, PACKAGE_SCREEN, isNative, rendersRe, routeVia;
4798
4833
  var init_structure = __esm({
4799
4834
  "src/doctor/structure.ts"() {
4800
4835
  init_scan();
@@ -4806,8 +4841,9 @@ var init_structure = __esm({
4806
4841
  SWITCHER = /<(TabsList|SubNavTabs)(?=[\s>/])/g;
4807
4842
  SHELL = /<AppShell(?=[\s>/])/;
4808
4843
  GENESIS_PACKAGE2 = /^@marktiderman\/genesis(?:-[\w-]+)?(?:\/|$)/;
4809
- PAGE_LAYOUT = /^(?!Breadcrumb)[A-Z]\w*(?:Page|PageShell)$/;
4810
- PACKAGE_SCREEN = /^(?!Breadcrumb)[A-Z]\w*(?:Page|PageShell|Screen|Workspace)$/;
4844
+ PAGE_LAYOUT = /^(?:(?!Breadcrumb)[A-Z]\w*(?:Page|PageShell)|ResourceView)$/;
4845
+ EMBEDDED_RESOURCE_VIEW = /<ResourceView\b[^>]*?\blayout\s*=\s*(?:(["'`]) {8}\1|\{\s*(["'`]) {8}\2\s*\})[^>]*>/g;
4846
+ PACKAGE_SCREEN = /^(?:(?!Breadcrumb)[A-Z]\w*(?:Page|PageShell|Screen|Workspace)|ResourceView)$/;
4811
4847
  isNative = (ctx) => ctx.config.packs?.includes("native") ?? false;
4812
4848
  rendersRe = (names) => {
4813
4849
  const list = [...names];