File size: 8,982 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var fs = require("fs");
var path = require("path-posix");
var symlinkOrCopy = require("symlink-or-copy");
var Logger = require("heimdalljs-logger");
var entry_1 = require("./entry");
var util_1 = require("./util");
var logger = Logger('fs-tree-diff:');
var ARBITRARY_START_OF_TIME = 0;
var DEFAULT_DELEGATE = {
    unlink: function (inputPath, outputPath, relativePath) {
        try {
            fs.unlinkSync(outputPath);
        }
        catch (e) {
            if (typeof e === 'object' && e !== null && e.code === 'ENOENT') {
                return;
            }
            throw e;
        }
    },
    rmdir: function (inputPath, outputPath, relativePath) {
        fs.rmdirSync(outputPath);
    },
    mkdir: function (inputPath, outputPath, relativePath) {
        fs.mkdirSync(outputPath);
    },
    change: function (inputPath, outputPath, relativePath) {
        // We no-op if the platform can symlink, because we assume the output path
        // is already linked via a prior create operation.
        if (symlinkOrCopy.canSymlink) {
            return;
        }
        fs.unlinkSync(outputPath);
        symlinkOrCopy.sync(inputPath, outputPath);
    },
    create: function (inputPath, outputPath, relativePath) {
        symlinkOrCopy.sync(inputPath, outputPath);
    }
};
var FSTree = /** @class */ (function () {
    function FSTree(options) {
        if (options === void 0) { options = {}; }
        var entries = options.entries || [];
        if (options.sortAndExpand) {
            util_1.sortAndExpand(entries);
        }
        else {
            util_1.validateSortedUnique(entries);
        }
        this.entries = entries;
    }
    FSTree.fromPaths = function (paths, options) {
        if (options === void 0) { options = {}; }
        var entries = paths.map(function (path) {
            return new entry_1.default(path, 0, ARBITRARY_START_OF_TIME);
        });
        return new this({
            entries: entries,
            sortAndExpand: options.sortAndExpand,
        });
    };
    FSTree.fromEntries = function (entries, options) {
        if (options === void 0) { options = {}; }
        return new this({
            entries: entries,
            sortAndExpand: options.sortAndExpand,
        });
    };
    Object.defineProperty(FSTree.prototype, "size", {
        get: function () {
            return this.entries.length;
        },
        enumerable: true,
        configurable: true
    });
    FSTree.prototype.addEntries = function (entries, options) {
        if (!Array.isArray(entries)) {
            throw new TypeError('entries must be an array');
        }
        if (options !== null && typeof options === 'object' && options.sortAndExpand) {
            util_1.sortAndExpand(entries);
        }
        else {
            util_1.validateSortedUnique(entries);
        }
        var fromIndex = 0;
        var toIndex = 0;
        while (fromIndex < entries.length) {
            while (toIndex < this.entries.length &&
                this.entries[toIndex].relativePath < entries[fromIndex].relativePath) {
                toIndex++;
            }
            if (toIndex < this.entries.length &&
                this.entries[toIndex].relativePath === entries[fromIndex].relativePath) {
                this.entries.splice(toIndex, 1, entries[fromIndex++]);
            }
            else {
                this.entries.splice(toIndex++, 0, entries[fromIndex++]);
            }
        }
    };
    FSTree.prototype.addPaths = function (paths, options) {
        var entries = paths.map(function (path) {
            // TODO:
            // addPths + a custom isEqual comparator + custom Entry types are actually incompatible
            // As a addPaths will not abide by the custom Entry type
            // and will make this.entries be contain a mixture of types.
            // isEqual's arguments will then be typed incorrectly
            //
            // We should likely just deprecate `addPaths` in-favor of addEntries,
            // which correctly externalizes the creation of entry
            return new entry_1.default(path, 0, ARBITRARY_START_OF_TIME);
        });
        this.addEntries(entries, options);
    };
    FSTree.prototype.forEach = function (fn, context) {
        this.entries.forEach(fn, context);
    };
    FSTree.prototype.calculatePatch = function (theirFSTree, isEqual) {
        if (arguments.length > 1 && typeof isEqual !== 'function') {
            throw new TypeError('calculatePatch\'s second argument must be a function');
        }
        // TODO: the TS here is strange
        if (typeof isEqual !== 'function') {
            isEqual = this.constructor.defaultIsEqual;
        }
        var ours = this.entries;
        var theirs = theirFSTree.entries;
        var additions = [];
        var removals = [];
        var i = 0;
        var j = 0;
        var command;
        while (i < ours.length && j < theirs.length) {
            var x = ours[i];
            var y = theirs[j];
            if (x.relativePath < y.relativePath) {
                // ours
                i++;
                removals.push(removeOperation(x));
                // remove additions
            }
            else if (x.relativePath > y.relativePath) {
                // theirs
                j++;
                additions.push(addOperation(y));
            }
            else {
                if (!isEqual(x, y)) {
                    command = updateOperation(y);
                    if (x.isDirectory()) {
                        removals.push(command);
                    }
                    else {
                        additions.push(command);
                    }
                }
                // both are the same
                i++;
                j++;
            }
        }
        // cleanup ours
        for (; i < ours.length; i++) {
            removals.push(removeOperation(ours[i]));
        }
        // cleanup theirs
        for (; j < theirs.length; j++) {
            additions.push(addOperation(theirs[j]));
        }
        return removals.reverse().concat(additions);
    };
    FSTree.prototype.calculateAndApplyPatch = function (otherFSTree, input, output, delegate) {
        this.constructor.applyPatch(input, output, this.calculatePatch(otherFSTree), delegate);
    };
    FSTree.defaultIsEqual = function (entryA, entryB) {
        if (entryA.isDirectory() && entryB.isDirectory()) {
            // ignore directory changes by default
            return true;
        }
        var equal;
        if (entryA.size === entryB.size && entryA.mode === entryB.mode) {
            if (entryA.mtime === entryB.mtime) {
                equal = true;
            }
            else if (entryA.mtime === undefined || entryB.mtime === undefined) {
                equal = false;
            }
            else if (+entryA.mtime === +entryB.mtime) {
                equal = true;
            }
            else {
                equal = false;
            }
        }
        else {
            equal = false;
        }
        if (equal === false) {
            logger.info('invalidation reason: \nbefore %o\n entryB %o', entryA, entryB);
        }
        return equal;
    };
    FSTree.applyPatch = function (input, output, patch, _delegate) {
        var delegate = __assign({}, DEFAULT_DELEGATE, _delegate);
        for (var i = 0; i < patch.length; i++) {
            applyOperation(input, output, patch[i], delegate);
        }
    };
    return FSTree;
}());
function applyOperation(input, output, operation, delegate) {
    var methodName = operation[0];
    var relativePath = operation[1];
    var inputPath = path.join(input, relativePath);
    var outputPath = path.join(output, relativePath);
    var method = delegate[methodName];
    if (typeof method === 'function') {
        method(inputPath, outputPath, relativePath);
    }
    else {
        throw new Error('Unable to apply patch operation: ' + methodName + '. The value of delegate.' + methodName + ' is of type ' + typeof method + ', and not a function. Check the `delegate` argument to `FSTree.prototype.applyPatch`.');
    }
}
function addOperation(entry) {
    return [
        entry.isDirectory() ? 'mkdir' : 'create',
        entry.relativePath,
        entry
    ];
}
function removeOperation(entry) {
    return [
        entry.isDirectory() ? 'rmdir' : 'unlink',
        entry.relativePath,
        entry
    ];
}
function updateOperation(entry) {
    return [
        'change',
        entry.relativePath,
        entry
    ];
}
;
module.exports = FSTree;