@nuasite/cms-core 0.53.0 → 0.55.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.
- package/dist/types/asset-paths.d.ts +28 -0
- package/dist/types/asset-paths.d.ts.map +1 -0
- package/dist/types/check-entries.d.ts +50 -0
- package/dist/types/check-entries.d.ts.map +1 -0
- package/dist/types/check-live.d.ts +25 -0
- package/dist/types/check-live.d.ts.map +1 -0
- package/dist/types/check-shape.d.ts +28 -0
- package/dist/types/check-shape.d.ts.map +1 -0
- package/dist/types/check-write.d.ts +35 -0
- package/dist/types/check-write.d.ts.map +1 -0
- package/dist/types/check.d.ts +18 -1
- package/dist/types/check.d.ts.map +1 -1
- package/dist/types/collection-scanner.d.ts.map +1 -1
- package/dist/types/content-config-ast.d.ts +7 -1
- package/dist/types/content-config-ast.d.ts.map +1 -1
- package/dist/types/editor-write-model.d.ts +48 -0
- package/dist/types/editor-write-model.d.ts.map +1 -0
- package/dist/types/handlers/entry-ops.d.ts.map +1 -1
- package/dist/types/index.d.ts +10 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/schema-port.d.ts +43 -0
- package/dist/types/schema-port.d.ts.map +1 -0
- package/dist/types/schema-probe.d.ts +53 -0
- package/dist/types/schema-probe.d.ts.map +1 -0
- package/dist/types/shared.d.ts +34 -10
- package/dist/types/shared.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -2
- package/src/asset-paths.ts +54 -0
- package/src/check-entries.ts +120 -0
- package/src/check-live.ts +61 -0
- package/src/check-shape.ts +137 -0
- package/src/check-write.ts +280 -0
- package/src/check.ts +192 -62
- package/src/collection-scanner.ts +14 -0
- package/src/content-config-ast.ts +301 -18
- package/src/editor-write-model.ts +82 -0
- package/src/handlers/entry-ops.ts +153 -73
- package/src/index.ts +41 -2
- package/src/schema-port.ts +46 -0
- package/src/schema-probe.ts +133 -0
- package/src/shared.ts +60 -18
|
@@ -3,8 +3,10 @@ import type * as t from '@babel/types'
|
|
|
3
3
|
import {
|
|
4
4
|
type CollectionLayout,
|
|
5
5
|
type CollectionLayoutSection,
|
|
6
|
+
type DerivedTransform,
|
|
6
7
|
type FieldHints,
|
|
7
8
|
type FieldType,
|
|
9
|
+
isDerivedTransform,
|
|
8
10
|
isFieldType,
|
|
9
11
|
type PathnameSegment,
|
|
10
12
|
type PathnameSpec,
|
|
@@ -43,11 +45,17 @@ export interface ParsedFieldLayout {
|
|
|
43
45
|
width?: 'full' | 'half'
|
|
44
46
|
order?: number
|
|
45
47
|
hidden?: boolean
|
|
48
|
+
/** Name of the field this one is computed from (`n.text({ derivedFrom: 'category' })`). */
|
|
49
|
+
derivedFrom?: string
|
|
50
|
+
/** Named transform for the derived value; absent means `slugifyHref`. */
|
|
51
|
+
derivedTransform?: DerivedTransform
|
|
46
52
|
}
|
|
47
53
|
|
|
48
54
|
export interface ParsedCollection {
|
|
49
55
|
name: string
|
|
50
56
|
fields: ParsedField[]
|
|
57
|
+
/** `fields` is known to be incomplete — a spread or computed key in the schema was skipped. */
|
|
58
|
+
partialFields?: true
|
|
51
59
|
loaderPattern?: string
|
|
52
60
|
loaderBase?: string
|
|
53
61
|
/** Declarative form layout from a `defineCmsCollection({ cms: { … } })` block. */
|
|
@@ -103,19 +111,113 @@ type Bindings = Map<string, t.Node>
|
|
|
103
111
|
|
|
104
112
|
/**
|
|
105
113
|
* Follow `Identifier` references through same-file `const` bindings until reaching
|
|
106
|
-
* a non-Identifier node
|
|
107
|
-
*
|
|
114
|
+
* a non-Identifier node, seeing through type-only wrappers (`as const`, `satisfies`,
|
|
115
|
+
* `!`) on the way — they change nothing about the value a schema declares. Cycle-safe
|
|
116
|
+
* via the visited set. Returns the node it stopped on — the peeled original when the
|
|
117
|
+
* identifier is unbound or already visited.
|
|
108
118
|
*/
|
|
109
119
|
function resolveExpression(node: t.Node, bindings: Bindings, visited: Set<string> = new Set()): t.Node {
|
|
110
120
|
let current: t.Node = node
|
|
111
|
-
|
|
121
|
+
for (;;) {
|
|
122
|
+
if (
|
|
123
|
+
current.type === 'TSAsExpression'
|
|
124
|
+
|| current.type === 'TSSatisfiesExpression'
|
|
125
|
+
|| current.type === 'TSNonNullExpression'
|
|
126
|
+
|| current.type === 'TSTypeAssertion'
|
|
127
|
+
) {
|
|
128
|
+
current = current.expression
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
if (current.type !== 'Identifier') return current
|
|
112
132
|
if (visited.has(current.name)) return current
|
|
113
133
|
visited.add(current.name)
|
|
114
134
|
const next = bindings.get(current.name)
|
|
115
135
|
if (!next) return current
|
|
116
136
|
current = next
|
|
117
137
|
}
|
|
118
|
-
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function isAstNode(value: unknown): value is t.Node {
|
|
141
|
+
return typeof value === 'object' && value !== null && 'type' in value && typeof value.type === 'string'
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Visit `node` and everything beneath it, finding children by shape — no @babel/traverse dependency. */
|
|
145
|
+
function walkNodes(node: t.Node, visit: (n: t.Node) => void): void {
|
|
146
|
+
visit(node)
|
|
147
|
+
const children: unknown[] = Object.values(node)
|
|
148
|
+
for (const child of children) {
|
|
149
|
+
if (Array.isArray(child)) {
|
|
150
|
+
for (const item of child) {
|
|
151
|
+
if (isAstNode(item)) walkNodes(item, visit)
|
|
152
|
+
}
|
|
153
|
+
} else if (isAstNode(child)) {
|
|
154
|
+
walkNodes(child, visit)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Record every name a binding construct introduces, destructuring patterns included. */
|
|
160
|
+
function declaredPatternNames(node: t.Node, record: (name: string) => void): void {
|
|
161
|
+
switch (node.type) {
|
|
162
|
+
case 'Identifier':
|
|
163
|
+
record(node.name)
|
|
164
|
+
return
|
|
165
|
+
case 'ObjectPattern':
|
|
166
|
+
for (const prop of node.properties) declaredPatternNames(prop.type === 'RestElement' ? prop.argument : prop.value, record)
|
|
167
|
+
return
|
|
168
|
+
case 'ArrayPattern':
|
|
169
|
+
for (const el of node.elements) {
|
|
170
|
+
if (el) declaredPatternNames(el, record)
|
|
171
|
+
}
|
|
172
|
+
return
|
|
173
|
+
case 'AssignmentPattern':
|
|
174
|
+
declaredPatternNames(node.left, record)
|
|
175
|
+
return
|
|
176
|
+
case 'RestElement':
|
|
177
|
+
declaredPatternNames(node.argument, record)
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Names the file declares more than once, anywhere.
|
|
184
|
+
*
|
|
185
|
+
* `bindings` is module-scope only, so a same-named local — a `const` inside the schema
|
|
186
|
+
* callback, a parameter — is invisible to it and an identifier would resolve to the
|
|
187
|
+
* module-level declaration instead: the wrong option list, the wrong type, the wrong
|
|
188
|
+
* `required` flag. Modelling scopes properly is a bigger job than this parser wants, so
|
|
189
|
+
* a name declared twice is simply not resolved and the field degrades as it did before.
|
|
190
|
+
*/
|
|
191
|
+
function shadowedNames(ast: t.File): Set<string> {
|
|
192
|
+
const seen = new Set<string>()
|
|
193
|
+
const shadowed = new Set<string>()
|
|
194
|
+
const record = (name: string): void => {
|
|
195
|
+
if (seen.has(name)) shadowed.add(name)
|
|
196
|
+
seen.add(name)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
walkNodes(ast, node => {
|
|
200
|
+
switch (node.type) {
|
|
201
|
+
case 'VariableDeclarator':
|
|
202
|
+
declaredPatternNames(node.id, record)
|
|
203
|
+
return
|
|
204
|
+
case 'FunctionDeclaration':
|
|
205
|
+
case 'FunctionExpression':
|
|
206
|
+
case 'ArrowFunctionExpression':
|
|
207
|
+
for (const param of node.params) declaredPatternNames(param, record)
|
|
208
|
+
if (node.type !== 'ArrowFunctionExpression' && node.id) record(node.id.name)
|
|
209
|
+
return
|
|
210
|
+
case 'ClassDeclaration':
|
|
211
|
+
if (node.id) record(node.id.name)
|
|
212
|
+
return
|
|
213
|
+
case 'ImportSpecifier':
|
|
214
|
+
case 'ImportDefaultSpecifier':
|
|
215
|
+
case 'ImportNamespaceSpecifier':
|
|
216
|
+
record(node.local.name)
|
|
217
|
+
return
|
|
218
|
+
}
|
|
219
|
+
})
|
|
220
|
+
return shadowed
|
|
119
221
|
}
|
|
120
222
|
|
|
121
223
|
/**
|
|
@@ -219,6 +321,10 @@ export function parseConfigSource(source: string, _sourcePath?: string): ParsedC
|
|
|
219
321
|
}
|
|
220
322
|
}
|
|
221
323
|
|
|
324
|
+
// A name the file declares twice cannot be resolved to one binding without a scope
|
|
325
|
+
// model, so drop it and let the fields that mention it stay untyped.
|
|
326
|
+
for (const name of shadowedNames(ast)) bindings.delete(name)
|
|
327
|
+
|
|
222
328
|
// Unify both styles: inline `name: defineCollection({...})` and the
|
|
223
329
|
// `const x = defineCollection({...}); collections = { name: x }` reference form.
|
|
224
330
|
const collectionObjects = new Map<string, t.ObjectExpression>(inlineCollections)
|
|
@@ -298,9 +404,16 @@ export function parseConfigSource(source: string, _sourcePath?: string): ParsedC
|
|
|
298
404
|
continue
|
|
299
405
|
}
|
|
300
406
|
|
|
407
|
+
const fields = parseSchemaFields(schemaObject, bindings)
|
|
408
|
+
// Once per parse of the config file, not once per entry: the parse result is cached by
|
|
409
|
+
// mtime, so a bad declaration is reported when it is read, not on every scan that
|
|
410
|
+
// consults it.
|
|
411
|
+
checkDerivedFromDeclarations(collectionName, fields)
|
|
412
|
+
|
|
301
413
|
result.set(collectionName, {
|
|
302
414
|
name: collectionName,
|
|
303
|
-
fields
|
|
415
|
+
fields,
|
|
416
|
+
...(hasSkippedMembers(schemaObject) ? { partialFields: true as const } : {}),
|
|
304
417
|
loaderPattern,
|
|
305
418
|
loaderBase,
|
|
306
419
|
layout,
|
|
@@ -361,6 +474,23 @@ function parseLayoutSection(node: t.Node): CollectionLayoutSection | null {
|
|
|
361
474
|
return collapsed ? { title, fields, collapsed } : { title, fields }
|
|
362
475
|
}
|
|
363
476
|
|
|
477
|
+
/**
|
|
478
|
+
* The complete option list of an `enum([…])` array, or null when any member is unreadable.
|
|
479
|
+
*
|
|
480
|
+
* All-or-nothing on purpose: these options become a *closed* set downstream, so reading
|
|
481
|
+
* `[...BASE, 'blog']` as just `['blog']` would lock the field to the one member we
|
|
482
|
+
* understood — every existing entry invalid, and the editor refusing to write the values
|
|
483
|
+
* the schema does allow. A list we cannot read whole leaves the field open instead.
|
|
484
|
+
*/
|
|
485
|
+
function readEnumOptions(node: t.ArrayExpression): string[] | null {
|
|
486
|
+
const options: string[] = []
|
|
487
|
+
for (const el of node.elements) {
|
|
488
|
+
if (el?.type !== 'StringLiteral') return null
|
|
489
|
+
options.push(el.value)
|
|
490
|
+
}
|
|
491
|
+
return options.length > 0 ? options : null
|
|
492
|
+
}
|
|
493
|
+
|
|
364
494
|
/** Collect string-literal elements from an array expression. */
|
|
365
495
|
function stringArray(node: t.ArrayExpression): string[] {
|
|
366
496
|
const out: string[] = []
|
|
@@ -583,6 +713,18 @@ function parseSchemaFields(schemaObject: t.ObjectExpression, bindings: Bindings)
|
|
|
583
713
|
return fields
|
|
584
714
|
}
|
|
585
715
|
|
|
716
|
+
/**
|
|
717
|
+
* Whether members of the schema object were skipped, leaving the field list short.
|
|
718
|
+
*
|
|
719
|
+
* A spread (`n.object({ ...base, order: n.number() })`) or a computed key contributes real
|
|
720
|
+
* fields that never make it into `fields`. The difference matters to anything that treats
|
|
721
|
+
* the list as exhaustive: an incomplete list is not the same as an empty one, and reading
|
|
722
|
+
* it as complete turns a legitimate config into a page of warnings.
|
|
723
|
+
*/
|
|
724
|
+
function hasSkippedMembers(schemaObject: t.ObjectExpression): boolean {
|
|
725
|
+
return schemaObject.properties.some(prop => prop.type !== 'ObjectProperty' || !propertyKeyName(prop.key))
|
|
726
|
+
}
|
|
727
|
+
|
|
586
728
|
/**
|
|
587
729
|
* Walk a field's value expression. Each layer is either a wrapper method call
|
|
588
730
|
* (`.optional()`, `.default()`, `.nullable()`, `.nullish()`, `.orderBy(...)`)
|
|
@@ -592,7 +734,7 @@ function parseSchemaFields(schemaObject: t.ObjectExpression, bindings: Bindings)
|
|
|
592
734
|
* patterns like `cs: TestimonialTranslation` and `en: TestimonialTranslation.optional()`
|
|
593
735
|
* are followed back to their defining call.
|
|
594
736
|
*/
|
|
595
|
-
function analyzeFieldExpression(node: t.Node, field: ParsedField, bindings: Bindings): void {
|
|
737
|
+
function analyzeFieldExpression(node: t.Node, field: ParsedField, bindings: Bindings, helpers: Set<string> = new Set()): void {
|
|
596
738
|
let current: t.Node | null = resolveExpression(node, bindings)
|
|
597
739
|
while (current) {
|
|
598
740
|
if (current.type !== 'CallExpression') return
|
|
@@ -602,10 +744,34 @@ function analyzeFieldExpression(node: t.Node, field: ParsedField, bindings: Bind
|
|
|
602
744
|
return
|
|
603
745
|
}
|
|
604
746
|
|
|
747
|
+
// `tag: optionalTag()` — continue into what the helper returns, so both its type and
|
|
748
|
+
// the `.optional()` it hides reach the field.
|
|
749
|
+
if (current.callee.type === 'Identifier') {
|
|
750
|
+
const returned = sameFileHelperReturn(current, bindings, helpers)
|
|
751
|
+
if (returned) analyzeFieldExpression(returned, field, bindings, helpers)
|
|
752
|
+
return
|
|
753
|
+
}
|
|
754
|
+
|
|
605
755
|
if (current.callee.type !== 'MemberExpression') return
|
|
606
756
|
const property = current.callee.property
|
|
607
757
|
const methodName = property.type === 'Identifier' ? property.name : ''
|
|
608
758
|
|
|
759
|
+
// `n.text().array()` — the postfix spelling of `n.array(n.text())`. Without this the
|
|
760
|
+
// walk steps into the receiver and stamps the *element* type on the list itself.
|
|
761
|
+
if (methodName === 'array') {
|
|
762
|
+
const item: ParsedField = { name: '__item__', required: true }
|
|
763
|
+
analyzeFieldExpression(current.callee.object, item, bindings, helpers)
|
|
764
|
+
// Keep an array of references flat, exactly as the `n.array(reference())` branch does.
|
|
765
|
+
if (item.reference) {
|
|
766
|
+
field.reference = { target: item.reference.target, isArray: true }
|
|
767
|
+
return
|
|
768
|
+
}
|
|
769
|
+
field.type = 'array'
|
|
770
|
+
if (item.type) field.itemType = item.type
|
|
771
|
+
if (item.fields) field.fields = item.fields
|
|
772
|
+
return
|
|
773
|
+
}
|
|
774
|
+
|
|
609
775
|
if (WRAPPER_METHODS.has(methodName)) {
|
|
610
776
|
field.required = false
|
|
611
777
|
} else if (methodName === 'orderBy') {
|
|
@@ -618,6 +784,28 @@ function analyzeFieldExpression(node: t.Node, field: ParsedField, bindings: Bind
|
|
|
618
784
|
}
|
|
619
785
|
}
|
|
620
786
|
|
|
787
|
+
/**
|
|
788
|
+
* The expression a same-file zero-argument helper returns, for field values written as
|
|
789
|
+
* `tag: optionalTag()`. Cycle-safe via `seen`. A helper taking arguments, or one imported
|
|
790
|
+
* from another module, resolves to null — its body is not here to read, and guessing at a
|
|
791
|
+
* type the schema may not declare is worse than leaving the field untyped.
|
|
792
|
+
*/
|
|
793
|
+
function sameFileHelperReturn(call: t.CallExpression, bindings: Bindings, seen: Set<string>): t.Node | null {
|
|
794
|
+
if (call.callee.type !== 'Identifier' || call.arguments.length > 0) return null
|
|
795
|
+
if (seen.has(call.callee.name)) return null
|
|
796
|
+
seen.add(call.callee.name)
|
|
797
|
+
|
|
798
|
+
const fn = resolveExpression(call.callee, bindings)
|
|
799
|
+
if (fn.type !== 'ArrowFunctionExpression' && fn.type !== 'FunctionExpression') return null
|
|
800
|
+
if (fn.params.length > 0) return null
|
|
801
|
+
|
|
802
|
+
if (fn.body.type !== 'BlockStatement') return fn.body
|
|
803
|
+
for (const stmt of fn.body.body) {
|
|
804
|
+
if (stmt.type === 'ReturnStatement' && stmt.argument) return stmt.argument
|
|
805
|
+
}
|
|
806
|
+
return null
|
|
807
|
+
}
|
|
808
|
+
|
|
621
809
|
/**
|
|
622
810
|
* A "base call" is the innermost call that defines the field's type: a Zod/n
|
|
623
811
|
* helper invocation or a bare `image()` / `reference()` from a callback param.
|
|
@@ -660,8 +848,9 @@ function analyzeBaseCall(node: t.CallExpression, field: ParsedField, bindings: B
|
|
|
660
848
|
const fn = callee.property.name
|
|
661
849
|
|
|
662
850
|
// n.image(), n.url(), n.text(...), etc. — semantic field types from @nuasite/cms.
|
|
663
|
-
// FIELD_HELPER_TYPES gates to the helper subset (excludes
|
|
664
|
-
//
|
|
851
|
+
// FIELD_HELPER_TYPES gates to the helper subset (excludes select/array/object/reference,
|
|
852
|
+
// inferred below, and boolean, which `z` declares the same way); isFieldType narrows
|
|
853
|
+
// `fn` to FieldType.
|
|
665
854
|
if (ns === 'n' && FIELD_HELPER_TYPES.has(fn) && isFieldType(fn)) {
|
|
666
855
|
field.type = fn
|
|
667
856
|
const firstArg = node.arguments[0]
|
|
@@ -674,18 +863,20 @@ function analyzeBaseCall(node: t.CallExpression, field: ParsedField, bindings: B
|
|
|
674
863
|
return
|
|
675
864
|
}
|
|
676
865
|
|
|
866
|
+
// (z|n).boolean() → checkbox. Not in FIELD_HELPER_TYPES because that gate is `n`-only,
|
|
867
|
+
// and a plain `z.boolean()` declares the very same field.
|
|
868
|
+
if ((ns === 'z' || ns === 'n') && fn === 'boolean') {
|
|
869
|
+
field.type = 'boolean'
|
|
870
|
+
return
|
|
871
|
+
}
|
|
872
|
+
|
|
677
873
|
// (z|n).enum([...]) → select with options
|
|
678
874
|
if ((ns === 'z' || ns === 'n') && fn === 'enum') {
|
|
679
|
-
const arg = node.arguments[0]
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
}
|
|
685
|
-
if (options.length > 0) {
|
|
686
|
-
field.type = 'select'
|
|
687
|
-
field.options = options
|
|
688
|
-
}
|
|
875
|
+
const arg = node.arguments[0] ? resolveExpression(node.arguments[0], bindings) : undefined
|
|
876
|
+
const options = arg?.type === 'ArrayExpression' ? readEnumOptions(arg) : null
|
|
877
|
+
if (options) {
|
|
878
|
+
field.type = 'select'
|
|
879
|
+
field.options = options
|
|
689
880
|
}
|
|
690
881
|
// `n.enum([…], { label, help, … })` — the value list takes the first argument, so
|
|
691
882
|
// layout hints ride in the second one; every other marker carries them in the first.
|
|
@@ -819,7 +1010,99 @@ function parseFieldLayoutFromObject(obj: t.ObjectExpression): ParsedFieldLayout
|
|
|
819
1010
|
case 'hidden':
|
|
820
1011
|
if (value.type === 'BooleanLiteral') layout.hidden = value.value
|
|
821
1012
|
break
|
|
1013
|
+
case 'derivedFrom':
|
|
1014
|
+
parseDerivedFromValue(value, layout)
|
|
1015
|
+
break
|
|
822
1016
|
}
|
|
823
1017
|
}
|
|
824
1018
|
return Object.keys(layout).length > 0 ? layout : undefined
|
|
825
1019
|
}
|
|
1020
|
+
|
|
1021
|
+
/**
|
|
1022
|
+
* Read a `derivedFrom` option in either authoring form:
|
|
1023
|
+
*
|
|
1024
|
+
* ```ts
|
|
1025
|
+
* n.text({ derivedFrom: 'category' }) // → slugifyHref
|
|
1026
|
+
* n.text({ derivedFrom: { field: 'category', transform: 'slug' } })
|
|
1027
|
+
* ```
|
|
1028
|
+
*
|
|
1029
|
+
* `field` is mandatory in the object form — without a source there is nothing to derive
|
|
1030
|
+
* from, so the whole declaration is dropped. An unknown `transform` name is ignored rather
|
|
1031
|
+
* than rejected: the field keeps deriving, on the default transform, instead of silently
|
|
1032
|
+
* dropping out of the recompute because of a typo.
|
|
1033
|
+
*/
|
|
1034
|
+
function parseDerivedFromValue(value: t.ObjectProperty['value'], layout: ParsedFieldLayout): void {
|
|
1035
|
+
if (value.type === 'StringLiteral') {
|
|
1036
|
+
if (value.value) layout.derivedFrom = value.value
|
|
1037
|
+
return
|
|
1038
|
+
}
|
|
1039
|
+
if (value.type !== 'ObjectExpression') return
|
|
1040
|
+
|
|
1041
|
+
let field: string | undefined
|
|
1042
|
+
let transform: DerivedTransform | undefined
|
|
1043
|
+
for (const prop of value.properties) {
|
|
1044
|
+
if (prop.type !== 'ObjectProperty') continue
|
|
1045
|
+
const key = propertyKeyName(prop.key)
|
|
1046
|
+
if (prop.value.type !== 'StringLiteral') continue
|
|
1047
|
+
if (key === 'field') field = prop.value.value
|
|
1048
|
+
else if (key === 'transform' && isDerivedTransform(prop.value.value)) transform = prop.value.value
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
if (!field) return
|
|
1052
|
+
layout.derivedFrom = field
|
|
1053
|
+
if (transform) layout.derivedTransform = transform
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* Report the `derivedFrom` declarations the recompute cannot honour, and drop the ones that
|
|
1058
|
+
* would otherwise take effect halfway.
|
|
1059
|
+
*
|
|
1060
|
+
* Two kinds, both silent before:
|
|
1061
|
+
*
|
|
1062
|
+
* - **Unknown source.** `derivedFrom: 'catgory'` parses perfectly well, so the field is
|
|
1063
|
+
* hidden (a declaration implies `hidden`) and then never computed, because no such key ever
|
|
1064
|
+
* appears in the frontmatter. The result is a field that has quietly left the CMS. Warn
|
|
1065
|
+
* rather than throw: a typo in one field must not take the whole config down, and the site
|
|
1066
|
+
* still builds.
|
|
1067
|
+
* - **Nested field.** Deriving is a top-level operation — `applyDerivedFields` reads the
|
|
1068
|
+
* entry's top-level frontmatter, which is the only place a source name is unambiguous.
|
|
1069
|
+
* A `derivedFrom` inside `n.object({ … })` used to hide the sub-field anyway and then never
|
|
1070
|
+
* compute it, so the key disappeared from the file on the next write. Rather than invent
|
|
1071
|
+
* "source relative to the parent object" semantics, the declaration is dropped here: the
|
|
1072
|
+
* nested field stays visible and hand-editable, and the warning says why.
|
|
1073
|
+
*
|
|
1074
|
+
* Emitted over `console.warn` for the same reason as `warnOnPathnameCollisions` in
|
|
1075
|
+
* `collection-scanner.ts`: `cms-core` cannot reach the ErrorCollector in `@nuasite/cms` and
|
|
1076
|
+
* must not grow a dependency on it.
|
|
1077
|
+
*/
|
|
1078
|
+
function checkDerivedFromDeclarations(collectionName: string, fields: ParsedField[]): void {
|
|
1079
|
+
const siblingNames = new Set(fields.map(f => f.name))
|
|
1080
|
+
for (const field of fields) {
|
|
1081
|
+
const source = field.layout?.derivedFrom
|
|
1082
|
+
if (source !== undefined && !siblingNames.has(source)) {
|
|
1083
|
+
console.warn(
|
|
1084
|
+
`[cms] collection "${collectionName}": field "${field.name}" derives from "${source}", which is not a field of this collection`
|
|
1085
|
+
+ ` — the field is hidden and nothing is ever computed for it`,
|
|
1086
|
+
)
|
|
1087
|
+
}
|
|
1088
|
+
dropNestedDerivedFrom(collectionName, field.name, field.fields)
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
/** Strip `derivedFrom` from every field below the top level, warning once per declaration. */
|
|
1093
|
+
function dropNestedDerivedFrom(collectionName: string, path: string, fields: ParsedField[] | undefined): void {
|
|
1094
|
+
if (!fields) return
|
|
1095
|
+
for (const field of fields) {
|
|
1096
|
+
const fieldPath = `${path}.${field.name}`
|
|
1097
|
+
const layout = field.layout
|
|
1098
|
+
if (layout?.derivedFrom !== undefined) {
|
|
1099
|
+
console.warn(
|
|
1100
|
+
`[cms] collection "${collectionName}": field "${fieldPath}" declares \`derivedFrom\` on a nested field, which is not supported`
|
|
1101
|
+
+ ` — only top-level fields derive, so the declaration is ignored and the field stays editable`,
|
|
1102
|
+
)
|
|
1103
|
+
delete layout.derivedFrom
|
|
1104
|
+
delete layout.derivedTransform
|
|
1105
|
+
}
|
|
1106
|
+
dropNestedDerivedFrom(collectionName, fieldPath, field.fields)
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the editor actually writes when it creates an entry — the create-route half.
|
|
3
|
+
*
|
|
4
|
+
* The value rules underneath (what a blank field holds, which keys a repeater item carries,
|
|
5
|
+
* which required fields count as unfilled) live in `@nuasite/cms-types` so that every editor
|
|
6
|
+
* can reach them: `collections-admin` and webmaster's own collections panel depend on
|
|
7
|
+
* `cms-client`, which is a sibling of this package, not a consumer. They are re-exported here
|
|
8
|
+
* because the create pipeline reads as one thing.
|
|
9
|
+
*
|
|
10
|
+
* What stays is what only makes sense next to the create route: the form's starting
|
|
11
|
+
* frontmatter, the empty filter the request applies, and the keys the route injects.
|
|
12
|
+
*
|
|
13
|
+
* A check built on a second, drifted copy of any of this reports failures that cannot happen
|
|
14
|
+
* and misses the ones that can — which is why none of it may be reimplemented anywhere.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { defaultValueForNewEntry, type WriteModelField } from '@nuasite/cms-types'
|
|
18
|
+
|
|
19
|
+
export {
|
|
20
|
+
blankRequiredFields,
|
|
21
|
+
defaultValueForNewEntry,
|
|
22
|
+
isBlankFieldValue,
|
|
23
|
+
newRepeaterItem,
|
|
24
|
+
type RepeaterItemField,
|
|
25
|
+
type RequiredGuardField,
|
|
26
|
+
type WriteModelField,
|
|
27
|
+
} from '@nuasite/cms-types'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The frontmatter the editor holds when "create entry" opens, before the user types.
|
|
31
|
+
*
|
|
32
|
+
* `title` is excluded because the create form carries it in its own header field, and
|
|
33
|
+
* hidden fields are excluded because the form cannot fill them in. Neither is missing from
|
|
34
|
+
* the finished entry — see `applyCreateRouteFields`, which is what puts `title` back.
|
|
35
|
+
*/
|
|
36
|
+
export function newEntryFrontmatter(fields: WriteModelField[], today?: () => Date): Record<string, unknown> {
|
|
37
|
+
const frontmatter: Record<string, unknown> = {}
|
|
38
|
+
for (const field of fields) {
|
|
39
|
+
if (field.name === 'title' || field.hidden) continue
|
|
40
|
+
frontmatter[field.name] = defaultValueForNewEntry(field, today)
|
|
41
|
+
}
|
|
42
|
+
return frontmatter
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Whether the create route treats a collection's files as markdown entries or as plain data. */
|
|
46
|
+
export type CollectionKind = 'markdown' | 'data'
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The last step of a create: what the server adds before the file is written.
|
|
50
|
+
*
|
|
51
|
+
* A markdown entry gets the title from the form — which refuses to be blank — plus today's
|
|
52
|
+
* date, and the form's own frontmatter wins over both (`handlers/api-routes.ts`). A data
|
|
53
|
+
* entry is written verbatim, with the title already among its fields.
|
|
54
|
+
*
|
|
55
|
+
* Anything predicting a create has to apply this. Stopping at the editor's payload predicts
|
|
56
|
+
* an entry with no title, and then reports a required `title` as broken on every project.
|
|
57
|
+
*/
|
|
58
|
+
export function applyCreateRouteFields(
|
|
59
|
+
frontmatter: Record<string, unknown>,
|
|
60
|
+
kind: CollectionKind,
|
|
61
|
+
today: () => Date = () => new Date(),
|
|
62
|
+
): Record<string, unknown> {
|
|
63
|
+
const title = 'Untitled'
|
|
64
|
+
if (kind === 'data') return { title, ...frontmatter }
|
|
65
|
+
return { title, date: today().toISOString().split('T')[0], ...frontmatter }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* What survives the create request.
|
|
70
|
+
*
|
|
71
|
+
* The editor drops empty strings and `undefined` rather than writing them, so a field
|
|
72
|
+
* the user left alone reaches the schema as *absent*, not as `''`. Anything predicting
|
|
73
|
+
* a create has to apply this — the two produce different validation errors, and only
|
|
74
|
+
* one of them is real.
|
|
75
|
+
*/
|
|
76
|
+
export function omitEmptyOnCreate(frontmatter: Record<string, unknown>): Record<string, unknown> {
|
|
77
|
+
const out: Record<string, unknown> = {}
|
|
78
|
+
for (const [key, value] of Object.entries(frontmatter)) {
|
|
79
|
+
if (value !== undefined && value !== '') out[key] = value
|
|
80
|
+
}
|
|
81
|
+
return out
|
|
82
|
+
}
|