1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var entry_1 = require("./entry");
|
4 | function validateSortedUnique(entries) {
|
5 | for (var i = 1; i < entries.length; i++) {
|
6 | var previous = entries[i - 1].relativePath;
|
7 | var current = entries[i].relativePath;
|
8 | if (previous < current) {
|
9 | continue;
|
10 | }
|
11 | else {
|
12 | throw new Error('expected entries[' + (i - 1) + ']: `' + previous +
|
13 | '` to be < entries[' + i + ']: `' + current + '`, but was not. Ensure your input is sorted and has no duplicate paths');
|
14 | }
|
15 | }
|
16 | }
|
17 | exports.validateSortedUnique = validateSortedUnique;
|
18 | function commonPrefix(a, b, term) {
|
19 | var max = Math.min(a.length, b.length);
|
20 | var end = -1;
|
21 | for (var i = 0; i < max; ++i) {
|
22 | if (a[i] !== b[i]) {
|
23 | break;
|
24 | }
|
25 | else if (a[i] === term) {
|
26 | end = i;
|
27 | }
|
28 | }
|
29 | return a.substr(0, end + 1);
|
30 | }
|
31 | exports.commonPrefix = commonPrefix;
|
32 | function basename(entry) {
|
33 | var path = entry.relativePath;
|
34 | var end = path.length - 2;
|
35 | for (var i = end; i >= 0; --i) {
|
36 | if (path[i] === '/') {
|
37 | return path.substr(0, i + 1);
|
38 | }
|
39 | }
|
40 | return '';
|
41 | }
|
42 | exports.basename = basename;
|
43 | function computeImpliedEntries(basePath, relativePath) {
|
44 | var rv = [];
|
45 | for (var i = 0; i < relativePath.length; ++i) {
|
46 | if (relativePath[i] === '/') {
|
47 | var path = basePath + relativePath.substr(0, i + 1);
|
48 | rv.push(new entry_1.default(path, 0, 0));
|
49 | }
|
50 | }
|
51 | return rv;
|
52 | }
|
53 | exports.computeImpliedEntries = computeImpliedEntries;
|
54 | function compareByRelativePath(entryA, entryB) {
|
55 | var pathA = entryA.relativePath;
|
56 | var pathB = entryB.relativePath;
|
57 | if (pathA < pathB) {
|
58 | return -1;
|
59 | }
|
60 | else if (pathA > pathB) {
|
61 | return 1;
|
62 | }
|
63 | return 0;
|
64 | }
|
65 | exports.compareByRelativePath = compareByRelativePath;
|
66 | function sortAndExpand(entries) {
|
67 | entries.sort(compareByRelativePath);
|
68 | var path = '';
|
69 | for (var i = 0; i < entries.length; ++i) {
|
70 | var entry = entries[i];
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | path = commonPrefix(path, entry.relativePath, '/');
|
76 |
|
77 |
|
78 | var base = basename(entry);
|
79 |
|
80 | var entryBaseSansCommon = base.substr(path.length);
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | var impliedEntries = computeImpliedEntries(path, entryBaseSansCommon);
|
86 |
|
87 | if (impliedEntries.length > 0) {
|
88 | entries.splice.apply(entries, [i, 0].concat(impliedEntries));
|
89 | i += impliedEntries.length;
|
90 | }
|
91 |
|
92 |
|
93 | if (entry.isDirectory()) {
|
94 | path = entry.relativePath;
|
95 | }
|
96 | else {
|
97 | path = base;
|
98 | }
|
99 | }
|
100 | return entries;
|
101 | }
|
102 | exports.sortAndExpand = sortAndExpand;
|