Home / Function/ writeContentFiles() — astro Function Reference

writeContentFiles() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  453daca9_4232_0b6c_8607_0e08e829d051["writeContentFiles()"]
  eca81928_a18b_c02e_8e5a_0440befa1a98["types-generator.ts"]
  453daca9_4232_0b6c_8607_0e08e829d051 -->|defined in| eca81928_a18b_c02e_8e5a_0440befa1a98
  50cf10da_1399_5279_238c_425437e9388c["createContentTypesGenerator()"]
  50cf10da_1399_5279_238c_425437e9388c -->|calls| 453daca9_4232_0b6c_8607_0e08e829d051
  9450af48_94b5_71a2_6795_b9b190df4757["typeForCollection()"]
  453daca9_4232_0b6c_8607_0e08e829d051 -->|calls| 9450af48_94b5_71a2_6795_b9b190df4757
  b10d9005_fa71_f3ce_7f9d_49b77a03fefd["getContentLayerSchema()"]
  453daca9_4232_0b6c_8607_0e08e829d051 -->|calls| b10d9005_fa71_f3ce_7f9d_49b77a03fefd
  e8e4a435_135c_9f53_dc21_719f629aad4a["generateJSONSchema()"]
  453daca9_4232_0b6c_8607_0e08e829d051 -->|calls| e8e4a435_135c_9f53_dc21_719f629aad4a
  a0e8b4bf_c039_6fa9_623e_e4f1a5bb6bb8["normalizeConfigPath()"]
  453daca9_4232_0b6c_8607_0e08e829d051 -->|calls| a0e8b4bf_c039_6fa9_623e_e4f1a5bb6bb8
  style 453daca9_4232_0b6c_8607_0e08e829d051 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/types-generator.ts lines 429–604

async function writeContentFiles({
	fs,
	contentPaths,
	collectionEntryMap,
	typeTemplateContent,
	contentEntryTypes,
	contentConfig,
	viteServer,
	logger,
	settings,
}: {
	fs: typeof fsMod;
	contentPaths: ContentPaths;
	collectionEntryMap: CollectionEntryMap;
	typeTemplateContent: string;
	contentEntryTypes: Pick<ContentEntryType, 'contentModuleTypes'>[];
	contentConfig?: ContentConfig;
	viteServer: ViteDevServer;
	logger: Logger;
	settings: AstroSettings;
}) {
	let dataTypesStr = '';

	const collectionSchemasDir = new URL(COLLECTIONS_DIR, settings.dotAstroDir);
	fs.mkdirSync(collectionSchemasDir, { recursive: true });

	for (const [collection, config] of Object.entries(contentConfig?.collections ?? {})) {
		collectionEntryMap[JSON.stringify(collection)] ??= {
			type: config.type ?? 'unknown',
			entries: {},
		};
	}

	let contentCollectionsMap: CollectionEntryMap = {};
	for (const collectionKey of Object.keys(collectionEntryMap).sort()) {
		const collectionConfig = contentConfig?.collections[JSON.parse(collectionKey)];
		const collection = collectionEntryMap[collectionKey];
		if (
			collectionConfig?.type &&
			collection.type !== 'unknown' &&
			collectionConfig.type !== CONTENT_LAYER_TYPE &&
			collection.type !== collectionConfig.type
		) {
			viteServer.environments.client.hot.send({
				type: 'error',
				err: new AstroError({
					...AstroErrorData.ContentCollectionTypeMismatchError,
					message: AstroErrorData.ContentCollectionTypeMismatchError.message(
						collectionKey,
						collection.type,
						collectionConfig.type,
					),
					hint:
						collection.type === 'data'
							? "Try adding `type: 'data'` to your collection config."
							: undefined,
					location: {
						file: '' /** required for error overlay `hot` messages */,
					},
				}) as any,
			});
			return;
		}

		const { type: dataType, injectedType } = await typeForCollection(
			collectionConfig,
			collectionKey,
		);

		if (injectedType) {
			if (settings.injectedTypes.some((t) => t.filename === CONTENT_TYPES_FILE)) {
				// If it's the first time, we inject types the usual way. sync() will handle creating files and references. If it's not the first time, we just override the dts content
				const url = new URL(injectedType.filename, settings.dotAstroDir);
				await fs.promises.mkdir(path.dirname(fileURLToPath(url)), { recursive: true });
				await fs.promises.writeFile(url, injectedType.content, 'utf-8');
			} else {
				settings.injectedTypes.push(injectedType);
			}
		}

		dataTypesStr += `${collectionKey}: Record<string, {\n  id: string;\n  body?: string;\n  collection: ${collectionKey};\n  data: ${dataType};\n  rendered?: RenderedContent;\n  filePath?: string;\n}>;\n`;

Subdomains

Frequently Asked Questions

What does writeContentFiles() do?
writeContentFiles() is a function in the astro codebase, defined in packages/astro/src/content/types-generator.ts.
Where is writeContentFiles() defined?
writeContentFiles() is defined in packages/astro/src/content/types-generator.ts at line 429.
What does writeContentFiles() call?
writeContentFiles() calls 4 function(s): generateJSONSchema, getContentLayerSchema, normalizeConfigPath, typeForCollection.
What calls writeContentFiles()?
writeContentFiles() is called by 1 function(s): createContentTypesGenerator.

Analyze Your Own Codebase

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

Try Supermodel Free