Files: e1a1630f31c55420ca08a2a8e36296e3c202f1b1 / prelude / common.js
5729 bytesRaw
1 | ; |
2 | |
3 | var assert = require('assert'); |
4 | var path = require('path'); |
5 | |
6 | exports.STORE_BLOB = 0; |
7 | exports.STORE_CONTENT = 1; |
8 | exports.STORE_LINKS = 2; |
9 | exports.STORE_STAT = 3; |
10 | exports.ALIAS_AS_RELATIVE = 0; // require("./file.js") // file or directory |
11 | exports.ALIAS_AS_RESOLVABLE = 1; // require("package") |
12 | |
13 | var win32 = process.platform === 'win32'; |
14 | var hasURL = typeof URL !== 'undefined'; |
15 | |
16 | function uppercaseDriveLetter (f) { |
17 | if (f.slice(1, 3) !== ':\\') return f; |
18 | return f[0].toUpperCase() + f.slice(1); |
19 | } |
20 | |
21 | function removeTrailingSlashes (f) { |
22 | if (f === '/') { |
23 | return f; // dont remove from "/" |
24 | } |
25 | if (f.slice(1) === ':\\') { |
26 | return f; // dont remove from "D:\" |
27 | } |
28 | var last = f.length - 1; |
29 | while (true) { |
30 | var char = f.charAt(last); |
31 | if (char === '\\') { |
32 | f = f.slice(0, -1); |
33 | last -= 1; |
34 | } else |
35 | if (char === '/') { |
36 | f = f.slice(0, -1); |
37 | last -= 1; |
38 | } else { |
39 | break; |
40 | } |
41 | } |
42 | return f; |
43 | } |
44 | |
45 | function isRootPath (p) { |
46 | if (Buffer.isBuffer(p)) p = p.toString(); |
47 | if (hasURL && p instanceof URL) p = p.pathname; |
48 | if (p === '.') p = path.resolve(p); |
49 | return path.dirname(p) === p; |
50 | } |
51 | |
52 | exports.isRootPath = isRootPath; |
53 | |
54 | var normalizePath; |
55 | |
56 | if (win32) { |
57 | normalizePath = function (f) { |
58 | var file = f; |
59 | if (Buffer.isBuffer(file)) file = file.toString(); |
60 | if (hasURL && file instanceof URL) file = file.pathname.replace(/^\//, ''); |
61 | if (!(/^.:$/.test(file))) file = path.normalize(file); // 'c:' -> 'c:.' |
62 | file = uppercaseDriveLetter(file); |
63 | file = removeTrailingSlashes(file); |
64 | return file; |
65 | }; |
66 | } else { |
67 | normalizePath = function (f) { |
68 | var file = f; |
69 | if (Buffer.isBuffer(file)) file = file.toString(); |
70 | if (hasURL && file instanceof URL) file = file.pathname; |
71 | if (!(/^.:$/.test(file))) file = path.normalize(file); // 'c:' -> 'c:.' |
72 | file = removeTrailingSlashes(file); |
73 | return file; |
74 | }; |
75 | } |
76 | |
77 | exports.normalizePath = normalizePath; |
78 | |
79 | exports.isPackageJson = function (file) { |
80 | return path.basename(file) === 'package.json'; |
81 | }; |
82 | |
83 | exports.isDotJS = function (file) { |
84 | return path.extname(file) === '.js'; |
85 | }; |
86 | |
87 | exports.isDotJSON = function (file) { |
88 | return path.extname(file) === '.json'; |
89 | }; |
90 | |
91 | exports.isDotNODE = function (file) { |
92 | return path.extname(file) === '.node'; |
93 | }; |
94 | |
95 | function replaceSlashes (file, slash) { |
96 | if (/^.:\\/.test(file)) { |
97 | if (slash === '/') { |
98 | return file.slice(2).replace(/\\/g, '/'); |
99 | } |
100 | } else |
101 | if (/^\//.test(file)) { |
102 | if (slash === '\\') { |
103 | return 'C:' + file.replace(/\//g, '\\'); |
104 | } |
105 | } |
106 | return file; |
107 | } |
108 | |
109 | function injectSnapshot (file) { |
110 | if (/^.:\\/.test(file)) { |
111 | // C:\path\to |
112 | if (file.length === 3) file = file.slice(0, -1); // C:\ |
113 | return file[0] + ':\\snapshot' + file.slice(2); |
114 | } else |
115 | if (/^\//.test(file)) { |
116 | // /home/user/project |
117 | if (file.length === 1) file = file.slice(0, -1); // / |
118 | return '/snapshot' + file; |
119 | } |
120 | return file; |
121 | } |
122 | |
123 | function longestCommonLength (s1, s2) { |
124 | var length = Math.min(s1.length, s2.length); |
125 | for (var i = 0; i < length; i += 1) { |
126 | if (s1.charCodeAt(i) !== s2.charCodeAt(i)) { |
127 | return i; |
128 | } |
129 | } |
130 | return length; |
131 | } |
132 | |
133 | function withoutNodeModules (file) { |
134 | return file.split(path.sep + 'node_modules' + path.sep)[0]; |
135 | } |
136 | |
137 | exports.retrieveDenominator = function (files) { |
138 | assert(files.length > 0); |
139 | |
140 | var s1 = withoutNodeModules(files[0]) + path.sep; |
141 | for (var i = 1; i < files.length; i += 1) { |
142 | var s2 = withoutNodeModules(files[i]) + path.sep; |
143 | s1 = s1.slice(0, longestCommonLength(s1, s2)); |
144 | } |
145 | |
146 | if (s1 === '') return win32 ? 2 : 0; |
147 | return s1.lastIndexOf(path.sep); |
148 | }; |
149 | |
150 | exports.substituteDenominator = function (f, denominator) { |
151 | var rootLength = win32 ? 2 : 0; |
152 | return f.slice(0, rootLength) + f.slice(denominator); |
153 | }; |
154 | |
155 | exports.snapshotify = function (file, slash) { |
156 | assert.equal(file, normalizePath(file)); |
157 | return injectSnapshot(replaceSlashes(file, slash)); |
158 | }; |
159 | |
160 | if (win32) { |
161 | exports.insideSnapshot = function insideSnapshot (f) { |
162 | if (Buffer.isBuffer(f)) f = f.toString(); |
163 | if (hasURL && f instanceof URL) f = f.pathname.replace(/^\//, ''); |
164 | if (typeof f !== 'string') return false; |
165 | var slice112 = f.slice(1, 12); |
166 | if (slice112 === ':\\snapshot\\' || |
167 | slice112 === ':/snapshot\\' || |
168 | slice112 === ':\\snapshot/' || |
169 | slice112 === ':/snapshot/' || |
170 | slice112 === ':\\snapshot' || |
171 | slice112 === ':/snapshot') return true; |
172 | return false; |
173 | }; |
174 | } else { |
175 | exports.insideSnapshot = function insideSnapshot (f) { |
176 | if (Buffer.isBuffer(f)) f = f.toString(); |
177 | if (hasURL && f instanceof URL) f = f.pathname; |
178 | if (typeof f !== 'string') return false; |
179 | var slice010 = f.slice(0, 10); |
180 | if (slice010 === '/snapshot/' || |
181 | slice010 === '/snapshot') return true; |
182 | return false; |
183 | }; |
184 | } |
185 | |
186 | exports.stripSnapshot = function (f) { |
187 | var file = normalizePath(f); |
188 | if (/^.:\\snapshot$/.test(file)) { |
189 | return file[0] + ':\\**\\'; |
190 | } |
191 | if (/^.:\\snapshot\\/.test(file)) { |
192 | return file[0] + ':\\**' + file.slice(11); |
193 | } |
194 | if (/^\/snapshot$/.test(file)) { |
195 | return '/**/'; |
196 | } |
197 | if (/^\/snapshot\//.test(file)) { |
198 | return '/**' + file.slice(9); |
199 | } |
200 | return f; // not inside |
201 | }; |
202 | |
203 | if (win32) { |
204 | exports.removeUplevels = function removeUplevels (f) { |
205 | while (true) { |
206 | if (f.slice(0, 3) === '..\\') { |
207 | f = f.slice(3); |
208 | } else |
209 | if (f === '..') { |
210 | f = '.'; |
211 | } else { |
212 | break; |
213 | } |
214 | } |
215 | return f; |
216 | }; |
217 | } else { |
218 | exports.removeUplevels = function removeUplevels (f) { |
219 | while (true) { |
220 | if (f.slice(0, 3) === '../') { |
221 | f = f.slice(3); |
222 | } else |
223 | if (f === '..') { |
224 | f = '.'; |
225 | } else { |
226 | break; |
227 | } |
228 | } |
229 | return f; |
230 | }; |
231 | } |
232 |
Built with git-ssb-web