All files / src file-helpers.ts

87.5% Statements 7/8
83.33% Branches 5/6
100% Functions 2/2
87.5% Lines 7/8

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              28x 2x   28x             21x 16x   5x 5x        
import fs from 'fs-extra';
import path from 'path';
 
/**
 * Creates file with given content with possible parent directories creation.
 */
export async function createDirAndWriteFile(filePath: string, content: string) {
  if (!(await fs.pathExists(path.dirname(filePath)))) {
    await fs.mkdirp(path.dirname(filePath));
  }
  await fs.writeFile(filePath, content);
}
 
/**
 * Reads given file as UTF-8 with fallback to given content when file is not found.
 */
export async function readFileWithFallback(filePath: string, fallbackContent?: string) {
  if (await fs.pathExists(filePath)) {
    return fs.readFile(filePath, 'utf-8');
  }
  Eif (fallbackContent) {
    return fallbackContent;
  }
  throw Error(`File not found ${filePath}`);
}