Skip to content

🎥 Make videos programmatically with React

1.3 KB
Raw Blame
import * as fs from 'node:fs';
import * as path from 'node:path';
import type {ReadDir} from '../functions/types';
import {getEtagOfFile} from './get-etag';

// Function to recursively read a directory and return a list of files
// with their etags and file names
export const readDirectory: ReadDir = ({
	dir,
	etags,
	originalDir,
	onProgress,
}) => {
	const files = fs.readdirSync(dir);

	for (const file of files) {
		if (file.startsWith('.DS_Store')) continue;

		const filePath = path.join(dir, file);

		if (fs.lstatSync(filePath).isDirectory()) {
			etags = {
				...etags,
				...readDirectory({
					dir: filePath,
					etags,
					originalDir,
					onProgress,
				}),
			};
			continue;
		}

		const relative = path
			.relative(originalDir, filePath)
			.split(path.sep)
			.join('/');

		if (fs.lstatSync(filePath).isSymbolicLink()) {
			if (fs.statSync(filePath).isDirectory()) {
				throw new Error(
					`The directory ${originalDir} contains a symbolic link to a directory at ${relative}. Directory symbolic links are not supported when deploying a site.`,
				);
			}

			const realPath = fs.realpathSync(filePath);

			etags[relative] = getEtagOfFile(realPath, onProgress);
		} else {
			etags[relative] = getEtagOfFile(filePath, onProgress);
		}
	}

	// Return the list of files with their etags and file names
	return etags;
};