All files / scripts/locale file-service.ts

60% Statements 6/10
100% Branches 0/0
66.66% Functions 2/3
60% Lines 6/10

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 431x   1x               152x 152x   36x                     4x                                    
import { promises as fs } from "fs";
 
export class FileService {
  /**
   * Reads the content of a file at the specified path
   * @param filePath Path to the file to read
   * @returns The file content as a string, or null if the file doesn't exist or can't be read
   * @throws Never - returns null instead of throwing
   */
  async read(filePath: string): Promise<string | null> {
    try {
      return await fs.readFile(filePath, "utf-8");
    } catch {
      return null;
    }
  }
 
  /**
   * Writes content to a file at the specified path
   * @param filePath Path to the file to write
   * @param content Content to write to the file
   * @throws If the file can't be written to (e.g., permissions, disk space)
   */
  async write(filePath: string, content: string): Promise<void> {
    await fs.writeFile(filePath, content, "utf-8");
  }
 
  /**
   * Checks if a file exists at the specified path
   * @param filePath Path to check for file existence
   * @returns True if the file exists and is accessible, false otherwise
   * @throws Never - returns false instead of throwing
   */
  async exists(filePath: string): Promise<boolean> {
    try {
      await fs.access(filePath);
      return true;
    } catch {
      return false;
    }
  }
}