cogens 0.2.0 → 0.3.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 (158) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/README.md +35 -7
  3. package/dist/admin/app.d.ts +21 -0
  4. package/dist/admin/app.d.ts.map +1 -0
  5. package/dist/admin/app.js +702 -0
  6. package/dist/admin/app.js.map +1 -0
  7. package/dist/admin/assets.d.ts +17 -0
  8. package/dist/admin/assets.d.ts.map +1 -0
  9. package/dist/admin/assets.js +51 -0
  10. package/dist/admin/assets.js.map +1 -0
  11. package/dist/admin/fields.d.ts +29 -0
  12. package/dist/admin/fields.d.ts.map +1 -0
  13. package/dist/admin/fields.js +130 -0
  14. package/dist/admin/fields.js.map +1 -0
  15. package/dist/admin/forms.d.ts +18 -0
  16. package/dist/admin/forms.d.ts.map +1 -0
  17. package/dist/admin/forms.js +53 -0
  18. package/dist/admin/forms.js.map +1 -0
  19. package/dist/admin/index.d.ts +15 -0
  20. package/dist/admin/index.d.ts.map +1 -0
  21. package/dist/admin/index.js +13 -0
  22. package/dist/admin/index.js.map +1 -0
  23. package/dist/admin/rules.d.ts +24 -0
  24. package/dist/admin/rules.d.ts.map +1 -0
  25. package/dist/admin/rules.js +51 -0
  26. package/dist/admin/rules.js.map +1 -0
  27. package/dist/admin/types.d.ts +147 -0
  28. package/dist/admin/types.d.ts.map +1 -0
  29. package/dist/admin/types.js +2 -0
  30. package/dist/admin/types.js.map +1 -0
  31. package/dist/admin/views.d.ts +138 -0
  32. package/dist/admin/views.d.ts.map +1 -0
  33. package/dist/admin/views.js +225 -0
  34. package/dist/admin/views.js.map +1 -0
  35. package/dist/auth/handoff.d.ts +27 -0
  36. package/dist/auth/handoff.d.ts.map +1 -0
  37. package/dist/auth/handoff.js +41 -0
  38. package/dist/auth/handoff.js.map +1 -0
  39. package/dist/auth/index.d.ts +10 -0
  40. package/dist/auth/index.d.ts.map +1 -0
  41. package/dist/auth/index.js +10 -0
  42. package/dist/auth/index.js.map +1 -0
  43. package/dist/auth/password.d.ts +48 -0
  44. package/dist/auth/password.d.ts.map +1 -0
  45. package/dist/auth/password.js +154 -0
  46. package/dist/auth/password.js.map +1 -0
  47. package/dist/auth/principal.d.ts +30 -0
  48. package/dist/auth/principal.d.ts.map +1 -0
  49. package/dist/auth/principal.js +30 -0
  50. package/dist/auth/principal.js.map +1 -0
  51. package/dist/auth/session.d.ts +68 -0
  52. package/dist/auth/session.d.ts.map +1 -0
  53. package/dist/auth/session.js +92 -0
  54. package/dist/auth/session.js.map +1 -0
  55. package/dist/core/access/ability.d.ts +24 -0
  56. package/dist/core/access/ability.d.ts.map +1 -0
  57. package/dist/core/access/ability.js +64 -0
  58. package/dist/core/access/ability.js.map +1 -0
  59. package/dist/core/access/can.d.ts +41 -0
  60. package/dist/core/access/can.d.ts.map +1 -0
  61. package/dist/core/access/can.js +38 -0
  62. package/dist/core/access/can.js.map +1 -0
  63. package/dist/core/access/compile.d.ts +12 -0
  64. package/dist/core/access/compile.d.ts.map +1 -0
  65. package/dist/core/access/compile.js +53 -0
  66. package/dist/core/access/compile.js.map +1 -0
  67. package/dist/core/access/permits.d.ts +24 -0
  68. package/dist/core/access/permits.d.ts.map +1 -0
  69. package/dist/core/access/permits.js +29 -0
  70. package/dist/core/access/permits.js.map +1 -0
  71. package/dist/core/access/when.d.ts +6 -0
  72. package/dist/core/access/when.d.ts.map +1 -0
  73. package/dist/core/access/when.js +64 -0
  74. package/dist/core/access/when.js.map +1 -0
  75. package/dist/core/access/where.d.ts +25 -0
  76. package/dist/core/access/where.d.ts.map +1 -0
  77. package/dist/core/access/where.js +81 -0
  78. package/dist/core/access/where.js.map +1 -0
  79. package/dist/core/columns.d.ts +6 -0
  80. package/dist/core/columns.d.ts.map +1 -0
  81. package/dist/core/columns.js +6 -0
  82. package/dist/core/columns.js.map +1 -0
  83. package/dist/core/config/define.d.ts +101 -0
  84. package/dist/core/config/define.d.ts.map +1 -0
  85. package/dist/core/config/define.js +97 -0
  86. package/dist/core/config/define.js.map +1 -0
  87. package/dist/core/config/field.d.ts +87 -0
  88. package/dist/core/config/field.d.ts.map +1 -0
  89. package/dist/core/config/field.js +75 -0
  90. package/dist/core/config/field.js.map +1 -0
  91. package/dist/core/errors.d.ts +66 -0
  92. package/dist/core/errors.d.ts.map +1 -0
  93. package/dist/core/errors.js +94 -0
  94. package/dist/core/errors.js.map +1 -0
  95. package/dist/core/index.d.ts +31 -0
  96. package/dist/core/index.d.ts.map +1 -0
  97. package/dist/core/index.js +20 -0
  98. package/dist/core/index.js.map +1 -0
  99. package/dist/core/media/index.d.ts +36 -0
  100. package/dist/core/media/index.d.ts.map +1 -0
  101. package/dist/core/media/index.js +99 -0
  102. package/dist/core/media/index.js.map +1 -0
  103. package/dist/core/principal.d.ts +28 -0
  104. package/dist/core/principal.d.ts.map +1 -0
  105. package/dist/core/principal.js +25 -0
  106. package/dist/core/principal.js.map +1 -0
  107. package/dist/core/store/assets.d.ts +3 -0
  108. package/dist/core/store/assets.d.ts.map +1 -0
  109. package/dist/core/store/assets.js +3 -0
  110. package/dist/core/store/assets.js.map +1 -0
  111. package/dist/core/store/cms.d.ts +53 -0
  112. package/dist/core/store/cms.d.ts.map +1 -0
  113. package/dist/core/store/cms.js +154 -0
  114. package/dist/core/store/cms.js.map +1 -0
  115. package/dist/core/store/content.d.ts +57 -0
  116. package/dist/core/store/content.d.ts.map +1 -0
  117. package/dist/core/store/content.js +455 -0
  118. package/dist/core/store/content.js.map +1 -0
  119. package/dist/core/store/sql.d.ts +88 -0
  120. package/dist/core/store/sql.d.ts.map +1 -0
  121. package/dist/core/store/sql.js +149 -0
  122. package/dist/core/store/sql.js.map +1 -0
  123. package/dist/core/store/types.d.ts +107 -0
  124. package/dist/core/store/types.d.ts.map +1 -0
  125. package/dist/core/store/types.js +2 -0
  126. package/dist/core/store/types.js.map +1 -0
  127. package/dist/core/testing/index.d.ts +27 -0
  128. package/dist/core/testing/index.d.ts.map +1 -0
  129. package/dist/core/testing/index.js +60 -0
  130. package/dist/core/testing/index.js.map +1 -0
  131. package/dist/migrations-tool/bin.d.ts +3 -0
  132. package/dist/migrations-tool/bin.d.ts.map +1 -0
  133. package/dist/migrations-tool/bin.js +40 -0
  134. package/dist/migrations-tool/bin.js.map +1 -0
  135. package/dist/migrations-tool/index.d.ts +16 -0
  136. package/dist/migrations-tool/index.d.ts.map +1 -0
  137. package/dist/migrations-tool/index.js +24 -0
  138. package/dist/migrations-tool/index.js.map +1 -0
  139. package/dist/testing/db.d.ts +16 -0
  140. package/dist/testing/db.d.ts.map +1 -0
  141. package/dist/testing/db.js +43 -0
  142. package/dist/testing/db.js.map +1 -0
  143. package/dist/testing/index.d.ts +10 -0
  144. package/dist/testing/index.d.ts.map +1 -0
  145. package/dist/testing/index.js +8 -0
  146. package/dist/testing/index.js.map +1 -0
  147. package/package.json +33 -11
  148. package/src/admin/app.ts +181 -36
  149. package/src/admin/fields.ts +5 -2
  150. package/src/admin/index.ts +3 -2
  151. package/src/admin/types.ts +52 -1
  152. package/src/admin/views.ts +99 -21
  153. package/src/core/config/define.ts +1 -1
  154. package/src/core/config/field.ts +19 -2
  155. package/src/core/index.ts +1 -1
  156. package/src/core/store/cms.ts +1 -1
  157. package/src/migrations-tool/bin.ts +34 -0
  158. package/{scripts/print-migration.ts → src/migrations-tool/index.ts} +6 -19
