All files writable_file.ts

100% Statements 24/24
82.35% Branches 14/17
100% Functions 4/4
100% Lines 19/19

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 261x 1x   1x   1x 23x 46x 46x 22x 22x   1x 5x 5x 4x   1x 3x 4x 4x   3x   1x  
import * as fs from 'fs-extra';
import {ReadableFile} from './readable_file';
 
export class WritableFile extends ReadableFile {
  props?: ConstructorParameters<typeof ReadableFile>[0];
  async writeFromString(content?: string) {
    content = content ?? '';
    await fs.mkdirp(this.dirname);
    await fs.writeFile(this.path, content);
    this.cacheContent = content;
    return this.content;
  }
  async createFromString(content?: string) {
    content = content ?? '';
    if (await this.exists) throw new Error('cannot create file, already exists');
    return this.writeFromString(content);
  }
  async upsertFromString(content?: string) {
    content = content ?? '';
    if (!(await this.exists)) {
      await this.createFromString(content);
    }
    return this.content;
  }
}