All files / infrastructure/files file.engine.ts

92.85% Statements 26/28
50% Branches 3/6
100% Functions 12/12
92.59% Lines 25/27

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 761x 1x   1x         1x 1x 1x   1x             1x 2x 2x 2x     2x                   1x 4x               1x 1x 1x 1x     1x                     1x 1x             1x 2x   1x   1x  
import * as fs from 'fs-extra';
import * as path from 'path';
 
export class FileEngine {
    private static instance: FileEngine;
 
    private constructor() {}
 
    public static getInstance() {
        Eif (!FileEngine.instance) {
            FileEngine.instance = new FileEngine();
        }
        return FileEngine.instance;
    }
 
    /**
     * Read async proxy
     * @param filepath Path of the file to read
     */
    public async get(filepath: string): Promise<string> {
        return new Promise<string>((resolve, reject) => {
            fs.readFile(path.resolve(filepath), 'utf8', (err, data) => {
                Iif (err) {
                    reject('Error during ' + filepath + ' read');
                } else {
                    resolve(data);
                }
            });
        });
    }
 
    /**
     * Read sync proxy
     * @param filepath Path of the file to read
     */
    public getSync(filepath: string): string {
        return fs.readFileSync(path.resolve(filepath), 'utf8');
    }
 
    /**
     * Async write content to file
     * @param filepath Path of the file to write
     * @param contents Content of the file to write
     */
    public async write(filepath: string, contents: string): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            fs.outputFile(path.resolve(filepath), contents, err => {
                Iif (err) {
                    reject(err);
                } else {
                    resolve();
                }
            });
        });
    }
 
    /**
     * Sync write content to file
     * @param filepath Path of the file to write
     * @param contents Content of the file to write
     */
    public writeSync(filepath: string, contents: string): void {
        fs.outputFileSync(filepath, contents);
    }
 
    /**
     * Sync check file exist
     * @param file The file to check
     */
    public existsSync(file: string): boolean {
        return fs.existsSync(file);
    }
}
 
export default FileEngine.getInstance();