all files / dir-compare/ print.js

92.92% Statements 105/113
90% Branches 90/100
85.71% Functions 6/7
92.86% Lines 104/112
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188                             804× 48× 48× 48× 48× 48×     48× 48× 46× 610× 52×   610× 610× 104×           48×   48× 48× 644× 644×   644×   174× 174× 94×     644× 550×   206× 206× 206×   160× 160× 160×   156× 156× 156×   28× 28× 28×         550× 426× 34×   392×               48× 48× 34× 34× 34× 34× 34×   14× 14× 14× 14× 14×   48× 42×   48×                       392×   392× 392×   182× 182×   94× 94×   92× 92×   24× 24×       392× 392× 392× 392× 144×   392× 392× 392× 334×   58×       1394× 1394× 1394× 1394× 1394×           34× 34× 14×   34× 12×     34× 34× 34×   34× 34×   34× 34×   34×      
var colors = require('colors');
var util = require('util');
 
var tab = function (tabs) {
    var res = '';
    while (tabs>=0) {
        res += ' ';
        tabs--;
    }
    return res;
};
 
// Prints dir compare results.
// 'program' represents display options and correspond to dircompare command line parameters.
// Example: 'dircompare --show-all --exclude *.js dir1 dir2' translates into 
// program: {showAll: true, exclude: '*.js'}
// 
var print = function(res, writer, program){
    var nocolor = function(str){return str};
    var cequal = program.nocolors?nocolor:colors.green;
    var cdistinct = program.nocolors?nocolor:colors.red;
    var cleft = program.nocolors?nocolor:colors.cyan;
    var cright = program.nocolors?nocolor:colors.magenta;
    var cdir = program.nocolors?nocolor:colors.gray;
 
    // calculate relative path length for pretty print
    var relativePathMaxLength = 0, fileNameMaxLength=0;
    if(!program.csv && res.diffSet){
        res.diffSet.forEach(function (detail) {
            if(detail.relativePath.length>relativePathMaxLength){
                relativePathMaxLength = detail.relativePath.length;
            }
            var len = getCompareFile(detail, '??').length;
            if(len>fileNameMaxLength){
                fileNameMaxLength = len;
            }
        });
    }
 
    // csv header
    if(program.csv){
        writer.write('path,name,state,type,size1,size2,date1,date2\n');
    }
    Eif(res.diffSet){
        for(var i = 0; i<res.diffSet.length; i++){
            var detail = res.diffSet[i];
            var color, show = true;
 
            if(!program.wholeReport){
                // show only files
                var type = detail.type1!=='missing'?detail.type1:detail.type2;
                if(type!=='file'){
                    show = false;
                }
            }
            if(show){
                switch (detail.state) {
                case 'equal':
                    color = cequal;
                    show = program.showAll || program.showEqual?true:false;
                    break;
                case 'left':
                    color = cleft;
                    show = program.showAll || program.showLeft?true:false; 
                    break;
                case 'right':
                    color = cright;
                    show = program.showAll || program.showRight?true:false; 
                    break;
                case 'distinct':
                    color = cdistinct;
                    show = program.showAll || program.showDistinct?true:false; 
                    break;
                default:
                    show = true;
                color = colors.gray;
                }
                if(show){
                    if(program.csv){
                        printCsv(writer, detail, color);
                    } else {
                        printPretty(writer, program, detail, color, cdir, relativePathMaxLength, fileNameMaxLength);
                    }
                }
            }
        }
    }
 
    // PRINT STATISTICS
    var statTotal, statEqual, statLeft, statRight, statDistinct;
    if(program.wholeReport){
        statTotal = res.total;
        statEqual = res.equal;
        statLeft = res.left;
        statRight = res.right;
        statDistinct = res.distinct;
    } else{
        statTotal = res.totalFiles;
        statEqual = res.equalFiles;
        statLeft = res.leftFiles;
        statRight = res.rightFiles;
        statDistinct = res.distinctFiles;
    }
    if(!program.noDiffIndicator){
        writer.write(res.same?cequal('Entries are identical\n'):cdistinct('Entries are different\n'));
    }
    writer.write(util.format('total: %s, equal: %s, distinct: %s, only left: %s, only right: %s\n',
            statTotal,
            cequal(statEqual),
            cdistinct(statDistinct),
            cleft(statLeft),
            cright(statRight)
    ));
}
 
/**
 * Print details for default view mode
 */
var printPretty = function(writer, program, detail, color, dircolor, relativePathMaxLength, fileNameMaxLength){
    var path = detail.relativePath===''?'/':detail.relativePath;
 
    var state;
    switch (detail.state) {
    case 'equal':
        state = '==';
        break;
    case 'left':
        state = '->';
        break;
    case 'right':
        state = '<-';
        break;
    case 'distinct':
        state = '<>';
        break;
    default:
        state = '?';
    }
    var spacePad = relativePathMaxLength - path.length;
    var type ='';
    type = detail.type1!=='missing' ? detail.type1 : detail.type2;
    if(type==='directory'){
        type = dircolor(type);
    }
    var cmpentrylen = getCompareFile(detail, "??").length;
    var cmpentry = getCompareFile(detail, color(state));
    if(program.wholeReport){
        writer.write(util.format('[%s] %s(%s)\n', path, cmpentry, type));
    } else{
        writer.write(util.format('[%s] %s\n', path, cmpentry));
    }
}
 
var getCompareFile = function(detail, state){
    p1 = detail.name1 ? detail.name1 : '';
    p2 = detail.name2 ? detail.name2 : '';
    var missing1 = detail.type1==='missing' ? 'missing' : '';
    var missing2 = detail.type2==='missing' ? 'missing' : '';
    return util.format('%s%s%s%s%s', missing1, p1, state, missing2, p2);
}
 
/**
 * Print csv details.
 */
var printCsv = function(writer, detail, color){
    var size1='', size2='';
    if(detail.type1==='file'){
        size1 = detail.size1!=undefined ? detail.size1 : '';
    }
    if(detail.type2==='file'){
        size2 = detail.size2!=undefined ? detail.size2 : '';
    }
 
    var date1='', date2='';
    date1 = detail.date1!=undefined ? detail.date1.toISOString() : '';
    date2 = detail.date2!=undefined ? detail.date2.toISOString() : '';
 
    var type ='';
    type = detail.type1!=='missing' ? detail.type1 : detail.type2;
 
    var path = detail.relativePath?detail.relativePath:'/';
    var name = (detail.name1?detail.name1:detail.name2);
 
    writer.write(util.format('%s,%s,%s,%s,%s,%s,%s,%s\n', path, name, color(detail.state), type, size1, size2, date1, date2));
};
 
module.exports = print;