Files: efcf9fd06b02fa9bcd28681b4c777224e19702bb / lib / refiner.js
2041 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, otherEntrypoints, 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 | var otherEntrypoints2 = {} |
58 | for (var name in otherEntrypoints) { |
59 | var file = otherEntrypoints[name] |
60 | otherEntrypoints2[name] = substituteDenominator(file, denominator) |
61 | } |
62 | |
63 | return { |
64 | records: newRecords, |
65 | entrypoint: substituteDenominator(entrypoint, denominator), |
66 | otherEntrypoints: otherEntrypoints2 |
67 | }; |
68 | } |
69 | |
70 | export default function (records, entrypoint, otherEntrypoints) { |
71 | purgeTopDirectories(records); |
72 | const denominator = retrieveDenominator(Object.keys(records)); |
73 | return denominate(records, entrypoint, otherEntrypoints, denominator); |
74 | } |
75 |
Built with git-ssb-web