All files readable_file.ts

100% Statements 22/22
100% Branches 10/10
100% Functions 4/4
100% Lines 15/15

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 251x 1x   1x 113x 113x     144x 144x 144x   76x 65x   11x 9x 9x   2x       1x  
import * as fs from 'fs-extra';
import {Path} from './path';
 
export class ReadableFile extends Path {
  constructor(public props?: ConstructorParameters<typeof Path>[0] & {defaultContent?: string}) {
    super(props);
  }
  cacheContent?: string;
  get content() {
    return (async () => {
      if (this.cacheContent) return this.cacheContent;
      try {
        this.cacheContent = await fs.readFile(this.path, 'utf8');
        return this.cacheContent;
      } catch (e) {
        if (this.props?.defaultContent) {
          this.cacheContent = this.props.defaultContent;
          return this.cacheContent;
        }
        throw e;
      }
    })();
  }
}