All files functions.js

95.31% Statements 61/64
80.76% Branches 21/26
100% Functions 12/12
96.66% Lines 58/60

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  37x   37x 37x 37x 37x   37x   37x 9x 9x 9x 9x   9x       9x 3x 3x 3x 3x 3x         3x 3x 6x   5x 5x               3x 1x 2x 1x   1x 1x 1x 1x     2x 1x   1x   1x 1x 1x 1x         6x 6x     37x 14x   14x 2x 2x   12x 12x   12x   12x     37x 3x   2x 2x 2x 2x   1x 1x       37x          
#!/usr/bin/env node
let processNum = 0;
 
const Malta = require('./malta'),
    watcher = require('./observe'),
    spawn = require('child_process').spawn,
    fs = require('fs'),
    // eslint-disable-next-line quotes
    NL = "\n",
 
    multi = (key, el) => {
        const multi = key.match(/(.*)\/\*\.(.*)$/),
            isCommand = Malta.isCommand(key),
            exclude = filename => filename.match(/\.buildNum\.json$/),
            multiElements = {};
 
        let noDemon = key.match(/#(.*)/),
            folder,
            ext;
 
        if (!isCommand && multi) {
            noDemon = multi[0].match(/#(.*)/);
            folder = multi[1];
            ext = multi[2];
            fs.readdir(folder.replace(/^#/, ''), (err, files) => {
                Iif (err) {
                    // eslint-disable-next-line no-console
                    console.log(err);
                    throw err;
                }
                Eif (files) {
                    files.forEach(file => {
                        if (!exclude(file) && file.match(new RegExp(`.*\\.${ext}$`))) {
                            // store the process
                            ++processNum;
                            multiElements[file] = proceed(`${folder}/${file}`, el);
                        }
                    });
                }
            });
 
            // if demon mode then observe folder, add / remove
            //
            if (!noDemon) {
                watcher.observe(folder, (diff, extension) => {
                    diff.added.filter(function (v) {
                        return v.match(new RegExp(`.*\\.${extension}$`));
                    }).forEach(v => {
                        Iif (exclude(v)) return;
                        ++processNum;
                        multiElements[v] = proceed(`${folder}/${v}`, el);
                        Malta.log_debug(`${'ADDED'.yellow()} ${folder}/${v}${NL}`);
                    });
 
                    diff.removed.filter(v => {
                        return v.match(new RegExp(`.*\\.${extension}$`));
                    }).forEach(v => {
                        const outFile = multiElements[v].data.name;
                        // remove out file if exists
                        Eif (fs.existsSync(outFile)) fs.unlink(outFile, () => { });
                        multiElements[v].shut();
                        multiElements[v] = null;
                        Malta.log_debug(`${'REMOVED'.yellow()} ${folder}/${v}${NL}`);
                    });
                }, ext);
            }
        } else {
            ++processNum;
            proceed(key, el);
        }
    },
    proceed = (tpl, options) => {
        let i = 0,
            l;
        if (typeof options !== 'undefined' && options instanceof Array) {
            l = options.length;
            for (null; i < l; i++) proceed(tpl, options[i]);
        } else {
            options = options || '';
            let o = [tpl];
 
            o = o.concat(options.split(/\s/))
                .concat([`proc=${processNum}`]);
            return Malta.get().check(o).start();
        }
    },
    subCommand = command => {
        switch (command) {
            case '-clean':
                Malta.log('Removing all .buildNum.json files');
                spawn('find', ['.', '-name', '*.buildNum.json', '-type', 'f', '-delete']);
                Malta.log('... done');
                return true;
            default:
                Malta.log(`Command "${command}" not available`);
                return false;
        }
    };
 
module.exports = {
    multi,
    subCommand,
    proceed
};