@nuasite/cms-core 0.56.3 → 0.57.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 (62) hide show
  1. package/dist/types/astro-source-analysis.d.ts +32 -0
  2. package/dist/types/astro-source-analysis.d.ts.map +1 -0
  3. package/dist/types/check-entries.d.ts +2 -0
  4. package/dist/types/check-entries.d.ts.map +1 -1
  5. package/dist/types/check-write.d.ts +2 -1
  6. package/dist/types/check-write.d.ts.map +1 -1
  7. package/dist/types/check.d.ts.map +1 -1
  8. package/dist/types/collection-scanner.d.ts +22 -2
  9. package/dist/types/collection-scanner.d.ts.map +1 -1
  10. package/dist/types/content-config-ast.d.ts +76 -2
  11. package/dist/types/content-config-ast.d.ts.map +1 -1
  12. package/dist/types/core.d.ts +19 -3
  13. package/dist/types/core.d.ts.map +1 -1
  14. package/dist/types/editor-write-model.d.ts +20 -0
  15. package/dist/types/editor-write-model.d.ts.map +1 -1
  16. package/dist/types/fs/glob.d.ts +17 -0
  17. package/dist/types/fs/glob.d.ts.map +1 -1
  18. package/dist/types/fs/node-fs.d.ts.map +1 -1
  19. package/dist/types/fs/types.d.ts +30 -2
  20. package/dist/types/fs/types.d.ts.map +1 -1
  21. package/dist/types/handlers/entry-ops.d.ts.map +1 -1
  22. package/dist/types/index.d.ts +8 -6
  23. package/dist/types/index.d.ts.map +1 -1
  24. package/dist/types/media/contember.d.ts.map +1 -1
  25. package/dist/types/media/errors.d.ts +17 -0
  26. package/dist/types/media/errors.d.ts.map +1 -0
  27. package/dist/types/media/index.d.ts +3 -0
  28. package/dist/types/media/index.d.ts.map +1 -1
  29. package/dist/types/media/local.d.ts.map +1 -1
  30. package/dist/types/media/names.d.ts +17 -0
  31. package/dist/types/media/names.d.ts.map +1 -0
  32. package/dist/types/media/s3.d.ts.map +1 -1
  33. package/dist/types/media/type-filter.d.ts +67 -0
  34. package/dist/types/media/type-filter.d.ts.map +1 -0
  35. package/dist/types/project-config-ast.d.ts +5 -1
  36. package/dist/types/project-config-ast.d.ts.map +1 -1
  37. package/dist/types/route-scan.d.ts +10 -0
  38. package/dist/types/route-scan.d.ts.map +1 -0
  39. package/dist/types/tsconfig.tsbuildinfo +1 -1
  40. package/package.json +2 -2
  41. package/src/astro-source-analysis.ts +562 -0
  42. package/src/check-entries.ts +16 -1
  43. package/src/check-write.ts +11 -5
  44. package/src/check.ts +14 -1
  45. package/src/collection-scanner.ts +413 -19
  46. package/src/content-config-ast.ts +616 -13
  47. package/src/core.ts +21 -5
  48. package/src/editor-write-model.ts +38 -0
  49. package/src/fs/glob.ts +39 -0
  50. package/src/fs/node-fs.ts +87 -11
  51. package/src/fs/types.ts +31 -2
  52. package/src/handlers/entry-ops.ts +17 -16
  53. package/src/index.ts +30 -3
  54. package/src/media/contember.ts +10 -2
  55. package/src/media/errors.ts +21 -0
  56. package/src/media/index.ts +10 -0
  57. package/src/media/local.ts +177 -79
  58. package/src/media/names.ts +24 -0
  59. package/src/media/s3.ts +33 -3
  60. package/src/media/type-filter.ts +186 -0
  61. package/src/project-config-ast.ts +185 -10
  62. package/src/route-scan.ts +32 -0
@@ -36,14 +36,30 @@ function collectBindings(ast: File): Bindings {
36
36
  return bindings
37
37
  }
38
38
 
