Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 4x 8x 8x 8x 18x 18x 4x 4x 14x 13x 13x 8x 4x 4x 4x 11x 4x 2x 9x 9x | /**
* File system utilities that are agnostic of your use case.
*/
import { writeFile } from '@metamask/utils/node';
import { promises as fs } from 'node:fs';
import path from 'node:path';
import { excludeGitIgnored } from './git-utils.ts';
/**
* A map of file paths to file contents.
*/
export type FileMap = Record<string, string>;
/**
* Recursively reads a directory and returns a map of file paths to file contents.
* The file paths are relative to the specified directory.
*
* @param baseDir - An absolute path to the directory to read files from.
* @returns A map of file paths to file contents.
*/
export async function readAllFiles(baseDir: string): Promise<FileMap> {
const readAllFilesRecur = async (dir: string): Promise<FileMap> => {
const result: FileMap = {};
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
const subDirResult = await readAllFilesRecur(fullPath);
Object.assign(result, subDirResult);
} else if (entry.isFile()) {
const content = await fs.readFile(fullPath, 'utf-8');
result[fullPath] = content;
}
}
return result;
};
const absoluteFileMap = await readAllFilesRecur(baseDir);
const filteredFileMap = await excludeGitIgnored(absoluteFileMap);
const relativeFileMap = Object.fromEntries(
Object.entries(filteredFileMap).map(([filePath, content]) => [
path.relative(baseDir, filePath),
content,
]),
);
return relativeFileMap;
}
/**
* Writes the specified files to disk. Recursively creates directories as needed.
*
* @param parentDirectory - The absolute path of the parent directory to write the files to.
* @param fileMap - A map of file paths to file contents. The file paths must be relative to
* the parent directory.
*/
export async function writeFiles(
parentDirectory: string,
fileMap: FileMap,
): Promise<void> {
for (const [relativePath, content] of Object.entries(fileMap)) {
const fullPath = path.join(parentDirectory, relativePath);
await writeFile(fullPath, content);
}
}
|