All files / lib/fs fsUtils.ts

100% Statements 23/23
100% Branches 8/8
100% Functions 5/5
100% Lines 23/23

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 71 72 73 74 75 76 77 78 79 80 8128x 28x             28x 50x 50x   12x                 28x 18x 18x   2x                       28x 6x 2x     4x                       28x 23x 2x     21x                       28x 19x 18x 18x     19x      
import fs from 'fs';
import Path from 'path';
 
/**
 * Check if a path exists and is a file
 * @param path Path to check
 * @returns True if path points to file, false otherwise
 */
export async function pathIsFile(path: string): Promise<boolean> {
    try {
        return (await fs.promises.stat(path)).isFile();
    } catch {
        return false;
    }
}
 
/**
 * Check if a directory exists and is a file
 * @param path Path to check
 * @returns True if path points to directory, false otherwise
 */
export async function pathIsDirectory(path: string): Promise<boolean> {
    try {
        return (await fs.promises.stat(path)).isDirectory();
    } catch {
        return false;
    }
}
 
/**
 * List the files in a directory.
 * Throws an exception if path is not a directory.
 *
 * @param path Path to directory
 * @returns Returns an array of filenames of all files in a directory
 * @throws Throws if path does not point to a directory or if an IO error occurs
 */
export async function getDirectoryContents(path: string): Promise<string[]> {
    if (!(await pathIsDirectory(path))) {
        throw new Error(`Attempting to list files in directory that does not exist or is not a directory: "${ path }"`);
    }
    
    return fs.promises.readdir(path);
}
 
/**
 * Reads the contents of a file as a string.
 * Throws an exception if path is not a file.
 *
 * @param path Path to file
 * @param encoding File encoding. Defaults to UTF 8.
 * @returns Contents of file as a string
 * @throws Throws if path does not point to a file
 */
export async function readFile(path: string, encoding: BufferEncoding = 'utf-8'): Promise<string> {
    if (!(await pathIsFile(path))) {
        throw new Error(`Attempting to read file that does not exist or is not a file: "${ path }"`);
    }
    
    return fs.promises.readFile(path, encoding);
}
 
/**
 * Writes a file as UTF-8.
 * Throws an exception if path is not a file.
 *
 * @param path Path to file
 * @param content File contents
 * @param createPaths If true, directory tree will be created up to the file
 * @param encoding File encoding. Defaults to UTF 8.
 */
export async function writeFile(path: string, content: string, createPaths?: boolean, encoding: BufferEncoding = 'utf-8'): Promise<void> {
    if (createPaths) {
        const directory: string = Path.dirname(path);
        await fs.promises.mkdir(directory, { recursive: true });
    }
 
    return fs.promises.writeFile(path, content, encoding);
}