all files / synchronizator/src/sync/ Manager.js

0% Statements 0/20
0% Branches 0/2
0% Functions 0/3
0% Lines 0/20
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                                                                                                                                                                   
let commandExists = require('command-exists');
let execSh = require('exec-sh');
 
/**
 * Sync manager - synchronizes
 *
 * @author Jan Busfy <jan.busfy@unitedclassifieds.sk>
 */
module.exports = function(srcDir, destDir, ignored)
{
    /**
     * Private context
     *
     * @var Object
     */
    let _this = {};
    
    /**
     * Source directory
     *
     * @var String
     */
    _this.srcDir = srcDir;
    
    /**
     * Destination directory
     *
     * @var String
     */
    _this.destDir = destDir;
    
    /**
     * Ignored patterns
     *
     * @var Array of String
     */
    _this.ignored = ignored;
    
    
    /**
     * Performs synchronization
     *
     * @param Function onFinish() - called when sync is complete
     */
    this.sync = function(onFinish)
    {
        commandExists("rsync", (err, exists) => {
            if (!exists) {
                onFinish();
                return;
            }
            
            execSh(
                _this.prepareCommandString(),
                true,
                () => {
                    onFinish();
                }
            );
        });
    };
    
    /**
     * Prepares command string
     *
     * @return String
     */
    _this.prepareCommandString = function()
    {
        let commandString = "rsync -update -raz ";
        
        commandString += _this.ignored
            .map((ignoredItem) => {
                return "--exclude " + ignoredItem
            })
            .join(" ");
            
        commandString += " " + _this.srcDir + "/* " + _this.destDir;
        
        return commandString;
    };
};