39
- function resolveExpression(node: Node, bindings: Bindings, visited: Set<string> = new Set()): Node {
39
+ /**
40
+ * The expression under the wrappers that exist for the type checker only — `x as T`,
41
+ * `x satisfies T`, `x!`, `<T>x`. They are gone at runtime, so a config that works there must
42
+ * read the same here: `const LOCALE = 'cs' as const`, `defineConfig({ … } satisfies NuaConfig)`.
43
+ */
44
+ function withoutTypeWrappers(node: Node): Node {
40
45
  let current = node
46
+ while (
47
+ current.type === 'TSAsExpression' || current.type === 'TSSatisfiesExpression' || current.type === 'TSNonNullExpression'
48
+ || current.type === 'TSTypeAssertion'
49
+ ) {
50
+ current = current.expression
51
+ }
52
+ return current
53
+ }
54
+
55
+ function resolveExpression(node: Node, bindings: Bindings, visited: Set<string> = new Set()): Node {
56
+ let current = withoutTypeWrappers(node)
41
57
  while (current.type === 'Identifier') {
42
58
  if (visited.has(current.name)) return current
43
59
  visited.add(current.name)
44
60
  const next = bindings.get(current.name)
45
61
  if (!next) return current
46
- current = next
62
+ current = withoutTypeWrappers(next)
47
63
  }
48
64
  return current
49
65
  }
@@ -119,7 +135,170 @@ function configObjectFromDefaultExport(ast: File, bindings: Bindings): ObjectExp
119
135
  return null
120
136
  }
121
137
 