@@ -103,23 +103,74 @@ export function signInPage(shell: Shell, next: string, error?: string): Html {
103
103
  });
104
104
  }
105
105
 
106
- // ── The type list ───────────────────────────────────────────────────
106
+ // ── The home page ───────────────────────────────────────────────────
107
107
 
108
- export function typeListPage(shell: Shell, stored: { id: string; name: string }[], external: { id: string; name: string }[]): Html {
109
- const { mount } = shell;
110
- const list = (items: { id: string; name: string }[], base: string) =>
111
- html`<ul class="stack">${items.map(t => html`<li><a href="${mount}/${base}/${t.id}">${t.name}</a></li>`)}</ul>`;
108
+ export interface HomeSection {
109
+ title: string;
110
+ line?: string;
111
+ items: { href: string; name: string }[];
112
+ }
113
+
114
+ /** The home page: the sections given, in order, each with its line and its items; a section with no items is not drawn. */
115
+ export function homePage(shell: Shell, sections: HomeSection[]): Html {
116
+ const drawn = sections.filter(s => s.items.length > 0);
112
117
  return page(shell, {
113
- title: 'Content',
118
+ title: shell.siteName,
114
119
  body:
115
- stored.length === 0 && external.length === 0
120
+ drawn.length === 0
116
121
  ? html`<p>There is nothing here you may read.</p>`
117
- : html`${stored.length > 0 ? html`<section class="stack"><h2>Types</h2>${list(stored, 't')}</section>` : ''}${
118
- external.length > 0 ? html`<section class="stack"><h2>Tables</h2>${list(external, 'x')}</section>` : ''
119
- }`
122
+ : html`${drawn.map(
123
+ s => html`<section class="stack"><h2>${s.title}</h2>${s.line ? html`<p>${s.line}</p>` : ''}<ul class="stack">${s.items.map(
124
+ i => html`<li><a href="${i.href}">${i.name}</a></li>`
125
+ )}</ul></section>`
126
+ )}`
120
127
  });
121
128
  }
122
129
 
130
+ // ── Columns ─────────────────────────────────────────────────────────
131
+
132
+ /** A column of a list after the display name: a field (with its meta) or a built-in column (without). */
133
+ export interface ListColumn {
134
+ name: string;
135
+ label: string;
136
+ meta?: FieldMeta;
137
+ }
138
+
139
+ /** One cell: a field as its read form (a reference as the referred name, a select as its label), a status as a badge. */
140
+ function cell(column: ListColumn, value: unknown, refs: ResolveRef): Html {
141
+ if (column.name === 'status' || column.name === 'state') return typeof value === 'string' ? statusBadge(value) : html``;
142
+ if (column.meta) return renderField({ name: column.name, meta: column.meta, value, refs }, 'read');
143
+ return value === undefined || value === null || value === '' ? html`<span data-tone="secondary" data-variant="ghost">—</span>` : html`<span>${String(value)}</span>`;
144
+ }
145
+
146
+ // ── What belongs to an entry ────────────────────────────────────────
147
+
148
+ /** A read-only list on an entry's or row's page: its children of one type, or a host's related rows. */
149
+ export interface Belonging {
150
+ label: string;
151
+ items: { href: string | null; label: string; status?: string }[];
152
+ /** How many there are in all; more than `items` when the list is cut short. */
153
+ total: number;
154
+ /** Where the whole list is, when there is such a page. */
155
+ more?: string;
156
+ }
157
+
158
+ function belongings(lists: Belonging[]): Html {
159
+ return html`${lists.map(
160
+ b => html`<section class="stack" aria-label="${b.label}"><h2>${b.label}</h2>${
161
+ b.items.length === 0
162
+ ? html`<p>None.</p>`
163
+ : html`<ul class="stack">${b.items.map(
164
+ i => html`<li>${i.href ? html`<a href="${i.href}">${i.label}</a>` : html`<span>${i.label}</span>`}${i.status ? html` ${statusBadge(i.status)}` : ''}</li>`
165
+ )}</ul>${
166
+ b.total > b.items.length
167
+ ? html`<p>${b.more ? html`<a href="${b.more}">and ${b.total - b.items.length} more</a>` : html`and ${b.total - b.items.length} more`}</p>`
168
+ : ''
169
+ }`
170
+ }</section>`
171
+ )}`;
172
+ }
173
+
123
174
  // ── An entry list ───────────────────────────────────────────────────
124
175
 
125
176
  export interface ListInput {
@@ -132,6 +183,11 @@ export interface ListInput {
132
183
  limit: number;
133
184
  hasNext: boolean;
134
185
  canCreate: boolean;
186
+ /** The parent entry the list is filtered to, by id. */
187
+ parent: string;
188
+ /** The columns the host named; absent shows slug, status, owner and when updated. */
189
+ columns?: ListColumn[];
190
+ refs: ResolveRef;
135
191
  notice?: string;
136
192
  }
137
193
 
@@ -147,7 +203,9 @@ export function entryListPage(shell: Shell, input: ListInput): Html {
147
203
  const { type } = input;
148
204
  const base = `${mount}/t/${type.id}`;
149
205
  const title = (e: Entry) => String(e.data[type.displayField] ?? '') || e.slug || e.id;
150
- const filters = { q: input.q, status: input.status, owner: input.owner };
206
+ const filters = { q: input.q, status: input.status, owner: input.owner, parent: input.parent };
207
+ const columns = input.columns;
208
+ const valueOf = (e: Entry, name: string): unknown => (name in e && name !== 'data' ? (e as unknown as Record<string, unknown>)[name] : e.data[name]);
151
209
  return page(shell, {
152
210
  title: type.name,
153
211
  crumbs: [],
@@ -157,18 +215,26 @@ export function entryListPage(shell: Shell, input: ListInput): Html {
157
215
  <input type="search" name="q" value="${input.q}" placeholder="Slug" aria-label="Search by slug">
158
216
  <select name="status" aria-label="Status" data-autosubmit><option value="">Any status</option><option value="published"${input.status === 'published' ? ' selected' : ''}>Published</option><option value="draft"${input.status === 'draft' ? ' selected' : ''}>Draft</option></select>${
159
217
  type.owned ? html`<input type="text" name="owner" value="${input.owner}" placeholder="Owner id" aria-label="Owner">` : ''
160
- }
218
+ }${input.parent ? html`<input type="hidden" name="parent" value="${input.parent}">` : ''}
161
219
  <button type="submit" data-variant="outline">Filter</button>${
162
- input.q || input.status || input.owner ? html`<a href="${base}">Clear</a>` : ''
220
+ input.q || input.status || input.owner || input.parent ? html`<a href="${base}">Clear</a>` : ''
163
221
  }${input.canCreate ? html`<a href="${base}/new">New ${type.name.replace(/s$/, '')}</a>` : ''}
164
222
  </form>${
165
223
  input.entries.length === 0
166
224
  ? html`<p>No entries.</p>`
167
225
  : html`
168
226
  <table>
169
- <thead><tr><th scope="col">${type.fieldMeta[type.displayField]?.label ?? 'Title'}</th><th scope="col">Slug</th><th scope="col">Status</th>${type.owned ? html`<th scope="col">Owner</th>` : ''}<th scope="col">Updated</th></tr></thead>
227
+ <thead><tr><th scope="col">${type.fieldMeta[type.displayField]?.label ?? 'Title'}</th>${
228
+ columns
229
+ ? columns.map(col => html`<th scope="col">${col.label}</th>`)
230
+ : html`<th scope="col">Slug</th><th scope="col">Status</th>${type.owned ? html`<th scope="col">Owner</th>` : ''}<th scope="col">Updated</th>`
231
+ }</tr></thead>
170
232
  <tbody>${input.entries.map(
171
- e => html`<tr><td><a href="${base}/${e.id}">${title(e)}</a></td><td>${e.slug ?? ''}</td><td>${statusBadge(e.status)}</td>${type.owned ? html`<td>${e.owner_id ?? ''}</td>` : ''}<td>${e.updated_at}</td></tr>`
233
+ e => html`<tr><td><a href="${base}/${e.id}">${title(e)}</a></td>${
234
+ columns
235
+ ? columns.map(col => html`<td>${cell(col, valueOf(e, col.name), input.refs)}</td>`)
236
+ : html`<td>${e.slug ?? ''}</td><td>${statusBadge(e.status)}</td>${type.owned ? html`<td>${e.owner_id ?? ''}</td>` : ''}<td>${e.updated_at}</td>`
237
+ }</tr>`
172
238
  )}</tbody>
173
239
  </table>`
174
240
  }
@@ -228,6 +294,8 @@ export interface EntryInput {
228
294
  canUnpublish: boolean;
229
295
  canDelete: boolean;
230
296
  actions: AdminAction<any>[];
297
+ /** Its children and the host's related rows. */
298
+ belongings: Belonging[];
231
299
  notice?: string;
232
300
  problems?: string[];
233
301
  }
@@ -266,7 +334,7 @@ export function entryPage(shell: Shell, input: EntryInput): Html {
266
334
  input.canDelete
267
335
  ? html`<form method="get" action="${base}/${entry.id}/delete"><button type="submit" data-tone="danger" data-variant="outline">Delete</button></form>`
268
336
  : ''
269
- }</section>`
337
+ }</section>${belongings(input.belongings)}`
270
338
  : ''
271
339
  }`
272
340
  });
@@ -311,17 +379,26 @@ export function externalListPage(
311
379
  rows: ExternalRow[],
312
380
  offset: number,
313
381
  limit: number,
314
- hasNext: boolean
382
+ hasNext: boolean,
383
+ columns?: ListColumn[],
384
+ refs?: ResolveRef
315
385
  ): Html {
316
386
  const base = `${shell.mount}/x/${collection.id}`;
317
387
  const label = (r: ExternalRow) => String(r[collection.displayField] ?? '') || r.id;
388
+ const resolve: ResolveRef = refs ?? ((_, id) => ({ href: null, label: id }));
318
389
  return page(shell, {
319
390
  title: collection.name,
320
391
  body: html`${
321
392
  rows.length === 0
322
393
  ? html`<p>No rows.</p>`
323
- : html`<table><thead><tr><th scope="col">${collection.fields[collection.displayField]?.meta.label ?? 'Name'}</th><th scope="col">State</th></tr></thead><tbody>${rows.map(
324
- r => html`<tr><td><a href="${base}/${r.id}">${label(r)}</a></td><td>${statusBadge(typeof r.state === 'string' ? r.state : r.status)}</td></tr>`
394
+ : html`<table><thead><tr><th scope="col">${collection.fields[collection.displayField]?.meta.label ?? 'Name'}</th>${
395
+ columns ? columns.map(col => html`<th scope="col">${col.label}</th>`) : html`<th scope="col">State</th>`
396
+ }</tr></thead><tbody>${rows.map(
397
+ r => html`<tr><td><a href="${base}/${r.id}">${label(r)}</a></td>${
398
+ columns
399
+ ? columns.map(col => html`<td>${cell(col, r[col.name], resolve)}</td>`)
400
+ : html`<td>${statusBadge(typeof r.state === 'string' ? r.state : r.status)}</td>`
401
+ }</tr>`
325
402
  )}</tbody></table>`
326
403
  }<nav class="cluster" aria-label="Pages">${offset > 0 ? html`<a href="${base}${query({ offset: Math.max(offset - limit, 0) })}">Previous</a>` : ''}${
327
404
  hasNext ? html`<a href="${base}${query({ offset: offset + limit })}">Next</a>` : ''
@@ -335,7 +412,8 @@ export function externalRowPage(
335
412
  row: ExternalRow,
336
413
  refs: ResolveRef,
337
414
  actions: AdminAction<any>[],
338
- notice?: string
415
+ notice?: string,
416
+ related: Belonging[] = []
339
417
  ): Html {
340
418
  const base = `${shell.mount}/x/${collection.id}`;
341
419
  const title = String(row[collection.displayField] ?? '') || row.id;
@@ -355,6 +433,6 @@ export function externalRowPage(
355
433
  a => html`<form method="get" action="${base}/${row.id}/do/${a.id}"><button type="submit" data-tone="${a.destructive ? 'danger' : 'secondary'}" data-variant="${a.destructive ? 'outline' : 'solid'}">${a.label}</button></form>`
356
434
  )}</section>`
357
435
  : ''
358
- }`
436
+ }${belongings(related)}`
359
437
  });
360
438
  }
@@ -79,7 +79,7 @@ export interface ContentType<F extends Fields = Fields> {
79
79
  readonly singleton?: boolean;
80
80
  /** Entries have an owner; the type must then have an `access` function. */
81
81
  readonly owned: boolean;
82
- /** Fields conditions may name; each becomes a generated column (print-migration.ts prints it). */
82
+ /** Fields conditions may name; each becomes a generated column (cogens-print-migration prints it). */
83
83
  readonly indexed: readonly string[];
84
84
  /** The slug is unique among this type's entries. */
85
85
  readonly uniqueSlug: boolean;
@@ -30,7 +30,10 @@ export interface FieldMeta {
30
30
  /** A system field is writable only by a caller whose rule lists it by name. */
31
31
  system: boolean;
32
32
  refContentType?: string;
33
+ /** A select's stored values, in order. */
33
34
  options?: string[];
35
+ /** A select's label for each value that has one; a value with none is shown as itself. */
36
+ optionLabels?: Record<string, string>;
34
37
  /** Stored when a create leaves the field out; set by the store, so a system field may have one. */
35
38
  default?: unknown;
36
39
  }
@@ -85,6 +88,10 @@ export interface StoredFile {
85
88
  url: string;
86
89
  }
87
90
 
91
+ /** A select's option: its stored value, or the value with the label the admin shows for it. */
92
+ export type SelectOption = string | { readonly value: string; readonly label: string };
93
+ type OptionValue<O> = O extends string ? O : O extends { readonly value: infer V extends string } ? V : never;
94
+
88
95
  export const field = {
89
96
  text: (label: string) => make<string>(label, 'text', { type: 'string' }),
90
97
  longText: (label: string) => make<string>(label, 'longText', { type: 'string' }),
@@ -96,8 +103,18 @@ export const field = {
96
103
  number: (label: string) => make<number>(label, 'number', { type: 'number' }),
97
104
  /** An ISO 8601 date and time. */
98
105
  date: (label: string) => make<string>(label, 'date', { type: 'string', format: 'date-time' }),
99
- select: <const O extends readonly string[]>(label: string, opts: { options: O }) =>
100
- make<O[number]>(label, 'select', { type: 'string', enum: [...opts.options] }, { options: [...opts.options] }),
106
+ /**
107
+ * One of a fixed list. An option is its stored value, or `{ value, label }`: the admin shows and offers the label
108
+ * and stores the value.
109
+ */
110
+ select: <const O extends readonly SelectOption[]>(label: string, opts: { options: O }) => {
111
+ const values = opts.options.map(o => (typeof o === 'string' ? o : o.value));
112
+ const labelled = opts.options.filter((o): o is Exclude<SelectOption, string> => typeof o !== 'string');
113
+ return make<OptionValue<O[number]>>(label, 'select', { type: 'string', enum: values }, {
114
+ options: values,
115
+ ...(labelled.length > 0 ? { optionLabels: Object.fromEntries(labelled.map(o => [o.value, o.label])) } : {})
116
+ });
117
+ },
101
118
  /** The id of an entry of another type. */
102
119
  ref: (contentType: string, label: string) =>
103
120
  make<string>(label, 'reference', { type: 'string' }, { refContentType: contentType }),
package/src/core/index.ts CHANGED
@@ -14,7 +14,7 @@ export type {
14
14
  CmsConfig, ContentType, ContentTypeOptions, Fields, GroupConfig, Infer, ServiceAction, ServiceConfig
15
15
  } from './config/define.ts';
16
16
  export { field, FieldBuilder } from './config/field.ts';
17
- export type { FieldMeta, FieldType, StoredFile } from './config/field.ts';
17
+ export type { FieldMeta, FieldType, SelectOption, StoredFile } from './config/field.ts';
18
18
 
19
19
  export { OPERATIONS } from './access/can.ts';
20
20
  export type { AccessContext, AccessFn, Can, Grant, GrantOptions } from './access/can.ts';
@@ -160,7 +160,7 @@ async function check(db: D1Database, types: ContentType<any>[]): Promise<void> {
160
160
  for (const field of type.indexed) {
161
161
  if (!columns.has(indexedColumn(field))) {
162
162
  throw new ConfigError(
163
- `Type "${type.id}" indexes "${field}", but the entries table has no column "${indexedColumn(field)}". Print the lines to apply with cogens-print-migration (scripts/print-migration.ts).`,
163
+ `Type "${type.id}" indexes "${field}", but the entries table has no column "${indexedColumn(field)}". Print the lines to apply with cogens-print-migration.`,
164
164
  type.id,
165
165
  field
166
166
  );
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * `cogens-print-migration <config file>`: prints the SQL the config's indexed fields need (see `index.ts`).
4
+ *
5
+ * The config's default export is `{ contentTypes }` (what `defineConfig` returns). Under Bun any config file
6
+ * loads, `.ts` included (`bunx cogens-print-migration site.config.ts`). Under Node the file is imported as
7
+ * is: `.js` and `.mjs` always work, and a `.ts` config only on a Node that strips types (22.18 or later,
8
+ * 24 and up) when the config's own imports are plain relative `.ts` or package imports.
9
+ */
10
+ import { pathToFileURL } from 'node:url';
11
+ import { resolve } from 'node:path';
12
+ import type { ContentType } from '../core/config/define.ts';
13
+ import { migrationLines } from './index.ts';
14
+
15
+ // The declaration keeps this file free of Node's types, which a Worker build does not have.
16
+ declare const process: { argv: string[]; cwd(): string; exitCode?: number };
17
+
18
+ const path = process.argv[2];
19
+ if (!path) {
20
+ console.error('Usage: cogens-print-migration <config file>');
21
+ process.exitCode = 1;
22
+ } else {
23
+ try {
24
+ const module = await import(pathToFileURL(resolve(process.cwd(), path)).href);
25
+ const config = module.default as { contentTypes?: ContentType<any>[] } | undefined;
26
+ if (!config || !Array.isArray(config.contentTypes)) {
27
+ throw new Error(`${path} must default-export { contentTypes } (what defineConfig returns).`);
28
+ }
29
+ console.log(migrationLines(config.contentTypes).join('\n'));
30
+ } catch (error) {
31
+ console.error(error instanceof Error ? error.message : String(error));
32
+ process.exitCode = 1;
33
+ }
34
+ }
@@ -1,16 +1,17 @@
1
- #!/usr/bin/env bun
2
1
  /**
3
- * Prints the SQL a config's content types need in `entries`: a virtual generated
2
+ * `cogens/migrations-tool`: the SQL a config's content types need in `entries`: a virtual generated
4
3
  * column and an index for each indexed field, and a unique index on the slug for
5
4
  * the types that ask for one. The host's own migration holds these lines and startup checks the columns exist.
6
5
  *
6
+ * `cogens-print-migration <config>` (bin.ts) prints them for a config file:
7
+ *
7
8
  * bunx cogens-print-migration site.config.ts
8
- * bun run node_modules/cogens/scripts/print-migration.ts site.config.ts
9
+ * npx cogens-print-migration site.config.mjs
9
10
  *
10
11
  * The config file's default export is `{ contentTypes }` (what `defineConfig` returns).
11
12
  */
12
- import type { ContentType } from '../src/core/config/define.ts';
13
- import { indexedColumn } from '../src/core/columns.ts';
13
+ import type { ContentType } from '../core/config/define.ts';
14
+ import { indexedColumn } from '../core/columns.ts';
14
15
 
15
16
  /** `ALTER TABLE ... ADD COLUMN` allows only VIRTUAL generated columns; SQLite can index them. */
16
17
  export function migrationLines(types: readonly ContentType<any>[]): string[] {
@@ -34,17 +35,3 @@ export function migrationLines(types: readonly ContentType<any>[]): string[] {
34
35
  }
35
36
  return lines;
36
37
  }
37
-
38
- // Run under Bun; the declarations keep this file free of Node's types, which a Worker build does not have.
39
- declare const process: { argv: string[]; cwd(): string; exit(code: number): never };
40
-
41
- if ((import.meta as { main?: boolean }).main) {
42
- const path = process.argv[2];
43
- if (!path) {
44
- console.error('Usage: bunx cogens-print-migration <config file>');
45
- process.exit(1);
46
- }
47
- const module = await import(new URL(path, `file://${process.cwd()}/`).href);
48
- const config = module.default as { contentTypes: ContentType<any>[] };
49
- console.log(migrationLines(config.contentTypes).join('\n'));
50
- }