All files / src/utils BasicEditor.js

70% Statements 14/20
100% Branches 0/0
75% Functions 6/8
70.59% Lines 12/17

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          24x           119x 119x 119x                                   3x 3x 3x 3x     114x             114x 114x 114x      
import {join} from 'path';
import Queue from 'p-queue';
import memFs from 'mem-fs';
import editor from 'mem-fs-editor';
 
const {assign} = Object;
/**
 * Base class to serve as base for JSON and module builder classes
 */
export class BasicEditor {
    constructor() {
        const fs = editor.create(memFs.create());
        const queue = new Queue({concurrency: 1});
        assign(this, {fs, queue});
    }
    /**
     *
     * @param {string} destination Destination to copy file
     * @return {BasicEditor} Chaining OK
     */
    copy(destination) {
        const self = this;
        const {fs, path, queue} = self;
        const [filename] = path.split('/').reverse();
        queue.add(() => fs.copy(path, join(destination, filename)));
        return self;
    }
    /**
     * @return {BasicEditor} Chaining OK
     */
    delete() {
        const self = this;
        const {fs, path, queue} = self;
        queue.add(() => fs.delete(path));
        return self;
    }
    done() {
        return this.queue.onEmpty();
    }
    /**
     * Write changes to disk
     * @return {Promise} Resolves when queue is empty
     */
    async commit() {
        const {fs} = this;
        await new Promise(resolve => fs.commit(resolve));
        await this.done();
    }
}
export default BasicEditor;