File size: 3,298 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var entry_1 = require("./entry");
function validateSortedUnique(entries) {
    for (var i = 1; i < entries.length; i++) {
        var previous = entries[i - 1].relativePath;
        var current = entries[i].relativePath;
        if (previous < current) {
            continue;
        }
        else {
            throw new Error('expected entries[' + (i - 1) + ']: `' + previous +
                '` to be < entries[' + i + ']: `' + current + '`, but was not. Ensure your input is sorted and has no duplicate paths');
        }
    }
}
exports.validateSortedUnique = validateSortedUnique;
function commonPrefix(a, b, term) {
    var max = Math.min(a.length, b.length);
    var end = -1;
    for (var i = 0; i < max; ++i) {
        if (a[i] !== b[i]) {
            break;
        }
        else if (a[i] === term) {
            end = i;
        }
    }
    return a.substr(0, end + 1);
}
exports.commonPrefix = commonPrefix;
function basename(entry) {
    var path = entry.relativePath;
    var end = path.length - 2;
    for (var i = end; i >= 0; --i) {
        if (path[i] === '/') {
            return path.substr(0, i + 1);
        }
    }
    return '';
}
exports.basename = basename;
function computeImpliedEntries(basePath, relativePath) {
    var rv = [];
    for (var i = 0; i < relativePath.length; ++i) {
        if (relativePath[i] === '/') {
            var path = basePath + relativePath.substr(0, i + 1);
            rv.push(new entry_1.default(path, 0, 0));
        }
    }
    return rv;
}
exports.computeImpliedEntries = computeImpliedEntries;
function compareByRelativePath(entryA, entryB) {
    var pathA = entryA.relativePath;
    var pathB = entryB.relativePath;
    if (pathA < pathB) {
        return -1;
    }
    else if (pathA > pathB) {
        return 1;
    }
    return 0;
}
exports.compareByRelativePath = compareByRelativePath;
function sortAndExpand(entries) {
    entries.sort(compareByRelativePath);
    var path = '';
    for (var i = 0; i < entries.length; ++i) {
        var entry = entries[i];
        // update our path eg
        //    path = a/b/c/d/
        //    entry = a/b/q/r/s/
        //    path' = a/b/
        path = commonPrefix(path, entry.relativePath, '/');
        // a/b/ -> a/
        // a/b  -> a/
        var base = basename(entry);
        // base - path
        var entryBaseSansCommon = base.substr(path.length);
        // determine what intermediate directories are missing eg
        //    path = a/b/
        //    entryBaseSansCommon = c/d/e/
        //    impliedEntries = [a/b/c/, a/b/c/d/, a/b/c/d/e/]
        var impliedEntries = computeImpliedEntries(path, entryBaseSansCommon);
        // actually add our implied entries to entries
        if (impliedEntries.length > 0) {
            entries.splice.apply(entries, [i, 0].concat(impliedEntries));
            i += impliedEntries.length;
        }
        // update path.  Now that we've created all the intermediate directories, we
        // don't need to recreate them for subsequent entries.
        if (entry.isDirectory()) {
            path = entry.relativePath;
        }
        else {
            path = base;
        }
    }
    return entries;
}
exports.sortAndExpand = sortAndExpand;