all files / dir-compare/ compareSync.js

89.58% Statements 86/96
88.64% Branches 78/88
75% Functions 3/4
89.58% Lines 86/96
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                     338× 65× 273× 273×   273× 273× 606× 606×                 606× 509×     273×                                   169× 169× 169× 169× 356× 356× 356× 356× 356× 356× 356× 356× 356×     356× 356× 287× 287× 287× 69× 31× 31× 31×   38× 38× 38×       356×   153× 153× 153× 73× 73× 73× 13× 60×   56×     80×   153× 153×   153× 153× 73×   80×                     153× 153× 153× 147× 78× 69×   69×       203×   102× 102×   102× 102× 70×   32×   102× 102× 28×       101× 101×   101× 101× 58×   43×   101× 101× 37×          
var fs = require('fs');
var pathUtils = require('path');
var common = require('./common'); 
 
 
 
 
 
 
/**
 * Returns the sorted list of entries in a directory.
 */
var getEntries = function (path, options) {
	// TODO: fs.statSync(path) is called twice. Cache the result of the first call
    if (!path) {
        return [];
    } else Eif (fs.statSync(path).isDirectory()) {
        var entries = fs.readdirSync(path);
 
        var res = [];
        entries.forEach(function (entryName) {
            var entryPath = path + '/' + entryName;
            var entry = {
                name : entryName,
                path : entryPath,
                stat : fs.statSync(entryPath),
                symlink : fs.lstatSync(entryPath).isSymbolicLink(),
                toString : function () {
                    return this.name;
                }
            };
            if (common.filterEntry(entry, options)){
                res.push(entry);
            }
        });
        return options.ignoreCase?res.sort(common.compareEntryIgnoreCase):res.sort(common.compareEntryCaseSensitive);
    } else {
        var name = pathUtils.basename(path);
        return [
            {
                name : name,
                path : path,
                stat : fs.statSync(path)
            }
 
        ];
    }
}
 
 
/**
 * Compares two directories synchronously.
 */
var compare = function (path1, path2, level, relativePath, options, statistics, diffSet) {
    var entries1 = getEntries(path1, options);
    var entries2 = getEntries(path2, options);
    var i1 = 0, i2 = 0;
    while (i1 < entries1.length || i2 < entries2.length) {
        var entry1 = entries1[i1];
        var entry2 = entries2[i2];
        var n1 = entry1 ? entry1.name : undefined;
        var n2 = entry2 ? entry2.name : undefined;
        var p1 = entry1 ? entry1.path : undefined;
        var p2 = entry2 ? entry2.path : undefined;
        var fileStat1 = entry1 ? entry1.stat : undefined;
        var fileStat2 = entry2 ? entry2.stat : undefined;
        var type1, type2;
 
        // compare entry name (-1, 0, 1)
        var cmp;
        if (i1 < entries1.length && i2 < entries2.length) {
            cmp = options.ignoreCase?common.compareEntryIgnoreCase(entry1, entry2):common.compareEntryCaseSensitive(entry1, entry2);
            type1 = common.getType(fileStat1);
            type2 = common.getType(fileStat2);
        } else if (i1 < entries1.length) {
            type1 = common.getType(fileStat1);
            type2 = common.getType(undefined);
            cmp = -1;
        } else {
            type1 = common.getType(undefined);
            type2 = common.getType(fileStat2);
            cmp = 1;
        }
 
        // process entry
        if (cmp === 0) {
            // Both left/right exist and have the same name
            Eif (type1 === type2) {
                var same;
                if(type1==='file'){
                    var compareSize = options.compareSize === undefined ? false : options.compareSize;
                    var compareContent = options.compareContent === undefined ? false : options.compareContent;
                    if (compareSize && fileStat1.size !== fileStat2.size) {
                        same = false;
                    } else if(compareContent){
                        same = options.compareFileSync(p1, fileStat1, p2, fileStat2, options);
                    } else{
                        same = true;
                    }
                } else{
                    same = true;
                }
                Eif(!options.noDiffSet){
                    options.resultBuilder(entry1, entry2, same ? 'equal' : 'distinct', level, relativePath, options, statistics, diffSet);
                }
                same ? statistics.equal++ : statistics.distinct++;
                if(type1==='file'){
                    same ? statistics.equalFiles++ : statistics.distinctFiles++;
                } else{
                    same ? statistics.equalDirs++ : statistics.distinctDirs++;
                }
            } else {
                // File and directory with same name
                if(!options.noDiffSet){
                    options.resultBuilder(entry1, entry2, 'distinct', level, relativePath, options, statistics, diffSet);
                }
                statistics.distinct+=2;
                statistics.distinctFiles++;
                statistics.distinctDirs++;
            }
            i1++;
            i2++;
            if(!options.skipSubdirs){
                if (type1 === 'directory' && type2 === 'directory') {
                    compare(p1, p2, level + 1, relativePath + '/' + entry1.name, options, statistics, diffSet);
                } else Iif (type1 === 'directory') {
                    compare(p1, undefined, level + 1, relativePath + '/' + entry1.name, options, statistics, diffSet);
                } else Iif (type2 === 'directory') {
                    compare(undefined, p2, level + 1, relativePath + '/' + entry2.name, options, statistics, diffSet);
                }
            }
        } else if (cmp < 0) {
            // Right missing
            Eif(!options.noDiffSet){
                options.resultBuilder(entry1, undefined, 'left', level, relativePath, options, statistics, diffSet);
            }
            statistics.left++;
            if(type1==='file'){
                statistics.leftFiles++;
            } else{
                statistics.leftDirs++;
            }
            i1++;
            if (type1 === 'directory' && !options.skipSubdirs) {
                compare(p1, undefined, level + 1, relativePath + '/' + entry1.name, options, statistics, diffSet);
            }
        } else {
            // Left missing
            Eif(!options.noDiffSet){
                options.resultBuilder(undefined, entry2, 'right', level, relativePath, options, statistics, diffSet);
            }
            statistics.right++;
            if(type2==='file'){
                statistics.rightFiles++;
            } else{
                statistics.rightDirs++;
            }
            i2++;
            if (type2 === 'directory' && !options.skipSubdirs) {
                compare(undefined, p2, level + 1, relativePath + '/' + entry2.name, options, statistics, diffSet);
            }
        }
    }
};
 
module.exports = compare;