122
- /** Parse `nua.cms.cmsConfig.listStyles` from an Astro config source string. */
138
+ /**
139
+ * The integrations that take the CMS options as a call argument, by the module their default
140
+ * export comes from, with the path to those options inside the first argument:
141
+ * `nuaCms(options)` and `nua({ cms: options })`.
142
+ */
143
+ const CMS_INTEGRATION_OPTIONS_PATH: Record<string, string[]> = {
144
+ '@nuasite/cms': [],
145
+ '@nuasite/nua': ['cms'],
146
+ '@nuasite/nua/integration': ['cms'],
147
+ }
148
+
149
+ /** Local names the file imports a CMS-carrying integration under, mapped to its options path. */
150
+ function collectCmsIntegrationImports(ast: File): Map<string, string[]> {
151
+ const imports = new Map<string, string[]>()
152
+ for (const stmt of ast.program.body) {
153
+ if (stmt.type !== 'ImportDeclaration') continue
154
+ const optionsPath = CMS_INTEGRATION_OPTIONS_PATH[stmt.source.value]
155
+ if (!optionsPath) continue
156
+ for (const specifier of stmt.specifiers) {
157
+ // `import nuaCms from …` and `import { default as nuaCms } from …`. `@nuasite/nua` also
158
+ // exports its integration by name, as `nua`.
159
+ const importedName = specifier.type === 'ImportDefaultSpecifier'
160
+ ? 'default'
161
+ : specifier.type === 'ImportSpecifier'
162
+ ? propertyKeyName(specifier.imported)
163
+ : null
164
+ if (importedName === 'default' || (importedName === 'nua' && optionsPath.length > 0)) imports.set(specifier.local.name, optionsPath)
165
+ }
166
+ }
167
+ return imports
168
+ }
169
+
170
+ /**
171
+ * What the default export's config goes through before Astro sees it: the `defineConfig` of
172
+ * `@nuasite/nua/config`, which adds the nua integration on its own; Astro's, or no call at
173
+ * all, where a `nua` key means nothing; or a function this reader does not know.
174
+ */
175
+ function defaultExportWrapper(ast: File, bindings: Bindings): 'nua' | 'astro' | 'unknown' {
176
+ const exported = ast.program.body.find(stmt => stmt.type === 'ExportDefaultDeclaration')
177
+ const declaration = exported?.type === 'ExportDefaultDeclaration' ? resolveExpression(exported.declaration, bindings) : null
178
+ if (declaration?.type !== 'CallExpression') return 'astro'
179
+ // `(defineConfig as any)(…)` and `defineConfig!(…)` call the same function.
180
+ const callee = withoutTypeWrappers(declaration.callee)
181
+ if (callee.type !== 'Identifier') return 'unknown'
182
+ for (const stmt of ast.program.body) {
183
+ if (stmt.type !== 'ImportDeclaration' || !stmt.specifiers.some(specifier => specifier.local.name === callee.name)) continue
184
+ if (stmt.source.value === '@nuasite/nua/config') return 'nua'
185
+ if (stmt.source.value === 'astro/config') return 'astro'
186
+ }
187
+ return 'unknown'
188
+ }
189
+
190
+ /**
191
+ * A CMS instance as far as this reader is concerned: its `cmsConfig` object, `null` when it
192
+ * declares none that can be read, `undefined` when there is no instance at all.
193
+ */
194
+ type CmsInstance = ObjectExpression | null | undefined
195
+
196
+ /**
197
+ * The CMS instance a nua integration runs, given the value of its `cms` option (`null` when
198
+ * the option is not set). nua runs a CMS unless told `cms: false`; `true`, nothing, or a
199
+ * value that cannot be read are all an instance — only an object can carry a `cmsConfig`.
200
+ */
201
+ function nuaCmsInstance(cms: Node | null, bindings: Bindings): CmsInstance {
202
+ if (cms?.type === 'BooleanLiteral' && !cms.value) return undefined
203
+ return cms?.type === 'ObjectExpression' ? objectAtPath(cms, ['cmsConfig'], bindings) : null
204
+ }
205
+
206
+ /** The CMS instance the config's own `nua` key stands for, if anything reads that key. */
207
+ function wrapperCmsInstance(ast: File, configObject: ObjectExpression, bindings: Bindings): CmsInstance {
208
+ const wrapper = defaultExportWrapper(ast, bindings)
209
+ if (wrapper === 'astro') return undefined
210
+
211
+ const nua = objectPropertyValue(configObject, 'nua', bindings)
212
+ const cms = nua?.type === 'ObjectExpression' ? objectPropertyValue(nua, 'cms', bindings) : null
213
+ if (wrapper === 'unknown') {
214
+ // Somebody's own wrapper: whether it adds nua cannot be known from here. A `nua.cms`
215
+ // block is taken as a sign that it does; without one, nothing is assumed.
216
+ return cms?.type === 'ObjectExpression' ? nuaCmsInstance(cms, bindings) : undefined
217
+ }
218
+ // The nua wrapper adds the integration whether or not the config mentions `nua` — only
219
+ // `nua: false` stops it — so the instance is there even with nothing to read from.
220
+ if (nua?.type === 'BooleanLiteral' && !nua.value) return undefined
221
+ return nuaCmsInstance(cms, bindings)
222
+ }
223
+
224
+ /**
225
+ * The `cmsConfig` object of the CMS instance whose configuration the page ends up with, or
226
+ * null when that instance declares none (or none can be read).
227
+ *
228
+ * The CMS options reach the integration three ways: the `nua` key of `defineConfig` from
229
+ * `@nuasite/nua/config`, or the integration called directly in `integrations` —
230
+ * `nuaCms({ … })`, `nua({ cms: { … } })`. A call is recognised by where its callee is
231
+ * imported from, not by its name.
232
+ *
233
+ * A site picks one. A config that uses several runs several CMS instances, every one of them
234
+ * assigns `window.NuaCmsConfig`, and the page keeps the last assignment. Which one that is was
235
+ * established by running such configs through Astro's own dev setup, not by reading hook
236
+ * code: nua registers its CMS behind everything listed in `integrations`, so
237
+ *
238
+ * - nua's CMS — the wrapper's, or a listed `nua()` call's — beats every `nuaCms()` call,
239
+ * wherever it stands and whether or not it carries a `cmsConfig`;
240
+ * - without one, the last `nuaCms()` call is it.
241
+ *
242
+ * Exactly one object is the answer; `listStyles` from one instance and `locale` from another
243
+ * would describe a configuration no page ever has. Two nua integrations in one config (the
244
+ * wrapper plus a listed `nua()`, or `nua()` twice) is not a working setup — their page
245
+ * scripts collide — so nothing is promised for it beyond an answer.
246
+ */
247
+ function effectiveCmsConfig(ast: File, configObject: ObjectExpression, bindings: Bindings): ObjectExpression | null {
248
+ let fromNua = wrapperCmsInstance(ast, configObject, bindings)
249
+ let fromCms: CmsInstance
250
+
251
+ const integrations = objectPropertyValue(configObject, 'integrations', bindings)
252
+ const integrationImports = collectCmsIntegrationImports(ast)
253
+ for (const element of integrations?.type === 'ArrayExpression' ? integrations.elements : []) {
254
+ if (!element || element.type === 'SpreadElement') continue
255
+ const call = resolveExpression(element, bindings)
256
+ // The callee as it is at runtime: `(nuaCms as any)(…)` is a `nuaCms` call like any other.
257
+ const callee = call.type === 'CallExpression' ? withoutTypeWrappers(call.callee) : null
258
+ if (call.type !== 'CallExpression' || callee?.type !== 'Identifier') continue
259
+ const optionsPath = integrationImports.get(callee.name)
260
+ if (!optionsPath) continue
261
+
262
+ const firstArg = call.arguments[0]
263
+ const options = firstArg && firstArg.type !== 'SpreadElement' ? resolveExpression(firstArg, bindings) : null
264
+ const optionsObject = options?.type === 'ObjectExpression' ? options : null
265
+ if (optionsPath.length === 0) {
266
+ fromCms = optionsObject && objectAtPath(optionsObject, ['cmsConfig'], bindings)
267
+ } else if (fromNua === undefined) {
268
+ fromNua = nuaCmsInstance(optionsObject && objectPropertyValue(optionsObject, 'cms', bindings), bindings)
269
+ }
270
+ }
271
+ return fromNua !== undefined ? fromNua : fromCms ?? null
272
+ }
273
+
274
+ /** `listStyles` of the `cmsConfig` object; empty when absent or not an array. */
275
+ function parseConfigListStyles(cmsConfig: ObjectExpression | null, bindings: Bindings): CmsListStyle[] {
276
+ const listStylesNode = cmsConfig ? objectPropertyValue(cmsConfig, 'listStyles', bindings) : null
277
+ return listStylesNode?.type === 'ArrayExpression' ? parseListStyles(listStylesNode, bindings) : []
278
+ }
279
+
280
+ /** The value of `key` in `object`, when it is a non-empty static string. */
281
+ function staticStringOf(object: ObjectExpression | null, key: string, bindings: Bindings): string | undefined {
282
+ const node = object ? objectPropertyValue(object, key, bindings) : null
283
+ return (node && staticString(node, bindings)) || undefined
284
+ }
285
+
286
+ /**
287
+ * The project's language: `cmsConfig.locale` when declared, otherwise Astro's
288
+ * `i18n.defaultLocale`. The explicit key comes first because most sites are single-language
289
+ * and have no `i18n` block — adding one changes how Astro routes, which is not a lever to
290
+ * pull for the sake of sorting labels. A value that cannot be read statically counts as
291
+ * absent, so an unreadable explicit locale falls through to `i18n.defaultLocale`.
292
+ */
293
+ function parseLocale(cmsConfig: ObjectExpression | null, configObject: ObjectExpression, bindings: Bindings): string | undefined {
294
+ return staticStringOf(cmsConfig, 'locale', bindings) ?? staticStringOf(objectAtPath(configObject, ['i18n'], bindings), 'defaultLocale', bindings)
295
+ }
296
+
297
+ /**
298
+ * Parse the project-level CMS config from an Astro config source string: `listStyles` and
299
+ * the project's language (see {@link parseLocale}), from wherever the config hands the
300
+ * integration its `cmsConfig` (see {@link effectiveCmsConfig}).
301
+ */
123
302
  export function parseProjectCmsConfigSource(source: string): CmsConfig {
124
303
  const ast = parseSource(source)
125
304
  if (!ast) return EMPTY_CONFIG
@@ -128,13 +307,9 @@ export function parseProjectCmsConfigSource(source: string): CmsConfig {
128
307
  const configObject = configObjectFromDefaultExport(ast, bindings)
129
308
  if (!configObject) return EMPTY_CONFIG
130
309
 
131
- const cmsConfig = objectAtPath(configObject, ['nua', 'cms', 'cmsConfig'], bindings)
132
- if (!cmsConfig) return EMPTY_CONFIG
133
-
134
- const listStylesNode = objectPropertyValue(cmsConfig, 'listStyles', bindings)
135
- if (!listStylesNode || listStylesNode.type !== 'ArrayExpression') return EMPTY_CONFIG
136
-
137
- return { listStyles: parseListStyles(listStylesNode, bindings) }
310
+ const cmsConfig = effectiveCmsConfig(ast, configObject, bindings)
311
+ const locale = parseLocale(cmsConfig, configObject, bindings)
312
+ return { listStyles: parseConfigListStyles(cmsConfig, bindings), ...(locale ? { locale } : {}) }
138
313
  }
139
314
 
140
315
  const ASTRO_CONFIG_FILES = ['astro.config.ts', 'astro.config.mts', 'astro.config.mjs', 'astro.config.js']
@@ -0,0 +1,32 @@
1
+ import { analyzeAstroSource } from './astro-source-analysis'
2
+
3
+ /** Content collections referenced by an Astro route, split by route role. */
4
+ export interface RouteCollectionScan {
5
+ /** Collections read directly inside the exported `getStaticPaths` function. */
6
+ staticPaths: string[]
7
+ /** Every collection read through an `astro:content` import. */
8
+ all: string[]
9
+ }
10
+
11
+ function appendUnique(collections: string[], seen: Set<string>, collectionName: string): void {
12
+ if (seen.has(collectionName)) return
13
+ seen.add(collectionName)
14
+ collections.push(collectionName)
15
+ }
16
+
17
+ /** Find the content collections read by an Astro route's frontmatter. */
18
+ export function scanRouteCollections(source: string): RouteCollectionScan {
19
+ const analysis = analyzeAstroSource(source)
20
+ const all: string[] = []
21
+ const allSeen = new Set<string>()
22
+ const staticPaths: string[] = []
23
+ const staticPathsSeen = new Set<string>()
24
+
25
+ for (const call of analysis.collectionCalls) {
26
+ if (call.accessor !== 'getCollection' || call.collectionName === null) continue
27
+ appendUnique(all, allSeen, call.collectionName)
28
+ if (call.inExportedGetStaticPaths) appendUnique(staticPaths, staticPathsSeen, call.collectionName)
29
+ }
30
+
31
+ return { staticPaths, all }
32
+ }