Home / File/ fs.ts — astro Source File

fs.ts — astro Source File

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

File typescript CoreAstro CoreMiddleware 4 imports 5 functions

Entity Profile

Dependency Diagram

graph LR
  ae97098f_05c3_e11f_9a05_46a9f8fa42ea["fs.ts"]
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  ae97098f_05c3_e11f_9a05_46a9f8fa42ea --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  5d6d1861_a18d_b246_cd94_08889ab7e74c["promises"]
  ae97098f_05c3_e11f_9a05_46a9f8fa42ea --> 5d6d1861_a18d_b246_cd94_08889ab7e74c
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  ae97098f_05c3_e11f_9a05_46a9f8fa42ea --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  d9a92db9_c95e_9165_13ac_24b3d859d946["node:url"]
  ae97098f_05c3_e11f_9a05_46a9f8fa42ea --> d9a92db9_c95e_9165_13ac_24b3d859d946
  style ae97098f_05c3_e11f_9a05_46a9f8fa42ea fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { PathLike } from 'node:fs';
import { existsSync } from 'node:fs';
import * as fs from 'node:fs/promises';
import nodePath from 'node:path';
import { fileURLToPath } from 'node:url';

export async function writeJson<T>(path: PathLike, data: T) {
	await fs.writeFile(path, JSON.stringify(data, null, '\t'), { encoding: 'utf-8' });
}

export async function removeDir(dir: PathLike) {
	await fs.rm(dir, { recursive: true, force: true, maxRetries: 3 });
}

export async function emptyDir(dir: PathLike): Promise<void> {
	await removeDir(dir);
	await fs.mkdir(dir, { recursive: true });
}

export async function getFilesFromFolder(dir: URL) {
	const data = await fs.readdir(dir, { withFileTypes: true });
	let files: URL[] = [];
	for (const item of data) {
		if (item.isDirectory()) {
			const moreFiles = await getFilesFromFolder(new URL(`./${item.name}/`, dir));
			files = files.concat(moreFiles);
		} else {
			files.push(new URL(`./${item.name}`, dir));
		}
	}
	return files;
}

/**
 * Copies files into a folder keeping the folder structure intact.
 * The resulting file tree will start at the common ancestor.
 *
 * @param {URL[]} files A list of files to copy (absolute path).
 * @param {URL} outDir Destination folder where to copy the files to (absolute path).
 * @param {URL[]} [exclude] A list of files to exclude (absolute path).
 * @returns {Promise<string>} The common ancestor of the copied files.
 */
export async function copyFilesToFolder(
	files: URL[],
	outDir: URL,
	exclude: URL[] = [],
): Promise<string> {
	const excludeList = exclude.map((url) => fileURLToPath(url));
	const fileList = files.map((url) => fileURLToPath(url)).filter((f) => !excludeList.includes(f));

	if (files.length === 0) throw new Error('No files found to copy');

	let commonAncestor = nodePath.dirname(fileList[0]);
	for (const file of fileList.slice(1)) {
		while (!file.startsWith(commonAncestor)) {
			commonAncestor = nodePath.dirname(commonAncestor);
		}
	}

	for (const origin of fileList) {
		const dest = new URL(nodePath.relative(commonAncestor, origin), outDir);

		const realpath = await fs.realpath(origin);
		const isSymlink = realpath !== origin;
		const isDir = (await fs.stat(origin)).isDirectory();

		// Create directories recursively
		if (isDir && !isSymlink) {
			await fs.mkdir(new URL('..', dest), { recursive: true });
		} else {
			await fs.mkdir(new URL('.', dest), { recursive: true });
		}

		if (isSymlink) {
			const realdest = fileURLToPath(new URL(nodePath.relative(commonAncestor, realpath), outDir));
			const target = nodePath.relative(fileURLToPath(new URL('.', dest)), realdest);
			// NOTE: when building function per route, dependencies are linked at the first run, then there's no need anymore to do that once more.
			// So we check if the destination already exists. If it does, move on.
			// Symbolic links here are usually dependencies and not user code. Symbolic links exist because of the pnpm strategy.
			if (!existsSync(dest)) {
				await fs.symlink(target, dest, isDir ? 'dir' : 'file');
			}
		} else if (!isDir) {
			await fs.copyFile(origin, dest);
		}
	}

	return commonAncestor;
}

Domain

Subdomains

Dependencies

  • node:fs
  • node:path
  • node:url
  • promises

Frequently Asked Questions

What does fs.ts do?
fs.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in fs.ts?
fs.ts defines 5 function(s): copyFilesToFolder, emptyDir, getFilesFromFolder, removeDir, writeJson.
What does fs.ts depend on?
fs.ts imports 4 module(s): node:fs, node:path, node:url, promises.
Where is fs.ts in the architecture?
fs.ts is located at packages/internal-helpers/src/fs.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/internal-helpers/src).

Analyze Your Own Codebase

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

Try Supermodel Free