all files / src/action/ Gatherer.js

0% Statements 0/13
100% Branches 0/0
0% Functions 0/3
0% Lines 0/13
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                                                                                                                                     
let chokidar = require('chokidar');
let ActionData = require("./Data.js");
 
/**
 * Wathces filesystem and gathers actions
 *
 * @author Jan Busfy <jan.busfy@unitedclassifieds.sk>
 */
module.exports = function(srcDir, ignored, onNewAction)
{
    /**
     * Private context
     *
     * @var Object
     */
    let _this = {};
    
    /**
     * Source directory
     *
     * @var String
     */
    _this.srcDir = srcDir;
    
    /**
     * Destination directory
     *
     * @var String
     */
    _this.ignored = ignored;
    
    /**
     * Called when new action has occured
     *
     * @var Function(ActionData actionData)
     */
    _this.onNewAction = onNewAction;
    
    
    /**
     * Initializes watcher 
     */
    _this.init = function()
    {
        chokidar.watch(
            _this.srcDir + "/**/*",
            {
                ignored: function (path) {
                    return _this.ignored.filter((ignoredItem) => {
                        return path.indexOf(ignoredItem) !== -1;
                    }).length > 0;
                },
                persistent: true,
                ignoreInitial: true
            }
        ).on('all', (event, path) => {
            _this.onNewAction(
                new ActionData(
                    event,
                    path 
                )
            );
        });
    };
    
    _this.init();
};