Home / Function/ copyFilesToFolder() — astro Function Reference

copyFilesToFolder() — astro Function Reference

Architecture documentation for the copyFilesToFolder() function in fs.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  c9093454_1641_c9ea_facc_eb0e4d27e342["copyFilesToFolder()"]
  ae97098f_05c3_e11f_9a05_46a9f8fa42ea["fs.ts"]
  c9093454_1641_c9ea_facc_eb0e4d27e342 -->|defined in| ae97098f_05c3_e11f_9a05_46a9f8fa42ea
  style c9093454_1641_c9ea_facc_eb0e4d27e342 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/internal-helpers/src/fs.ts lines 43–89

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

Frequently Asked Questions

What does copyFilesToFolder() do?
copyFilesToFolder() is a function in the astro codebase, defined in packages/internal-helpers/src/fs.ts.
Where is copyFilesToFolder() defined?
copyFilesToFolder() is defined in packages/internal-helpers/src/fs.ts at line 43.

Analyze Your Own Codebase

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

Try Supermodel Free