Files: 65191f9a9664f013ebe726bb95a1a54b086e1bd5 / lib / refiner.js
1764 bytesRaw
1 | import { STORE_LINKS, retrieveDenominator, |
2 | substituteDenominator } from '../prelude/common.js'; |
3 | import path from 'path'; |
4 | |
5 | const win32 = process.platform === 'win32'; |
6 | |
7 | function hasParent (file, records) { |
8 | const dirname = path.dirname(file); |
9 | if (dirname === file) return false; // root directory |
10 | return Boolean(records[dirname]); |
11 | } |
12 | |
13 | function purgeTopDirectories (records) { |
14 | while (true) { |
15 | let found = false; |
16 | |
17 | for (const file in records) { |
18 | const record = records[file]; |
19 | const links = record[STORE_LINKS]; |
20 | if (links && links.length === 1) { |
21 | if (!hasParent(file, records)) { |
22 | const file2 = path.join(file, links[0]); |
23 | const record2 = records[file2]; |
24 | const links2 = record2[STORE_LINKS]; |
25 | if (links2 && links2.length === 1) { |
26 | const file3 = path.join(file2, links2[0]); |
27 | const record3 = records[file3]; |
28 | const links3 = record3[STORE_LINKS]; |
29 | if (links3) { |
30 | delete records[file]; |
31 | found = true; |
32 | } |
33 | } |
34 | } |
35 | } |
36 | } |
37 | |
38 | if (!found) break; |
39 | } |
40 | } |
41 | |
42 | function denominate (records, entrypoint, denominator) { |
43 | const newRecords = {}; |
44 | |
45 | for (const file in records) { |
46 | let snap = substituteDenominator(file, denominator); |
47 | |
48 | if (win32) { |
49 | if (snap.slice(1) === ':') snap += '\\'; |
50 | } else { |
51 | if (snap === '') snap = '/'; |
52 | } |
53 | |
54 | newRecords[snap] = records[file]; |
55 | } |
56 | |
57 | return { |
58 | records: newRecords, |
59 | entrypoint: substituteDenominator(entrypoint, denominator) |
60 | }; |
61 | } |
62 | |
63 | export default function (records, entrypoint) { |
64 | purgeTopDirectories(records); |
65 | const denominator = retrieveDenominator(Object.keys(records)); |
66 | return denominate(records, entrypoint, denominator); |
67 | } |
68 |
Built with git-ssb-web