All files pluginManager.js

83.63% Statements 46/55
64.28% Branches 18/28
83.33% Functions 5/6
84.9% Lines 45/53

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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120  37x 37x 37x 37x 37x       24x 24x 24x 24x 24x 24x       24x 24x     24x   24x 4x 4x 4x 4x   20x         24x 24x 24x 10x                         4x 4x 4x 4x 4x       4x     4x         4x 3x       1x 1x                                 4x 4x   4x 4x                 4x   4x 4x   4x 4x                 37x  
/* eslint-disable no-console */
const fs = require('fs'),
    path = require('path'),
    Executor = require('./executor'),
    execPath = process.cwd(),
    utils = require('./utils.js');
 
class PluginManager {
    constructor (instance) {
        this.userPath = `${execPath}/plugins/`;
        this.maltaPath = `${__dirname}/../plugins/`;
        this.testPath = `${__dirname}/../test/`;
        this.mself = instance;
        this.plugins = {};
        this.executor = null;
    }
 
    run () {
        const mself = this.mself,
            pluginKeys = Object.keys(this.plugins);
        let iterator;
 
        if (pluginKeys.length) mself.log_info('Starting plugins'.yellow());
 
        if (mself.hasPlugins) {
            mself.log_debug(`on ${mself.outName.underline()} called plugins:`);
            iterator = utils.getIterator(pluginKeys);
            this.executor = new Executor(iterator, mself, this);
            this.executor.run();
        } else {
            this.maybeNotifyBuild();
        }
    }
 
    maybeNotifyBuild (gotErrs) {
        const mself = this.mself,
            now = `${new Date()}`;
        if (mself.constructor.verbose > 0 && mself.notifyBuild) {
            mself.sticky(
                ['Malta @ ', now.replace(/(GMT.*)$/, '')].join(''), [
                    path.basename(mself.outName),
                    'build completed in',
                    mself.t_end - mself.t_start,
                    'ms'
                ].join(' '),
                gotErrs
            );
        }
    }
 
    add (fname, params) {
        const userPath = `${this.userPath}${fname}.js`,
            userPathFolder = `${this.userPath}${fname}/index.js`,
            testPathFolder = `${this.testPath}${fname}/index.js`,
            maltaPath = `${this.maltaPath}${fname}.js`,
            self = this;
 
        let plugin;
 
        try {
            // first the user execution dir
            //
            Iif (fs.existsSync(userPath)) {
                plugin = require(userPath);
 
                // then check if malta package has it, in folder/index.js
                //
            } else if (fs.existsSync(testPathFolder)) {
                plugin = require(testPathFolder);
 
                // or in file
                //
            } else Eif (fs.existsSync(userPathFolder)) {
                plugin = require(userPathFolder);
 
                // or in file
                //
            } else if (fs.existsSync(maltaPath)) {
                plugin = require(maltaPath);
 
                // otherwise most likely is available as package if installed
                //
            } else {
                plugin = require(fname);
            }
        } catch (e) {
            this.mself.log_err(`\`${fname}\` required plugin not found OR there was an error in the plugin!`);
            this.mself.log_err(e);
        }
 
        Eif ('ext' in plugin) {
            Iif (utils.isArray(plugin.ext)) {
                plugin.ext.forEach(ext => self.doAdd(ext, plugin, params));
            } else Eif (utils.isString(plugin.ext)) {
                this.doAdd(plugin.ext, plugin, params);
            }
        } else {
            this.doAdd('*', plugin, params);
        }
    }
 
    doAdd (el, plu, params) {
        // handle * wildcard
        el = el === '*' ? this.mself.tplName.split('.').pop() : el;
 
        Eif (!(el in this.plugins)) {
            this.plugins[el] = [];
        }
        Eif (!(plu.name in this.plugins[el])) {
            this.plugins[el].push({
                name: plu.name,
                func: plu,
                params: params
            });
        }
    }
}
 
module.exports = PluginManager;