All files / src executor.js

84.61% Statements 33/39
62.5% Branches 10/16
83.33% Functions 5/6
83.78% Lines 31/37

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 76 77  33x     1x 1x 1x     33x 2x 2x   2x 1x 1x   1x 1x 1x       2x 1x 1x 1x 1x 1x 1x         1x 1x                         1x             1x 1x       33x 1x 1x           1x       1x     33x  
/* eslint-disable no-console */
const utils = require('./utils.js');
 
function Executor (iterator, maltaInstance, pmInstance) {
    this.iterator = iterator;
    this.malta = maltaInstance;
    this.pm = pmInstance;
}
 
Executor.prototype.run = function () {
    const self = this,
        malta = this.malta;
 
    if (this.iterator.hasNext()) {
        const ext = this.iterator.next(),
            pins = self.pm.plugins[ext] || [];
 
        Eif (malta.outName.match(new RegExp(`.*\\.${ext}$`))) {
            const iterator = utils.getIterator(pins);
            (function go () {
                let res,
                    pl;
 
                if (iterator.hasNext()) {
                    pl = iterator.next();
                    res = self.callPlugin(pl);
                    Eif (res) {
                        (new Promise(res)).then(function (obj) {
                            Eif (malta.userWatch) malta.userWatch.call(malta, obj, pl);
                            malta.data.name = obj.name;
 
                            // replace the name given by the plugin fo the file
                            // produced and to be passed to the next plugin
                            //
                            malta.data.content = `${obj.content}`;
                            go();
                        }).catch(function (msg) {
                            self.pm.maybeNotifyBuild({ gotErrs: msg });
                            malta.log_debug(`Plugin '${pl.name}' error: `);
                            // console.log(Malta.TAB + msg);
                            malta.log_debug(msg);
                            go();
                            // malta.stop();
                        });
                    } else {
                        go();
                    }
                } else {
                    self.run();
                }
            })();
        } else {
            self.run();
        }
    } else {
        Eif (typeof malta.endCb === 'function') malta.endCb(malta.data);
        self.pm.maybeNotifyBuild();
    }
};
 
Executor.prototype.callPlugin = function (p) {
    const malta = this.malta;
    malta.log_debug([
        '> ',
        p.name.yellow(),
        (p.params ? `called passing ${JSON.stringify(p.params).darkgray()}` : '')
    ].join(''));
 
    malta.doBuild = true;
    // actually I dont` need to pass data, since it can be retrieved by the context,
    // but is better (and I don`t have to modify every plugin and the documentation)
    //
    return p.func.bind(malta)(malta.data, p.params);
};
 
module.exports = Executor;