Home / Function/ createContentTypesGenerator() — astro Function Reference

createContentTypesGenerator() — astro Function Reference

Architecture documentation for the createContentTypesGenerator() function in types-generator.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  50cf10da_1399_5279_238c_425437e9388c["createContentTypesGenerator()"]
  eca81928_a18b_c02e_8e5a_0440befa1a98["types-generator.ts"]
  50cf10da_1399_5279_238c_425437e9388c -->|defined in| eca81928_a18b_c02e_8e5a_0440befa1a98
  453daca9_4232_0b6c_8607_0e08e829d051["writeContentFiles()"]
  50cf10da_1399_5279_238c_425437e9388c -->|calls| 453daca9_4232_0b6c_8607_0e08e829d051
  ea13bd40_9fb0_16c2_7750_8633fcb65120["invalidateVirtualMod()"]
  50cf10da_1399_5279_238c_425437e9388c -->|calls| ea13bd40_9fb0_16c2_7750_8633fcb65120
  style 50cf10da_1399_5279_238c_425437e9388c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/types-generator.ts lines 79–336

export async function createContentTypesGenerator({
	contentConfigObserver,
	fs,
	logger,
	settings,
	viteServer,
}: CreateContentGeneratorParams) {
	const collectionEntryMap: CollectionEntryMap = {};
	const contentPaths = getContentPaths(
		settings.config,
		fs,
		settings.config.legacy?.collectionsBackwardsCompat,
	);
	const contentEntryConfigByExt = getEntryConfigByExtMap(settings.contentEntryTypes);
	const contentEntryExts = [...contentEntryConfigByExt.keys()];
	const dataEntryExts = getDataEntryExts(settings);

	let events: ContentEvent[] = [];
	let debounceTimeout: NodeJS.Timeout | undefined;

	const typeTemplateContent = await fs.promises.readFile(contentPaths.typesTemplate, 'utf-8');

	async function init(): Promise<void> {
		events.push({ name: 'add', entry: contentPaths.config.url });
		await runEvents();
	}

	async function handleEvent(event: ContentEvent): Promise<{ shouldGenerateTypes: boolean }> {
		if (event.name === 'addDir' || event.name === 'unlinkDir') {
			const collection = normalizePath(
				path.relative(fileURLToPath(contentPaths.contentDir), fileURLToPath(event.entry)),
			);
			const collectionKey = JSON.stringify(collection);
			// If directory is multiple levels deep, it is not a collection. Ignore event.
			const isCollectionEvent = collection.split('/').length === 1;
			if (!isCollectionEvent) return { shouldGenerateTypes: false };

			switch (event.name) {
				case 'addDir':
					collectionEntryMap[collectionKey] = {
						type: 'unknown',
						entries: {},
					};
					logger.debug('content', `${colors.cyan(collection)} collection added`);
					break;
				case 'unlinkDir':
					delete collectionEntryMap[collectionKey];
					break;
			}
			return { shouldGenerateTypes: true };
		}
		const fileType = getEntryType(
			fileURLToPath(event.entry),
			contentPaths,
			contentEntryExts,
			dataEntryExts,
		);
		if (fileType === 'ignored') {
			return { shouldGenerateTypes: false };
		}
		if (fileType === 'config') {
			await reloadContentConfigObserver({
				fs,
				settings,
				environment: viteServer.environments[
					ASTRO_VITE_ENVIRONMENT_NAMES.astro
				] as RunnableDevEnvironment,
			});
			return { shouldGenerateTypes: true };
		}

		const { entry } = event;
		const { contentDir } = contentPaths;

		const collection = getEntryCollectionName({ entry, contentDir });
		if (collection === undefined) {
			logger.warn(
				'content',
				`${colors.bold(
					normalizePath(
						path.relative(fileURLToPath(contentPaths.contentDir), fileURLToPath(event.entry)),

Subdomains

Frequently Asked Questions

What does createContentTypesGenerator() do?
createContentTypesGenerator() is a function in the astro codebase, defined in packages/astro/src/content/types-generator.ts.
Where is createContentTypesGenerator() defined?
createContentTypesGenerator() is defined in packages/astro/src/content/types-generator.ts at line 79.
What does createContentTypesGenerator() call?
createContentTypesGenerator() calls 2 function(s): invalidateVirtualMod, writeContentFiles.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free