@nuasite/cms-core 0.56.4 → 0.58.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/astro-source-analysis.d.ts +32 -0
- package/dist/types/astro-source-analysis.d.ts.map +1 -0
- package/dist/types/check-entries.d.ts +2 -0
- package/dist/types/check-entries.d.ts.map +1 -1
- package/dist/types/check.d.ts.map +1 -1
- package/dist/types/collection-scanner.d.ts +22 -2
- package/dist/types/collection-scanner.d.ts.map +1 -1
- package/dist/types/content-config-ast.d.ts +76 -2
- package/dist/types/content-config-ast.d.ts.map +1 -1
- package/dist/types/core.d.ts +19 -3
- package/dist/types/core.d.ts.map +1 -1
- package/dist/types/fs/glob.d.ts +17 -0
- package/dist/types/fs/glob.d.ts.map +1 -1
- package/dist/types/fs/node-fs.d.ts.map +1 -1
- package/dist/types/fs/types.d.ts +30 -2
- package/dist/types/fs/types.d.ts.map +1 -1
- package/dist/types/handlers/entry-ops.d.ts.map +1 -1
- package/dist/types/index.d.ts +7 -5
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/media/contember.d.ts.map +1 -1
- package/dist/types/media/errors.d.ts +17 -0
- package/dist/types/media/errors.d.ts.map +1 -0
- package/dist/types/media/index.d.ts +3 -0
- package/dist/types/media/index.d.ts.map +1 -1
- package/dist/types/media/local.d.ts.map +1 -1
- package/dist/types/media/names.d.ts +17 -0
- package/dist/types/media/names.d.ts.map +1 -0
- package/dist/types/media/s3.d.ts.map +1 -1
- package/dist/types/media/type-filter.d.ts +67 -0
- package/dist/types/media/type-filter.d.ts.map +1 -0
- package/dist/types/project-config-ast.d.ts +5 -1
- package/dist/types/project-config-ast.d.ts.map +1 -1
- package/dist/types/route-scan.d.ts +10 -0
- package/dist/types/route-scan.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/astro-source-analysis.ts +562 -0
- package/src/check-entries.ts +16 -1
- package/src/check.ts +14 -1
- package/src/collection-scanner.ts +413 -19
- package/src/content-config-ast.ts +616 -13
- package/src/core.ts +21 -5
- package/src/fs/glob.ts +39 -0
- package/src/fs/node-fs.ts +87 -11
- package/src/fs/types.ts +31 -2
- package/src/handlers/entry-ops.ts +11 -13
- package/src/index.ts +28 -3
- package/src/media/contember.ts +10 -2
- package/src/media/errors.ts +21 -0
- package/src/media/index.ts +10 -0
- package/src/media/local.ts +177 -79
- package/src/media/names.ts +24 -0
- package/src/media/s3.ts +33 -3
- package/src/media/type-filter.ts +186 -0
- package/src/project-config-ast.ts +185 -10
- package/src/route-scan.ts +32 -0
|
@@ -1,8 +1,26 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
CollectionDefinition,
|
|
3
|
+
CollectionEntryInfo,
|
|
4
|
+
CollectionScanReport,
|
|
5
|
+
FieldDefinition,
|
|
6
|
+
FieldType,
|
|
7
|
+
UnresolvedCollection,
|
|
8
|
+
} from '@nuasite/cms-types'
|
|
2
9
|
import path from 'node:path'
|
|
3
10
|
import { isMap, isPair, isScalar, parse as parseYaml, parseDocument } from 'yaml'
|
|
4
|
-
import {
|
|
5
|
-
|
|
11
|
+
import {
|
|
12
|
+
classifyEmptyContentConfig,
|
|
13
|
+
configOptionMentions,
|
|
14
|
+
CONTENT_CONFIG_PATHS,
|
|
15
|
+
type ContentConfigRead,
|
|
16
|
+
type DeclaredCollection,
|
|
17
|
+
type ParseCache,
|
|
18
|
+
type ParsedConfig,
|
|
19
|
+
type ParsedField,
|
|
20
|
+
readContentConfig,
|
|
21
|
+
UNREAD_CONTENT_CONFIG_PATHS,
|
|
22
|
+
} from './content-config-ast'
|
|
23
|
+
import type { CmsFileSystem, GlobOptions } from './fs/types'
|
|
6
24
|
import { resolvePathnameFromSpec, slugifyHref } from './shared'
|
|
7
25
|
|
|
8
26
|
/** Regex patterns for type inference */
|
|
@@ -400,16 +418,19 @@ function assembleCollectionDefinition(
|
|
|
400
418
|
/**
|
|
401
419
|
* Compute the root-relative base path of a collection's source directory.
|
|
402
420
|
* `basePath` is root-relative (e.g. `src/content/blog` for the default layout,
|
|
403
|
-
* or a glob loader base like
|
|
404
|
-
* `<contentDir>/<name>` location, the canonical
|
|
405
|
-
* otherwise the
|
|
421
|
+
* or a glob loader base as the config spells it, like `./src/content/jsem/`).
|
|
422
|
+
* When it matches the default `<contentDir>/<name>` location, the canonical
|
|
423
|
+
* default path is returned; otherwise the base path is returned normalized,
|
|
424
|
+
* without the leading `./` and trailing slash, so it is a prefix of each entry's
|
|
425
|
+
* `sourcePath`, except when the base is the project root, which is reported as `.`.
|
|
406
426
|
*/
|
|
407
427
|
function getCollectionSourceBasePath(basePath: string, collectionName: string, contentDir: string): string {
|
|
408
428
|
const defaultCollectionPath = path.join(contentDir, collectionName)
|
|
409
|
-
|
|
429
|
+
const normalizedBasePath = normalizeBasePath(basePath)
|
|
430
|
+
if (normalizedBasePath === path.normalize(defaultCollectionPath)) {
|
|
410
431
|
return path.join(contentDir, collectionName)
|
|
411
432
|
}
|
|
412
|
-
return
|
|
433
|
+
return normalizedBasePath
|
|
413
434
|
}
|
|
414
435
|
|
|
415
436
|
async function buildCollectionDefinition(
|
|
@@ -540,6 +561,7 @@ async function buildDataCollectionDefinition(
|
|
|
540
561
|
}
|
|
541
562
|
|
|
542
563
|
return assembleCollectionDefinition(collectionName, contentDir, fieldMap, entryInfos, sources.length, {
|
|
564
|
+
path: sourceBasePath,
|
|
543
565
|
type: 'data',
|
|
544
566
|
fileExtension: ext,
|
|
545
567
|
})
|
|
@@ -588,6 +610,29 @@ async function scanCollection(
|
|
|
588
610
|
return await buildCollectionDefinition(fs, collectionPath, sources, collectionName, contentDir)
|
|
589
611
|
}
|
|
590
612
|
|
|
613
|
+
/**
|
|
614
|
+
* The files a glob loader matches, relative to its base. The scan leaves symbolic links out;
|
|
615
|
+
* the scan report follows them, as Astro does, to see what that left out.
|
|
616
|
+
*/
|
|
617
|
+
async function globLoaderFiles(
|
|
618
|
+
fs: CmsFileSystem,
|
|
619
|
+
baseRel: string,
|
|
620
|
+
pattern: string,
|
|
621
|
+
options?: GlobOptions,
|
|
622
|
+
): Promise<string[]> {
|
|
623
|
+
// The port's glob walks the root; scope it to the loader base and re-relativize matches.
|
|
624
|
+
// The scanner ignores files/dirs whose path segments start with `_` or `.` (private/hidden),
|
|
625
|
+
// matching the directory-walk behavior of the original scanner — the glob pattern alone
|
|
626
|
+
// would otherwise match them.
|
|
627
|
+
const isPrivate = (relToBase: string) => relToBase.split('/').some(seg => seg.startsWith('_') || seg.startsWith('.'))
|
|
628
|
+
// Such a directory is not walked either, so one that cannot be read does not cost the
|
|
629
|
+
// collection: none of its files would be kept.
|
|
630
|
+
const matches = await fs.glob(path.join(baseRel, pattern), { ...options, skipDirectory: dir => isPrivate(path.relative(baseRel, dir)) })
|
|
631
|
+
return matches
|
|
632
|
+
.map(rel => path.relative(baseRel, rel))
|
|
633
|
+
.filter(relToBase => !isPrivate(relToBase))
|
|
634
|
+
}
|
|
635
|
+
|
|
591
636
|
/**
|
|
592
637
|
* Scan a collection declared in content config via a glob loader (base + pattern),
|
|
593
638
|
* which may share a base directory with another collection (nested layout).
|
|
@@ -599,14 +644,7 @@ async function scanGlobCollection(
|
|
|
599
644
|
pattern: string,
|
|
600
645
|
contentDir: string,
|
|
601
646
|
): Promise<CollectionDefinition | null> {
|
|
602
|
-
|
|
603
|
-
// The scanner ignores files/dirs whose path segments start with `_` or `.` (private/hidden),
|
|
604
|
-
// matching the directory-walk behavior of the original scanner — the glob pattern alone
|
|
605
|
-
// would otherwise match them.
|
|
606
|
-
const matches = await fs.glob(path.join(baseRel, pattern))
|
|
607
|
-
const rels = matches
|
|
608
|
-
.map(rel => path.relative(baseRel, rel))
|
|
609
|
-
.filter(relToBase => !relToBase.split('/').some(seg => seg.startsWith('_') || seg.startsWith('.')))
|
|
647
|
+
const rels = await globLoaderFiles(fs, baseRel, pattern)
|
|
610
648
|
|
|
611
649
|
// Markdown collections win when both are present; otherwise build a data
|
|
612
650
|
// collection (yaml/json) — the loader pattern dictates which, and a data
|
|
@@ -662,6 +700,13 @@ function applyParsedConfig(
|
|
|
662
700
|
def.fragment = parsedColl.fragment
|
|
663
701
|
if (parsedColl.previewOf) def.previewOf = parsedColl.previewOf
|
|
664
702
|
}
|
|
703
|
+
// Collection-browser metadata from `defineCmsCollection({ cms: { label, hidden, order, group } })`.
|
|
704
|
+
// `hidden` is only a flag here: the definition stays in the map, because reference
|
|
705
|
+
// pickers and entry routes resolve through it. Browsers are the ones that skip it.
|
|
706
|
+
if (parsedColl.label) def.label = parsedColl.label
|
|
707
|
+
if (parsedColl.hidden) def.hidden = parsedColl.hidden
|
|
708
|
+
if (parsedColl.order !== undefined) def.order = parsedColl.order
|
|
709
|
+
if (parsedColl.group) def.group = parsedColl.group
|
|
665
710
|
// Declarative entry-title source from `defineCmsCollection({ cms: { titleField } })`.
|
|
666
711
|
// The scan derived titles by fallback; re-derive them from the declared field.
|
|
667
712
|
if (parsedColl.titleField) {
|
|
@@ -1042,9 +1087,11 @@ async function scanDataCollection(
|
|
|
1042
1087
|
|
|
1043
1088
|
/**
|
|
1044
1089
|
* Normalize a glob loader `base` (`./src/content/x`, `src/content/x/`) into a
|
|
1045
|
-
* root-relative path comparable with a collection definition's `path`.
|
|
1090
|
+
* root-relative path comparable with a collection definition's `path`. The project
|
|
1091
|
+
* root comes out as `.`. Entry reads and writes resolve their directory with this too,
|
|
1092
|
+
* so they land where the scan reports a collection.
|
|
1046
1093
|
*/
|
|
1047
|
-
function normalizeBasePath(basePath: string): string {
|
|
1094
|
+
export function normalizeBasePath(basePath: string): string {
|
|
1048
1095
|
return path.normalize(basePath.replace(/^\.[/\\]+/, '')).replace(/[/\\]+$/, '')
|
|
1049
1096
|
}
|
|
1050
1097
|
|
|
@@ -1094,8 +1141,11 @@ export async function scanCollections(
|
|
|
1094
1141
|
fs: CmsFileSystem,
|
|
1095
1142
|
contentDir: string = 'src/content',
|
|
1096
1143
|
parseCache: ParseCache = new Map(),
|
|
1144
|
+
options: { report?: boolean } = {},
|
|
1097
1145
|
): Promise<Record<string, CollectionDefinition>> {
|
|
1098
1146
|
const collections: Record<string, CollectionDefinition> = {}
|
|
1147
|
+
/** Collections whose scan threw, by name, as they go into the scan report. */
|
|
1148
|
+
const failedScans = new Map<string, UnresolvedCollection>()
|
|
1099
1149
|
|
|
1100
1150
|
const entries = await fs.list(contentDir)
|
|
1101
1151
|
|
|
@@ -1113,13 +1163,15 @@ export async function scanCollections(
|
|
|
1113
1163
|
}
|
|
1114
1164
|
} catch (error) {
|
|
1115
1165
|
console.warn(`[cms] Skipping collection "${entry.name}" — scan failed:`, error)
|
|
1166
|
+
failedScans.set(entry.name, { name: entry.name, reason: 'scan-failed', base: path.join(contentDir, entry.name) })
|
|
1116
1167
|
}
|
|
1117
1168
|
})
|
|
1118
1169
|
|
|
1119
1170
|
await Promise.all(scanPromises)
|
|
1120
1171
|
|
|
1121
1172
|
// Post-scan: apply schema-driven field config, detect references, derived fields, and ordering
|
|
1122
|
-
const
|
|
1173
|
+
const config = await readContentConfig(fs, parseCache)
|
|
1174
|
+
const { parsed } = config
|
|
1123
1175
|
|
|
1124
1176
|
// Where the directory scan put each collection, so a config-declared loader base can be
|
|
1125
1177
|
// matched back to the definition that already covers that directory.
|
|
@@ -1157,6 +1209,12 @@ export async function scanCollections(
|
|
|
1157
1209
|
definition = await scanGlobCollection(fs, collectionName, parsedCollection.loaderBase, parsedCollection.loaderPattern, contentDir)
|
|
1158
1210
|
} catch (error) {
|
|
1159
1211
|
console.warn(`[cms] Skipping glob collection "${collectionName}" — scan failed:`, error)
|
|
1212
|
+
failedScans.set(collectionName, {
|
|
1213
|
+
name: collectionName,
|
|
1214
|
+
reason: 'scan-failed',
|
|
1215
|
+
base: normalizeBasePath(parsedCollection.loaderBase),
|
|
1216
|
+
pattern: parsedCollection.loaderPattern,
|
|
1217
|
+
})
|
|
1160
1218
|
continue
|
|
1161
1219
|
}
|
|
1162
1220
|
if (!definition) continue
|
|
@@ -1177,9 +1235,345 @@ export async function scanCollections(
|
|
|
1177
1235
|
applyCollectionOrderBy(collections, parsed)
|
|
1178
1236
|
warnOnPathnameCollisions(collections)
|
|
1179
1237
|
|
|
1238
|
+
// The report has to describe the disk as this scan found it, so it is built here and not
|
|
1239
|
+
// when someone asks for it later; a caller that will not ask does not pay for it. And it
|
|
1240
|
+
// is an extra: failing to build it must not fail a scan that succeeded.
|
|
1241
|
+
if (options.report) {
|
|
1242
|
+
try {
|
|
1243
|
+
const contentDirectories = entries
|
|
1244
|
+
.filter(entry => entry.isDirectory && !entry.name.startsWith('_') && !entry.name.startsWith('.'))
|
|
1245
|
+
.map(entry => entry.name)
|
|
1246
|
+
scanReports.set(collections, await buildScanReport(fs, contentDir, collections, config, failedScans, contentDirectories))
|
|
1247
|
+
} catch (error) {
|
|
1248
|
+
console.warn('[cms] Collection scan report unavailable:', error)
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1180
1252
|
return collections
|
|
1181
1253
|
}
|
|
1182
1254
|
|
|
1255
|
+
/** The report of each scan, by the record that scan returned — see {@link collectionScanReport}. */
|
|
1256
|
+
const scanReports = new WeakMap<Record<string, CollectionDefinition>, CollectionScanReport>()
|
|
1257
|
+
|
|
1258
|
+
/**
|
|
1259
|
+
* What the scan that returned `collections` could not vouch for.
|
|
1260
|
+
*
|
|
1261
|
+
* Looked up by the record itself instead of being returned next to it: `scanCollections`
|
|
1262
|
+
* keeps its return type for the callers that only want the definitions, and a report cannot
|
|
1263
|
+
* be paired with another scan's collections. `undefined` for a scan that was not asked for a
|
|
1264
|
+
* report (`options.report`), for one whose report could not be built, and for any other
|
|
1265
|
+
* object (a copy, a hand-built record) — read that as "not reported", never as "nothing
|
|
1266
|
+
* unresolved".
|
|
1267
|
+
*/
|
|
1268
|
+
export function collectionScanReport(collections: Record<string, CollectionDefinition>): CollectionScanReport | undefined {
|
|
1269
|
+
return scanReports.get(collections)
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
/**
|
|
1273
|
+
* Patterns the `CmsFileSystem` port does not run the way Astro's glob does. Syntax it reads
|
|
1274
|
+
* literally or wrongly: a character class, an extglob, an escape, a `|` (alternation to
|
|
1275
|
+
* Astro), a `+` behind a brace (a quantifier), a brace without a comma (the name `{a}` to
|
|
1276
|
+
* Astro), and inside a brace another brace, a range (`{x,1..3}`) or a wildcard. And
|
|
1277
|
+
* shapes Astro's glob reads wider than the port: a `..` segment, also one a brace completes
|
|
1278
|
+
* (`..{/drafts,x}`), a trailing slash (also one an empty brace alternative leaves behind), a
|
|
1279
|
+
* negation behind `./`, and a final `/**`, which to Astro also matches a file named by what
|
|
1280
|
+
* stands before it. What such a pattern matches here says nothing about what the loader loads.
|
|
1281
|
+
* A lone leading `!` is not among them: it matches nothing in Astro, and nothing here.
|
|
1282
|
+
*/
|
|
1283
|
+
const UNSUPPORTED_GLOB_SYNTAX = /[[\]()\\|]|\}\+|\{[^}]*(?:\{|\.\.|[*?]|,,)|\{,|,\}|\{[^,}]*\}|(?:^|[/{},])\.\.(?:[/{},]|$)|\/$|^(?:\.\/)+!|.\/\*\*$/
|
|
1284
|
+
|
|
1285
|
+
/**
|
|
1286
|
+
* A `..` segment in a pattern leads out of the base, so the files under the base do not bound
|
|
1287
|
+
* it. A brace next to the dots may be what makes them a segment.
|
|
1288
|
+
*/
|
|
1289
|
+
const LEAVES_THE_BASE = /(?:^|[/{},])\.\.(?:[/{},]|$)/
|
|
1290
|
+
|
|
1291
|
+
/**
|
|
1292
|
+
* How many directories behind symbolic links the report reads for one collection before it
|
|
1293
|
+
* gives up on it. Links can lead into a tree of any size, and into each other.
|
|
1294
|
+
*/
|
|
1295
|
+
const REPORT_LINKED_DIRECTORY_LIMIT = 20_000
|
|
1296
|
+
|
|
1297
|
+
/**
|
|
1298
|
+
* Files that may be entries. Astro's glob loads md, mdx (with the integration), json, yaml,
|
|
1299
|
+
* yml and toml; the other markdown spellings and mdoc are loaded only where an integration
|
|
1300
|
+
* adds them, and are counted anyway: a file too many is a report, a file too few a miss.
|
|
1301
|
+
* A loader's pattern may match more (a cover image next to an entry), and that is not content.
|
|
1302
|
+
*/
|
|
1303
|
+
const ENTRY_FILE = /\.(?:md|mdx|mdoc|markdown|mdown|mkdn|mkd|mdwn|json|ya?ml|toml)$/i
|
|
1304
|
+
|
|
1305
|
+
/** The markdown half of {@link ENTRY_FILE}: what Astro takes from a content directory nothing declares. */
|
|
1306
|
+
const MARKDOWN_FILE = /\.(?:md|mdx|mdoc|markdown|mdown|mkdn|mkd|mdwn)$/i
|
|
1307
|
+
|
|
1308
|
+
/**
|
|
1309
|
+
* Whether Astro reads `base` as the relative directory the scan takes it for. Astro resolves
|
|
1310
|
+
* it as a URL against the project root: an absolute path or a scheme is somewhere else, an
|
|
1311
|
+
* escape, a query or a fragment is not part of the directory name, a backslash becomes a
|
|
1312
|
+
* slash, whitespace at the edges and tabs or line breaks anywhere are dropped, and a `..`
|
|
1313
|
+
* that leaves the root is resolved against it. And the port would read a glob character in
|
|
1314
|
+
* the base as part of the pattern.
|
|
1315
|
+
*/
|
|
1316
|
+
function readsAsRelativeDirectory(base: string): boolean {
|
|
1317
|
+
if (/[*?{}]/.test(base)) return false
|
|
1318
|
+
try {
|
|
1319
|
+
const asUrl = decodeURIComponent(new URL(base, 'file:///project-root/').pathname).replace(/\/{2,}/g, '/').replace(/\/+$/, '')
|
|
1320
|
+
const asPath = normalizeBasePath(base).split(path.sep).join('/')
|
|
1321
|
+
return asUrl === (asPath === '.' ? '/project-root' : `/project-root/${asPath}`)
|
|
1322
|
+
} catch {
|
|
1323
|
+
return false
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
/** Where a declaration says its files are, in the shape the report carries. */
|
|
1328
|
+
function declaredLocation(declaration: DeclaredCollection, contentDir: string): Pick<UnresolvedCollection, 'base' | 'pattern' | 'file'> {
|
|
1329
|
+
const location: Pick<UnresolvedCollection, 'base' | 'pattern' | 'file'> = {}
|
|
1330
|
+
if (declaration.noLoader) location.base = path.join(contentDir, declaration.name)
|
|
1331
|
+
if (declaration.loaderBase !== undefined) location.base = normalizeBasePath(declaration.loaderBase)
|
|
1332
|
+
if (declaration.loaderPattern !== undefined) location.pattern = declaration.loaderPattern
|
|
1333
|
+
if (declaration.loaderFile !== undefined) location.file = normalizeBasePath(declaration.loaderFile)
|
|
1334
|
+
return location
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
/**
|
|
1338
|
+
* The entry files of a content directory read without a loader, root-relative: all of them at
|
|
1339
|
+
* any depth, `_` and `.` paths aside. This is what Astro loads for a declaration without a
|
|
1340
|
+
* loader and for a content directory nothing declares, under the
|
|
1341
|
+
* `legacy.collectionsBackwardsCompat` flag that nua sets.
|
|
1342
|
+
*/
|
|
1343
|
+
async function contentDirectoryFiles(fs: CmsFileSystem, directory: string, entryFile: RegExp): Promise<string[]> {
|
|
1344
|
+
const files = await globLoaderFiles(fs, directory, '**/*', { followSymlinks: true, maxLinkedDirectories: REPORT_LINKED_DIRECTORY_LIMIT })
|
|
1345
|
+
return files.filter(file => entryFile.test(file)).map(file => path.join(directory, file))
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
/**
|
|
1349
|
+
* The entry files a declared collection loads, root-relative, as far as that can be told
|
|
1350
|
+
* without running its loader. `exact` when the scan can evaluate the loader; otherwise the
|
|
1351
|
+
* files are every entry file the loader could match. `null` when not even that is known.
|
|
1352
|
+
*/
|
|
1353
|
+
async function declaredEntryFiles(
|
|
1354
|
+
fs: CmsFileSystem,
|
|
1355
|
+
contentDir: string,
|
|
1356
|
+
declaration: DeclaredCollection,
|
|
1357
|
+
): Promise<{ files: string[]; exact: boolean } | null> {
|
|
1358
|
+
if (declaration.noLoader) {
|
|
1359
|
+
// Astro resolves the name as a URL against the content directory, like a base.
|
|
1360
|
+
if (!readsAsRelativeDirectory(declaration.name)) return null
|
|
1361
|
+
return { exact: true, files: await contentDirectoryFiles(fs, path.join(contentDir, declaration.name), ENTRY_FILE) }
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
// Without a literal base there is nothing to look under, and one Astro reads as something
|
|
1365
|
+
// other than a relative path is no better.
|
|
1366
|
+
const declaredBase = declaration.loaderBase
|
|
1367
|
+
if (declaredBase === undefined || !readsAsRelativeDirectory(declaredBase)) return null
|
|
1368
|
+
const base = normalizeBasePath(declaredBase)
|
|
1369
|
+
const pattern = declaration.loaderPattern
|
|
1370
|
+
// A list of patterns is not run here either, but it can be read for where it leads.
|
|
1371
|
+
const patterns = pattern !== undefined ? [pattern] : declaration.loaderPatterns
|
|
1372
|
+
// A pattern that cannot be read may lead anywhere.
|
|
1373
|
+
if (patterns === undefined) return null
|
|
1374
|
+
// As spelled and as the port joins it to the base, which drops `.` segments and doubled slashes.
|
|
1375
|
+
const exact = pattern !== undefined && !UNSUPPORTED_GLOB_SYNTAX.test(pattern) && !UNSUPPORTED_GLOB_SYNTAX.test(path.posix.normalize(pattern))
|
|
1376
|
+
// Patterns the scan cannot evaluate select among the files under the base: all of them
|
|
1377
|
+
// are candidates. Unless one leads out of the base, and the whole project is not walked
|
|
1378
|
+
// on that off chance either.
|
|
1379
|
+
if (!exact && (base === '.' || patterns.some(one => LEAVES_THE_BASE.test(one)))) return null
|
|
1380
|
+
const matches = await fs.glob(path.join(base, exact ? pattern : '**/*'), {
|
|
1381
|
+
followSymlinks: true,
|
|
1382
|
+
maxLinkedDirectories: REPORT_LINKED_DIRECTORY_LIMIT,
|
|
1383
|
+
})
|
|
1384
|
+
// Astro's glob leaves dot paths out unless the pattern asks for them. It does load `_`
|
|
1385
|
+
// files, which the scan skips, so those stay in.
|
|
1386
|
+
const files = matches.filter(file => ENTRY_FILE.test(file) && patterns.some(one => mayTakeDotPath(one, path.relative(base, file))))
|
|
1387
|
+
return { exact, files }
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
/**
|
|
1391
|
+
* Whether a loader pattern can match `file` (relative to the base) as far as leading dots go.
|
|
1392
|
+
* Astro's glob keeps `*` and `**` from matching one, so a dotfile or a file in a dot directory
|
|
1393
|
+
* is loaded only when the pattern asks for it. It does by naming a dot at the start of a
|
|
1394
|
+
* segment or of an alternative. And by a wildcard at the start of an alternative, right
|
|
1395
|
+
* behind a brace or behind a `|`: Astro's glob guards `*` and `?` against a leading dot only
|
|
1396
|
+
* at the start of the pattern and behind a slash, so `{guide/,notes/}*.md` takes
|
|
1397
|
+
* `guide/.wip.md`. In both cases nothing is ruled out here. And by a character class or an
|
|
1398
|
+
* extglob (in patterns the scan does not run), which take a leading dot like any other
|
|
1399
|
+
* character: in the last segment a dotfile, in an earlier one a dot directory. Where a brace
|
|
1400
|
+
* or an extglob spans a slash the segments cannot be told apart, and either takes both.
|
|
1401
|
+
*/
|
|
1402
|
+
function mayTakeDotPath(pattern: string, file: string): boolean {
|
|
1403
|
+
const segments = file.split(/[/\\]/)
|
|
1404
|
+
const dotFile = segments.pop()!.startsWith('.')
|
|
1405
|
+
const dotDirectory = segments.some(segment => segment.startsWith('.'))
|
|
1406
|
+
if (!dotFile && !dotDirectory) return true
|
|
1407
|
+
if (/(?:^|[/{},|!\\])\.[^./]|[{,}|][*?]/.test(pattern)) return true
|
|
1408
|
+
const cut = pattern.lastIndexOf('/')
|
|
1409
|
+
const blurred = /[{(][^})]*\//.test(pattern)
|
|
1410
|
+
const takesDot = (part: string): boolean => /[[(]/.test(blurred ? pattern : part)
|
|
1411
|
+
return (!dotFile || takesDot(pattern.slice(cut + 1))) && (!dotDirectory || takesDot(pattern.slice(0, Math.max(cut, 0))))
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
/** Where Astro looks for its own config. It takes the first one there is. */
|
|
1415
|
+
const ASTRO_CONFIG_PATHS = ['astro.config.mjs', 'astro.config.js', 'astro.config.ts', 'astro.config.mts']
|
|
1416
|
+
|
|
1417
|
+
/**
|
|
1418
|
+
* The project's source directory when the Astro config moves it from `src`: the directory,
|
|
1419
|
+
* `undefined` for the default, or `null` when where Astro loads content from cannot be told.
|
|
1420
|
+
*
|
|
1421
|
+
* Read off the code of the one config file Astro uses. Two of its options decide the place:
|
|
1422
|
+
* `srcDir`, where the content config and the content directory are, and `root`, which every
|
|
1423
|
+
* loader's `base` is resolved against. Which mention of either name is Astro's option cannot
|
|
1424
|
+
* be told without evaluating the file (it may name another plugin's option, a variable, a
|
|
1425
|
+
* member), so every one of them has to be a property given a plain string: all `srcDir`s the
|
|
1426
|
+
* same directory inside the project, every `root` the project root itself, which the scan
|
|
1427
|
+
* takes it to be. Either name given an object literal is not Astro's option, which is a string.
|
|
1428
|
+
*/
|
|
1429
|
+
async function customSrcDir(fs: CmsFileSystem): Promise<string | null | undefined> {
|
|
1430
|
+
for (const configPath of ASTRO_CONFIG_PATHS) {
|
|
1431
|
+
const source = await fs.readFile(configPath).catch(() => null)
|
|
1432
|
+
if (source === null) continue
|
|
1433
|
+
const roots = configOptionMentions(source, 'root')
|
|
1434
|
+
const srcDirs = configOptionMentions(source, 'srcDir')
|
|
1435
|
+
if (roots === null || srcDirs === null) return null
|
|
1436
|
+
if (roots.some(root => root === null || normalizeBasePath(root) !== '.')) return null
|
|
1437
|
+
if (srcDirs.length === 0) return undefined
|
|
1438
|
+
const [srcDir, ...others] = srcDirs.map(literal => (literal === null ? null : normalizeBasePath(literal)))
|
|
1439
|
+
if (srcDir === null || srcDir === undefined || others.some(other => other !== srcDir)) return null
|
|
1440
|
+
if (srcDir === 'src') return undefined
|
|
1441
|
+
return path.isAbsolute(srcDir) || srcDir.startsWith('..') ? null : srcDir
|
|
1442
|
+
}
|
|
1443
|
+
return undefined
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
/**
|
|
1447
|
+
* How much of the content config was read, for the report. Astro takes the config from more
|
|
1448
|
+
* file names than the reader looks at, and from another directory when `srcDir` says so, so
|
|
1449
|
+
* which files exist is part of the answer.
|
|
1450
|
+
*/
|
|
1451
|
+
async function contentConfigStatus(fs: CmsFileSystem, config: ContentConfigRead): Promise<CollectionScanReport['contentConfig']> {
|
|
1452
|
+
const configPaths = [...CONTENT_CONFIG_PATHS, ...UNREAD_CONTENT_CONFIG_PATHS]
|
|
1453
|
+
const srcDir = await customSrcDir(fs)
|
|
1454
|
+
// Where Astro loads from cannot be told, so neither can whether a config is there, nor
|
|
1455
|
+
// whether what the scan found under the project root is what Astro loads.
|
|
1456
|
+
if (srcDir === null) return 'unreadable'
|
|
1457
|
+
if (srcDir !== undefined) {
|
|
1458
|
+
// The reader only looks under `src`, so a config found here was not read. Nothing here
|
|
1459
|
+
// settles nothing: the mention may not have been Astro's option, and then `src` is
|
|
1460
|
+
// where the config is, so `src` is judged as usual.
|
|
1461
|
+
for (const configPath of configPaths) {
|
|
1462
|
+
if (await fs.exists(path.join(srcDir, path.relative('src', configPath)))) return 'unreadable'
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
let configFiles = 0
|
|
1467
|
+
for (const configPath of configPaths) {
|
|
1468
|
+
if (await fs.exists(configPath)) configFiles++
|
|
1469
|
+
}
|
|
1470
|
+
if (configFiles === 0) return 'missing'
|
|
1471
|
+
// With two config files, the one the reader took need not be the one Astro uses.
|
|
1472
|
+
if (config.partial || configFiles > 1) return 'partial'
|
|
1473
|
+
if (config.declared.length > 0) return 'ok'
|
|
1474
|
+
// Nothing declared: a site without collections, or a config that was not read at all
|
|
1475
|
+
// (it does not parse, or it is one of the names the reader does not look at).
|
|
1476
|
+
return await classifyEmptyContentConfig(fs) === 'no-collections' ? 'ok' : 'unreadable'
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
/**
|
|
1480
|
+
* Account for every collection the scan knows of and cannot vouch for.
|
|
1481
|
+
*
|
|
1482
|
+
* "Knows of" is every name the content config declares plus every content directory the
|
|
1483
|
+
* walk tried. A declared collection is vouched for when every entry file its loader loads is
|
|
1484
|
+
* an entry of the scan — which makes one that loads no files an empty collection, not an
|
|
1485
|
+
* unresolved one, and lets a site the scan reads whole report nothing. Being in `collections`
|
|
1486
|
+
* is not enough: a content directory of the declared name is read whatever the loader says.
|
|
1487
|
+
*/
|
|
1488
|
+
async function buildScanReport(
|
|
1489
|
+
fs: CmsFileSystem,
|
|
1490
|
+
contentDir: string,
|
|
1491
|
+
collections: Record<string, CollectionDefinition>,
|
|
1492
|
+
config: ContentConfigRead,
|
|
1493
|
+
failedScans: Map<string, UnresolvedCollection>,
|
|
1494
|
+
contentDirectories: string[],
|
|
1495
|
+
): Promise<CollectionScanReport> {
|
|
1496
|
+
const contentConfig = await contentConfigStatus(fs, config)
|
|
1497
|
+
const unresolved = new Map<string, UnresolvedCollection>()
|
|
1498
|
+
const declaredByName = new Map(config.declared.map(declaration => [declaration.name, declaration]))
|
|
1499
|
+
|
|
1500
|
+
/** Every file that is an entry of some collection. */
|
|
1501
|
+
const listed = new Set<string>()
|
|
1502
|
+
for (const definition of Object.values(collections)) {
|
|
1503
|
+
for (const entry of definition.entries ?? []) listed.add(path.normalize(entry.sourcePath))
|
|
1504
|
+
// `entryCount` is the source files found, `entries` the ones that could be read and parsed.
|
|
1505
|
+
if ((definition.entries?.length ?? 0) >= definition.entryCount) continue
|
|
1506
|
+
const declaration = declaredByName.get(definition.name)
|
|
1507
|
+
unresolved.set(definition.name, {
|
|
1508
|
+
name: definition.name,
|
|
1509
|
+
reason: 'unlisted-entries',
|
|
1510
|
+
base: normalizeBasePath(definition.path),
|
|
1511
|
+
...(declaration ? declaredLocation(declaration, contentDir) : {}),
|
|
1512
|
+
})
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
for (const declaration of config.declared) {
|
|
1516
|
+
const { name } = declaration
|
|
1517
|
+
if (unresolved.has(name)) continue
|
|
1518
|
+
const location = declaredLocation(declaration, contentDir)
|
|
1519
|
+
|
|
1520
|
+
const failed = collections[name] ? undefined : failedScans.get(name)
|
|
1521
|
+
if (failed) {
|
|
1522
|
+
unresolved.set(name, failed)
|
|
1523
|
+
continue
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
let loads: Awaited<ReturnType<typeof declaredEntryFiles>>
|
|
1527
|
+
try {
|
|
1528
|
+
loads = await declaredEntryFiles(fs, contentDir, declaration)
|
|
1529
|
+
} catch {
|
|
1530
|
+
// Looking where the loader loads from threw: a base outside the root, a directory that cannot be read.
|
|
1531
|
+
unresolved.set(name, { name, reason: 'scan-failed', ...location })
|
|
1532
|
+
continue
|
|
1533
|
+
}
|
|
1534
|
+
if (loads === null) {
|
|
1535
|
+
unresolved.set(name, { name, reason: 'unsupported-loader', ...location })
|
|
1536
|
+
} else if (!loads.files.every(file => listed.has(path.normalize(file)))) {
|
|
1537
|
+
// A file the loader is known to load is missing from the entries — or, where the loader
|
|
1538
|
+
// cannot be evaluated, one it may load is.
|
|
1539
|
+
unresolved.set(name, { name, reason: loads.exact ? 'unlisted-entries' : 'unsupported-loader', ...location })
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
// A content directory nothing declares. As nua configures Astro
|
|
1544
|
+
// (`legacy.collectionsBackwardsCompat`), each one is a collection of its markdown files at
|
|
1545
|
+
// any depth, unless some declared collection has a loader — then Astro leaves them alone.
|
|
1546
|
+
// Whether the flag is set cannot be read reliably, so it is taken to be: that way a
|
|
1547
|
+
// directory Astro does not load can be reported, and one it does load cannot be missed.
|
|
1548
|
+
// A declaration whose loader is unknown may have none, so only a loader that was seen counts.
|
|
1549
|
+
// A config that does not export `collections` gives Astro nothing to add to.
|
|
1550
|
+
const loadsUndeclared = config.exportsCollections && (contentConfig === 'ok' || contentConfig === 'partial')
|
|
1551
|
+
&& !config.declared.some(declaration => declaration.hasLoader)
|
|
1552
|
+
if (loadsUndeclared) {
|
|
1553
|
+
for (const name of contentDirectories) {
|
|
1554
|
+
if (declaredByName.has(name) || unresolved.has(name) || failedScans.has(name)) continue
|
|
1555
|
+
const directory = path.join(contentDir, name)
|
|
1556
|
+
const files = await contentDirectoryFiles(fs, directory, MARKDOWN_FILE).catch(() => null)
|
|
1557
|
+
if (files === null) unresolved.set(name, { name, reason: 'scan-failed', base: directory })
|
|
1558
|
+
else if (!files.every(file => listed.has(path.normalize(file)))) unresolved.set(name, { name, reason: 'unlisted-entries', base: directory })
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
// What is left are content directories the config does not declare under that name. One a
|
|
1563
|
+
// declared collection loads from under another key (`heroSlides` over `hero-slides`) has
|
|
1564
|
+
// failed there already, and one whose declared loader did read it has not failed at all.
|
|
1565
|
+
for (const failed of failedScans.values()) {
|
|
1566
|
+
if (collections[failed.name] || unresolved.has(failed.name)) continue
|
|
1567
|
+
if ([...unresolved.values()].some(other => other.reason === 'scan-failed' && other.base === failed.base)) continue
|
|
1568
|
+
unresolved.set(failed.name, failed)
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
return {
|
|
1572
|
+
contentConfig,
|
|
1573
|
+
unresolved: [...unresolved.values()].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)),
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1183
1577
|
/**
|
|
1184
1578
|
* Warn when a collection's declarative `cms.pathname` rule maps two distinct
|
|
1185
1579
|
* entries to the same URL — e.g. an all-literal spec, or a spec whose `field`
|