UNPKG

3.3 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var entry_1 = require("./entry");
4function 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}
17exports.validateSortedUnique = validateSortedUnique;
18function 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}
31exports.commonPrefix = commonPrefix;
32function 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}
42exports.basename = basename;
43function 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}
53exports.computeImpliedEntries = computeImpliedEntries;
54function 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}
65exports.compareByRelativePath = compareByRelativePath;
66function sortAndExpand(entries) {
67 entries.sort(compareByRelativePath);
68 var path = '';
69 for (var i = 0; i < entries.length; ++i) {
70 var entry = entries[i];
71 // update our path eg
72 // path = a/b/c/d/
73 // entry = a/b/q/r/s/
74 // path' = a/b/
75 path = commonPrefix(path, entry.relativePath, '/');
76 // a/b/ -> a/
77 // a/b -> a/
78 var base = basename(entry);
79 // base - path
80 var entryBaseSansCommon = base.substr(path.length);
81 // determine what intermediate directories are missing eg
82 // path = a/b/
83 // entryBaseSansCommon = c/d/e/
84 // impliedEntries = [a/b/c/, a/b/c/d/, a/b/c/d/e/]
85 var impliedEntries = computeImpliedEntries(path, entryBaseSansCommon);
86 // actually add our implied entries to entries
87 if (impliedEntries.length > 0) {
88 entries.splice.apply(entries, [i, 0].concat(impliedEntries));
89 i += impliedEntries.length;
90 }
91 // update path. Now that we've created all the intermediate directories, we
92 // don't need to recreate them for subsequent entries.
93 if (entry.isDirectory()) {
94 path = entry.relativePath;
95 }
96 else {
97 path = base;
98 }
99 }
100 return entries;
101}
102exports.sortAndExpand = sortAndExpand;