Home / File/ file.ts — astro Source File

file.ts — astro Source File

Architecture documentation for file.ts, a typescript file in the astro codebase. 8 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  7fd49d85_29da_fb8a_f2fc_3479c947eee8["file.ts"]
  8df634da_0f30_1e1f_1314_2439b0c9baab["../core/errors/errors-data.js"]
  7fd49d85_29da_fb8a_f2fc_3479c947eee8 --> 8df634da_0f30_1e1f_1314_2439b0c9baab
  ef8a1e3f_e350_75a6_b92d_62a8566d8db9["../core/errors/index.js"]
  7fd49d85_29da_fb8a_f2fc_3479c947eee8 --> ef8a1e3f_e350_75a6_b92d_62a8566d8db9
  520c567a_b741_f105_70ac_c637eacc7f83["../content/utils.js"]
  7fd49d85_29da_fb8a_f2fc_3479c947eee8 --> 520c567a_b741_f105_70ac_c637eacc7f83
  a7935420_ff38_d53d_2988_e0293e03591e["./loaders/types.js"]
  7fd49d85_29da_fb8a_f2fc_3479c947eee8 --> a7935420_ff38_d53d_2988_e0293e03591e
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  7fd49d85_29da_fb8a_f2fc_3479c947eee8 --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  d9a92db9_c95e_9165_13ac_24b3d859d946["node:url"]
  7fd49d85_29da_fb8a_f2fc_3479c947eee8 --> d9a92db9_c95e_9165_13ac_24b3d859d946
  38ee36f6_1b8f_5a62_1295_989b44329ca0["js-yaml"]
  7fd49d85_29da_fb8a_f2fc_3479c947eee8 --> 38ee36f6_1b8f_5a62_1295_989b44329ca0
  700c64be_9e12_6323_a727_8d7af053511b["smol-toml"]
  7fd49d85_29da_fb8a_f2fc_3479c947eee8 --> 700c64be_9e12_6323_a727_8d7af053511b
  style 7fd49d85_29da_fb8a_f2fc_3479c947eee8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { existsSync, promises as fs } from 'node:fs';
import { fileURLToPath } from 'node:url';
import yaml from 'js-yaml';
import toml from 'smol-toml';
import { FileGlobNotSupported, FileParserNotFound } from '../../core/errors/errors-data.js';
import { AstroError } from '../../core/errors/index.js';
import { posixRelative } from '../utils.js';
import type { Loader, LoaderContext } from './types.js';

type ParserOutput = Record<string, Record<string, unknown>> | Array<Record<string, unknown>>;

interface FileOptions {
	/**
	 * the parsing function to use for this data
	 * @default JSON.parse or yaml.load, depending on the extension of the file
	 * */
	parser?: (text: string) => Promise<ParserOutput> | ParserOutput;
}

/**
 * Loads entries from a JSON file. The file must contain an array of objects that contain unique `id` fields, or an object with string keys.
 * @param fileName The path to the JSON file to load, relative to the content directory.
 * @param options Additional options for the file loader
 */
export function file(fileName: string, options?: FileOptions): Loader {
	if (fileName.includes('*')) {
		throw new AstroError(FileGlobNotSupported);
	}

	let parse: ((text: string) => any) | null = null;

	const ext = fileName.split('.').at(-1);
	if (ext === 'json') {
		parse = JSON.parse;
	} else if (ext === 'yml' || ext === 'yaml') {
		parse = (text) =>
			yaml.load(text, {
				filename: fileName,
			});
	} else if (ext === 'toml') {
		parse = toml.parse;
	}
	if (options?.parser) parse = options.parser;

	if (parse === null) {
		throw new AstroError({
			...FileParserNotFound,
			message: FileParserNotFound.message(fileName),
		});
	}

	async function syncData(filePath: string, { logger, parseData, store, config }: LoaderContext) {
		let data: Array<Record<string, unknown>> | Record<string, Record<string, unknown>>;

		try {
			const contents = await fs.readFile(filePath, 'utf-8');
			data = await parse!(contents);
		} catch (error: any) {
			logger.error(`Error reading data from ${fileName}`);
			logger.debug(error.message);
// ... (70 more lines)

Subdomains

Functions

Dependencies

  • ../content/utils.js
  • ../core/errors/errors-data.js
  • ../core/errors/index.js
  • ./loaders/types.js
  • js-yaml
  • node:fs
  • node:url
  • smol-toml

Frequently Asked Questions

What does file.ts do?
file.ts is a source file in the astro codebase, written in typescript. It belongs to the ContentCollections domain, DataLoaders subdomain.
What functions are defined in file.ts?
file.ts defines 2 function(s): file, text.
What does file.ts depend on?
file.ts imports 8 module(s): ../content/utils.js, ../core/errors/errors-data.js, ../core/errors/index.js, ./loaders/types.js, js-yaml, node:fs, node:url, smol-toml.
Where is file.ts in the architecture?
file.ts is located at packages/astro/src/content/loaders/file.ts (domain: ContentCollections, subdomain: DataLoaders, directory: packages/astro/src/content/loaders).

Analyze Your Own Codebase

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

Try Supermodel Free