all files / synchronizator/src/ Synchronizator.js

0% Statements 0/21
0% Branches 0/2
0% Functions 0/3
0% Lines 0/21
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                                                                                                                                                           
let OptionsPreparer = require("./options/Preparer.js");
let SyncManager = require("./sync/Manager.js");
let ActionGatherer = require("./action/Gatherer.js");
let ActionHandler = require("./action/Handler.js");
 
/**
 * Synchronizator - main class
 *
 * @author Jan Busfy <jan.busfy@unitedclassifieds.sk>
 */
module.exports = function(srcDir, destDir, options)
{
    /**
     * Private context
     *
     * @var Object
     */
    let _this = {};
    
    /**
     * Source directory
     *
     * @var String
     */
    _this.srcDir = srcDir;
    
    /**
     * Destination directory
     *
     * @var String
     */
    _this.destDir = destDir;
    
    /**
     * Options
     *
     * @var Object
     */
    _this.options = new OptionsPreparer().prepare(options);
    
    
    /**
     * Initializes synchronizator 
     */
    _this.init = function()
    {
        _this.doInitialSync(() => {
            let actionHandler = new ActionHandler(_this.srcDir, _this.destDir);
            
            new ActionGatherer(
                _this.srcDir,
                _this.options.ignored,
                (actionData) => {
                    actionHandler.add(actionData);
                }
            );
        });
    };
    
    /**
     * Performs initial synchronization if needed
     *
     * @param Function onFinish() - called when initial sync has completed
     */
    _this.doInitialSync = function(onFinish)
    {
        if (_this.options.performInitialSync === false) {
            onFinish();
            return;
        }
        
        new SyncManager(_this.srcDir, _this.destDir, _this.options.ignored).sync(() => {
            onFinish();
        });
    };
    
    _this.init();
};