@environment-safe/file 0.0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/pre-commit +5 -3
- package/README.md +25 -7
- package/package.json +19 -5
- package/src/buffer.d.mts +5 -0
- package/src/buffer.mjs +9 -3
- package/src/filesystem.d.mts +47 -0
- package/src/filesystem.mjs +449 -0
- package/src/index.d.mts +27 -0
- package/src/index.mjs +146 -519
- package/src/path.d.mts +35 -0
- package/src/path.mjs +835 -0
- package/test/isolated/crud.mjs +0 -0
- package/test/isolated/path.mjs +48 -0
- package/test/isolated-path.html +448 -0
- package/test/test.html +466 -0
- package/test/test.mjs +78 -22
- /package/test/{index.html → local-file-test.html} +0 -0
package/src/path.mjs
ADDED
|
@@ -0,0 +1,835 @@
|
|
|
1
|
+
/* eslint-disable no-case-declarations */
|
|
2
|
+
import * as os from 'os';
|
|
3
|
+
import {
|
|
4
|
+
isBrowser,
|
|
5
|
+
isJsDom,
|
|
6
|
+
isServer, // is running on a server runtime
|
|
7
|
+
os as osName, // Operating system, machine friendly
|
|
8
|
+
} from '@environment-safe/runtime-context';
|
|
9
|
+
|
|
10
|
+
const knownLocationsMap = {
|
|
11
|
+
darwin : {
|
|
12
|
+
'desktop': '~/Desktop',
|
|
13
|
+
'documents': '~/Documents',
|
|
14
|
+
'downloads': '~/Downloads',
|
|
15
|
+
'music': '~/Music',
|
|
16
|
+
'pictures': '~/Pictures',
|
|
17
|
+
'temporary': '/tmp',
|
|
18
|
+
'videos': '~/Movies',
|
|
19
|
+
'web': '~/Sites',
|
|
20
|
+
'home': '~'
|
|
21
|
+
},
|
|
22
|
+
win : {},
|
|
23
|
+
linux : {},
|
|
24
|
+
};
|
|
25
|
+
knownLocationsMap['mac os x'] = knownLocationsMap.darwin;
|
|
26
|
+
knownLocationsMap['windows'] = knownLocationsMap.win;
|
|
27
|
+
|
|
28
|
+
const canonicalLocationToPath = (location, username)=>{
|
|
29
|
+
const os = osName;
|
|
30
|
+
return (
|
|
31
|
+
(knownLocationsMap[os] && knownLocationsMap[os][location]) || location
|
|
32
|
+
).replace('~', osToHome[os].replace('${user}', username));
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const osToHome = {
|
|
36
|
+
darwin : '/Users/${user}',
|
|
37
|
+
win : 'C:\\Users\\${user}',
|
|
38
|
+
linux : '/Users/${user}',
|
|
39
|
+
};
|
|
40
|
+
osToHome['mac os x'] = osToHome.darwin;
|
|
41
|
+
osToHome['windows'] = osToHome.win;
|
|
42
|
+
|
|
43
|
+
const join = (osName, ...parts)=>{
|
|
44
|
+
if(osName === 'windows') return parts.join('\\');
|
|
45
|
+
return parts.join('/');
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const formatWindows = (parsed)=>{
|
|
49
|
+
return `${parsed.dir}\\${parsed.name}`;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
//todo: audit the posix code
|
|
53
|
+
const windowsRelative = (from, to)=>{
|
|
54
|
+
if (from === to) return '';
|
|
55
|
+
const fromDrive = from.substring(0,1);
|
|
56
|
+
const toDrive = to.substring(0,1);
|
|
57
|
+
const fromPath = from.substring(2);
|
|
58
|
+
const toPath = to.substring(2);
|
|
59
|
+
if(fromDrive !== toDrive) return from;
|
|
60
|
+
let pos = 0;
|
|
61
|
+
let commonPath = '';
|
|
62
|
+
let pathSeparator = '\\';
|
|
63
|
+
if(fromPath[pos] === pathSeparator || (fromPath[pos] === null && toPath[pos] === pathSeparator)){
|
|
64
|
+
commonPath = fromPath.substring(0, pos);
|
|
65
|
+
}
|
|
66
|
+
while(fromPath[pos] === toPath[pos]){
|
|
67
|
+
pos++;
|
|
68
|
+
if(fromPath[pos] === pathSeparator || ((!fromPath[pos]) && toPath[pos] === pathSeparator)){
|
|
69
|
+
commonPath = fromPath.substring(0, pos);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if(!commonPath) return from;
|
|
73
|
+
const remainingFrom = fromPath.substring(commonPath.length);
|
|
74
|
+
const remainingTo = toPath.substring(commonPath.length);
|
|
75
|
+
const fromParts = remainingFrom.split(pathSeparator);
|
|
76
|
+
const toParts = remainingTo.split(pathSeparator);
|
|
77
|
+
if(fromParts[0] === '' && toParts[0] === ''){
|
|
78
|
+
fromParts.shift();
|
|
79
|
+
toParts.shift();
|
|
80
|
+
}
|
|
81
|
+
const ascendToCommonDirParts = fromParts.map(part=>'..');
|
|
82
|
+
return ascendToCommonDirParts.concat(toParts).join(pathSeparator);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/*
|
|
86
|
+
const pathRelativeTo = (target, relativeToPath, separator='/')=>{
|
|
87
|
+
let len = 0;
|
|
88
|
+
while(target.substring(0, len+1) === relativeToPath.substring(0, len+1)) len++;
|
|
89
|
+
let result = target.substring(len-1);
|
|
90
|
+
let parts = relativeToPath.substring(len).split(separator);
|
|
91
|
+
for (let lcv=0; lcv < parts.length; lcv++){
|
|
92
|
+
result = '..'+separator+result;
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
};
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
export class Path{
|
|
101
|
+
|
|
102
|
+
static browserLocations = [
|
|
103
|
+
'documents', 'desktop', 'downloads', 'music', 'pictures', 'videos'
|
|
104
|
+
];
|
|
105
|
+
|
|
106
|
+
static from(str){
|
|
107
|
+
return new Path(str);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static location(name){
|
|
111
|
+
if(name === 'home' && isServer) return os.homedir();
|
|
112
|
+
return canonicalLocationToPath(name, Path.user);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static isLocation(path){
|
|
116
|
+
let found = false;
|
|
117
|
+
Object.keys(knownLocationsMap[osName]).forEach((name)=>{
|
|
118
|
+
if(found) return;
|
|
119
|
+
const testPath = canonicalLocationToPath(name, Path.user);
|
|
120
|
+
if(Path.within(path, testPath)){
|
|
121
|
+
found = {
|
|
122
|
+
location : name,
|
|
123
|
+
locationPath : testPath,
|
|
124
|
+
remainingPath: path.substring(testPath.length)
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return found;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static within(path, subpath){
|
|
132
|
+
return path.indexOf(subpath) === 0;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
//only supports absolute URLs
|
|
136
|
+
static relative(to, from){
|
|
137
|
+
if((to.indexOf(':\\')) === 1){
|
|
138
|
+
return windowsRelative(to, from);
|
|
139
|
+
}else{
|
|
140
|
+
return posix.relative(to, from || Path.current);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
static join(...parts){
|
|
145
|
+
return join(osName, ...parts);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
constructor(url){
|
|
149
|
+
const result = {};
|
|
150
|
+
if((url.indexOf(':\\')) === 1){
|
|
151
|
+
const drive = url.substring(0, 1);
|
|
152
|
+
const path = url.substring(2).split('\\');
|
|
153
|
+
result.type = 'absolute';
|
|
154
|
+
const name = path.pop();
|
|
155
|
+
result.windows = {
|
|
156
|
+
name,
|
|
157
|
+
root: `${drive}:\\`,
|
|
158
|
+
drive,
|
|
159
|
+
ext: '',
|
|
160
|
+
dir: join('windows', `${drive}:${path.join('\\')}`),
|
|
161
|
+
base: name.split('.').shift()
|
|
162
|
+
};
|
|
163
|
+
}else{
|
|
164
|
+
if((url.indexOf('file://')) === 0){
|
|
165
|
+
result.posix = posix.parse(url.substring(7));
|
|
166
|
+
if(result.posix.dir && result.posix.dir[0] === '!'){
|
|
167
|
+
const target = result.posix.dir.substring(1);
|
|
168
|
+
result.posix.original = result.posix.dir;
|
|
169
|
+
result.posix.known = target.toLowerCase();
|
|
170
|
+
result.posix.dir = this.location(target.toLowerCase());
|
|
171
|
+
}
|
|
172
|
+
}else{
|
|
173
|
+
if((url.indexOf('://')) !== -1){
|
|
174
|
+
result.url = new URL(url);
|
|
175
|
+
}else{
|
|
176
|
+
if(url[0] === '.') result.type = 'relative';
|
|
177
|
+
else result.type = 'absolute';
|
|
178
|
+
result.posix = posix.parse(url);
|
|
179
|
+
if(result.posix.dir && result.posix.dir[0] === '!'){
|
|
180
|
+
const target = result.posix.dir.substring(1);
|
|
181
|
+
result.posix.original = result.posix.dir;
|
|
182
|
+
result.posix.known = target.toLowerCase();
|
|
183
|
+
result.posix.dir = this.location(target.toLowerCase());
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
this.parsed = result;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
toUrl(type, relative){
|
|
192
|
+
switch(type){
|
|
193
|
+
case 'https:':
|
|
194
|
+
case 'http:':
|
|
195
|
+
//todo: simple mode that doesn't compute full URLs
|
|
196
|
+
const url = this.toUrl('native');
|
|
197
|
+
if(relative || url[0]=='.'){
|
|
198
|
+
const relativeTo = typeof relative === 'string'?relative:Path.current;
|
|
199
|
+
return `${Path.relative(relativeTo, url)}`;
|
|
200
|
+
}
|
|
201
|
+
//todo: support not having the current dir (pure web mode)
|
|
202
|
+
if(!Path.within(Path.current, url)){
|
|
203
|
+
//uh oh, looks like we're outside the web root
|
|
204
|
+
let location = null;
|
|
205
|
+
// eslint-disable-next-line no-cond-assign
|
|
206
|
+
if(location = Path.isLocation(url)){
|
|
207
|
+
const result = `::${location.location}/${location.remainingPath}`;
|
|
208
|
+
result.parsed = location;
|
|
209
|
+
return result;
|
|
210
|
+
}else{
|
|
211
|
+
throw new Error('Path is outside of addressable locations');
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const relativePath = Path.relative(Path.current, url);
|
|
215
|
+
const prefix = relativePath[0]=='.'?'':type+'//';
|
|
216
|
+
const result = `${prefix}${relativePath}`;
|
|
217
|
+
return result;
|
|
218
|
+
case 'file:':
|
|
219
|
+
let res = null;
|
|
220
|
+
if(relative){
|
|
221
|
+
res = `${ Path.relative(Path.current, this.toUrl('native')).replace(/\\/g, '/') }`;
|
|
222
|
+
}
|
|
223
|
+
res = `file://${this.toUrl('native')}`;
|
|
224
|
+
return res;
|
|
225
|
+
case 'native':
|
|
226
|
+
switch(osName){
|
|
227
|
+
case 'darwin':
|
|
228
|
+
case 'mac os x':
|
|
229
|
+
case 'linux':
|
|
230
|
+
return this.toUrl('posix', relative);
|
|
231
|
+
case 'win':
|
|
232
|
+
case 'windows':
|
|
233
|
+
return this.toUrl('windows', relative);
|
|
234
|
+
default: throw new Error('unsupported OS'+osName);
|
|
235
|
+
}
|
|
236
|
+
case 'windows':
|
|
237
|
+
if(this.parsed.windows){
|
|
238
|
+
const windowsPath = `${this.parsed.windows.dir}\\${this.parsed.windows.name}`;
|
|
239
|
+
if(relative){
|
|
240
|
+
const relativeTo = typeof relative === 'string'?relative:Path.current;
|
|
241
|
+
return `${Path.relative(relativeTo, windowsPath)}`;
|
|
242
|
+
}
|
|
243
|
+
return windowsPath;
|
|
244
|
+
}
|
|
245
|
+
return '';
|
|
246
|
+
case 'posix':
|
|
247
|
+
if(this.parsed.posix){
|
|
248
|
+
const posixPath = posix.format(this.parsed.posix);
|
|
249
|
+
if(relative){
|
|
250
|
+
const relativeTo = typeof relative === 'string'?relative:Path.current;
|
|
251
|
+
return `${Path.relative(relativeTo, posixPath)}`;
|
|
252
|
+
}
|
|
253
|
+
return posixPath;
|
|
254
|
+
}
|
|
255
|
+
if(this.parsed.windows){
|
|
256
|
+
let windowsPath = formatWindows(this.parsed.windows);
|
|
257
|
+
if(relative){
|
|
258
|
+
const relativeTo = typeof relative === 'string'?relative:Path.current;
|
|
259
|
+
windowsPath = `${windowsRelative(relativeTo, windowsPath)}`;
|
|
260
|
+
}
|
|
261
|
+
windowsPath = windowsPath.replace(/[A-Z]:/g, '');
|
|
262
|
+
const posixPath = windowsPath;
|
|
263
|
+
return posixPath;
|
|
264
|
+
}
|
|
265
|
+
//it was parsed as a url
|
|
266
|
+
return '';
|
|
267
|
+
//anything else must be a relative path or url
|
|
268
|
+
default:
|
|
269
|
+
|
|
270
|
+
/*if(this.parsed.posix){
|
|
271
|
+
let posixFormat = this.parsed.posix || {};
|
|
272
|
+
return posix.relative(type, posix.format(posixFormat));
|
|
273
|
+
}
|
|
274
|
+
if(this.parsed.windows){
|
|
275
|
+
let windowsFormat = this.parsed.windows || {};
|
|
276
|
+
return windowsRelative(type, formatWindows(windowsFormat));
|
|
277
|
+
}*/
|
|
278
|
+
if(this.parsed.url){
|
|
279
|
+
return this.parsed.url.toString();
|
|
280
|
+
}
|
|
281
|
+
return this.toUrl('native', true);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
toString(){
|
|
286
|
+
return this.toUrl('native', Path.current);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
let currentPath = null;
|
|
290
|
+
let initialPath = null;
|
|
291
|
+
Object.defineProperty(Path, 'current', {
|
|
292
|
+
get() {
|
|
293
|
+
if(!currentPath){
|
|
294
|
+
if(isBrowser || isJsDom){
|
|
295
|
+
const base = document.getElementsByTagName('base')[0];
|
|
296
|
+
let basedir = null;
|
|
297
|
+
if(base && (basedir = base.getAttribute('href') && basedir.indexof('file://') !== -1)){
|
|
298
|
+
currentPath = basedir;
|
|
299
|
+
}else{
|
|
300
|
+
if(base && (basedir = base.getAttribute('filesystem'))){
|
|
301
|
+
currentPath = basedir;
|
|
302
|
+
}else{
|
|
303
|
+
let path = window.location.pathname;
|
|
304
|
+
path = path.split('/');
|
|
305
|
+
path.pop(); // drop the top one
|
|
306
|
+
currentPath = path .join('/');
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}else{
|
|
310
|
+
currentPath = process.cwd();
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if(!initialPath) initialPath = currentPath;
|
|
314
|
+
return currentPath;
|
|
315
|
+
},
|
|
316
|
+
set(newValue) {
|
|
317
|
+
currentPath = newValue;
|
|
318
|
+
},
|
|
319
|
+
enumerable: true,
|
|
320
|
+
configurable: true,
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
Object.defineProperty(Path, 'user', {
|
|
324
|
+
get() {
|
|
325
|
+
if(isBrowser || isJsDom){
|
|
326
|
+
const base = document.getElementsByTagName('base')[0];
|
|
327
|
+
let user = null;
|
|
328
|
+
if(base && (user = base.getAttribute('user'))){
|
|
329
|
+
return user;
|
|
330
|
+
}
|
|
331
|
+
return user;
|
|
332
|
+
}else{
|
|
333
|
+
return os.userInfo().username;
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
set(newValue) {
|
|
337
|
+
//do nothing
|
|
338
|
+
},
|
|
339
|
+
enumerable: true,
|
|
340
|
+
configurable: true,
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
function assertPath(path) {
|
|
344
|
+
if(typeof path !== 'string'){
|
|
345
|
+
throw new TypeError('Path must be a string. Received ' + JSON.stringify(path));
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Resolves . and .. elements in a path with directory names
|
|
350
|
+
function normalizeStringPosix(path, allowAboveRoot) {
|
|
351
|
+
var res = '';
|
|
352
|
+
var lastSegmentLength = 0;
|
|
353
|
+
var lastSlash = -1;
|
|
354
|
+
var dots = 0;
|
|
355
|
+
var code;
|
|
356
|
+
for (var i = 0; i <= path.length; ++i) {
|
|
357
|
+
if (i < path.length)
|
|
358
|
+
code = path.charCodeAt(i);
|
|
359
|
+
else if (code === 47 /*/*/)
|
|
360
|
+
break;
|
|
361
|
+
else
|
|
362
|
+
code = 47 /*/*/;
|
|
363
|
+
if (code === 47 /*/*/) {
|
|
364
|
+
if (lastSlash === i - 1 || dots === 1) {
|
|
365
|
+
// NOOP
|
|
366
|
+
} else if (lastSlash !== i - 1 && dots === 2) {
|
|
367
|
+
if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 /*.*/ || res.charCodeAt(res.length - 2) !== 46 /*.*/) {
|
|
368
|
+
if (res.length > 2) {
|
|
369
|
+
var lastSlashIndex = res.lastIndexOf('/');
|
|
370
|
+
if (lastSlashIndex !== res.length - 1) {
|
|
371
|
+
if (lastSlashIndex === -1) {
|
|
372
|
+
res = '';
|
|
373
|
+
lastSegmentLength = 0;
|
|
374
|
+
} else {
|
|
375
|
+
res = res.slice(0, lastSlashIndex);
|
|
376
|
+
lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
|
|
377
|
+
}
|
|
378
|
+
lastSlash = i;
|
|
379
|
+
dots = 0;
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
} else if (res.length === 2 || res.length === 1) {
|
|
383
|
+
res = '';
|
|
384
|
+
lastSegmentLength = 0;
|
|
385
|
+
lastSlash = i;
|
|
386
|
+
dots = 0;
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
if (allowAboveRoot) {
|
|
391
|
+
if (res.length > 0)
|
|
392
|
+
res += '/..';
|
|
393
|
+
else
|
|
394
|
+
res = '..';
|
|
395
|
+
lastSegmentLength = 2;
|
|
396
|
+
}
|
|
397
|
+
}else{
|
|
398
|
+
if(res.length > 0)
|
|
399
|
+
res += '/' + path.slice(lastSlash + 1, i);
|
|
400
|
+
else
|
|
401
|
+
res = path.slice(lastSlash + 1, i);
|
|
402
|
+
lastSegmentLength = i - lastSlash - 1;
|
|
403
|
+
}
|
|
404
|
+
lastSlash = i;
|
|
405
|
+
dots = 0;
|
|
406
|
+
}else if (code === 46 /*.*/ && dots !== -1){
|
|
407
|
+
++dots;
|
|
408
|
+
}else{
|
|
409
|
+
dots = -1;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return res;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function _format(sep, pathObject) {
|
|
416
|
+
var dir = pathObject.dir || pathObject.root;
|
|
417
|
+
var base = pathObject.base || (pathObject.name || '') + (pathObject.ext || '');
|
|
418
|
+
if (!dir) {
|
|
419
|
+
return base;
|
|
420
|
+
}
|
|
421
|
+
if (dir === pathObject.root) {
|
|
422
|
+
return dir + base;
|
|
423
|
+
}
|
|
424
|
+
return dir + sep + base;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
var posix = {
|
|
428
|
+
// path.resolve([from ...], to)
|
|
429
|
+
resolve: function resolve() {
|
|
430
|
+
var resolvedPath = '';
|
|
431
|
+
var resolvedAbsolute = false;
|
|
432
|
+
var cwd;
|
|
433
|
+
|
|
434
|
+
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
|
435
|
+
var path;
|
|
436
|
+
if (i >= 0)
|
|
437
|
+
path = arguments[i];
|
|
438
|
+
else {
|
|
439
|
+
if(cwd === undefined) cwd = Path.current;
|
|
440
|
+
path = cwd;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
assertPath(path);
|
|
444
|
+
|
|
445
|
+
// Skip empty entries
|
|
446
|
+
if (path.length === 0) {
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
resolvedPath = path + '/' + resolvedPath;
|
|
451
|
+
resolvedAbsolute = path.charCodeAt(0) === 47 /*/*/;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// At this point the path should be resolved to a full absolute path, but
|
|
455
|
+
// handle relative paths to be safe (might happen when process.cwd() fails)
|
|
456
|
+
|
|
457
|
+
// Normalize the path
|
|
458
|
+
resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute);
|
|
459
|
+
|
|
460
|
+
if(resolvedAbsolute){
|
|
461
|
+
if(resolvedPath.length > 0)
|
|
462
|
+
return '/' + resolvedPath;
|
|
463
|
+
else
|
|
464
|
+
return '/';
|
|
465
|
+
}else if (resolvedPath.length > 0){
|
|
466
|
+
return resolvedPath;
|
|
467
|
+
}else{
|
|
468
|
+
return '.';
|
|
469
|
+
}
|
|
470
|
+
},
|
|
471
|
+
|
|
472
|
+
normalize: function normalize(path) {
|
|
473
|
+
assertPath(path);
|
|
474
|
+
|
|
475
|
+
if (path.length === 0) return '.';
|
|
476
|
+
|
|
477
|
+
var isAbsolute = path.charCodeAt(0) === 47 /*/*/;
|
|
478
|
+
var trailingSeparator = path.charCodeAt(path.length - 1) === 47 /*/*/;
|
|
479
|
+
|
|
480
|
+
// Normalize the path
|
|
481
|
+
path = normalizeStringPosix(path, !isAbsolute);
|
|
482
|
+
|
|
483
|
+
if (path.length === 0 && !isAbsolute) path = '.';
|
|
484
|
+
if (path.length > 0 && trailingSeparator) path += '/';
|
|
485
|
+
|
|
486
|
+
if (isAbsolute) return '/' + path;
|
|
487
|
+
return path;
|
|
488
|
+
},
|
|
489
|
+
|
|
490
|
+
isAbsolute: function isAbsolute(path) {
|
|
491
|
+
assertPath(path);
|
|
492
|
+
return path.length > 0 && path.charCodeAt(0) === 47 /*/*/;
|
|
493
|
+
},
|
|
494
|
+
|
|
495
|
+
join: function join() {
|
|
496
|
+
if (arguments.length === 0)
|
|
497
|
+
return '.';
|
|
498
|
+
var joined;
|
|
499
|
+
for (var i = 0; i < arguments.length; ++i) {
|
|
500
|
+
var arg = arguments[i];
|
|
501
|
+
assertPath(arg);
|
|
502
|
+
if (arg.length > 0) {
|
|
503
|
+
if (joined === undefined)
|
|
504
|
+
joined = arg;
|
|
505
|
+
else
|
|
506
|
+
joined += '/' + arg;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
if (joined === undefined) return '.';
|
|
510
|
+
return posix.normalize(joined);
|
|
511
|
+
},
|
|
512
|
+
|
|
513
|
+
relative: function relative(from, to) {
|
|
514
|
+
assertPath(from);
|
|
515
|
+
assertPath(to);
|
|
516
|
+
|
|
517
|
+
if (from === to) return '';
|
|
518
|
+
|
|
519
|
+
from = posix.resolve(from);
|
|
520
|
+
to = posix.resolve(to);
|
|
521
|
+
|
|
522
|
+
if (from === to) return '';
|
|
523
|
+
|
|
524
|
+
// Trim any leading backslashes
|
|
525
|
+
var fromStart = 1;
|
|
526
|
+
for (; fromStart < from.length; ++fromStart) {
|
|
527
|
+
if (from.charCodeAt(fromStart) !== 47 /*/*/) break;
|
|
528
|
+
}
|
|
529
|
+
var fromEnd = from.length;
|
|
530
|
+
var fromLen = fromEnd - fromStart;
|
|
531
|
+
|
|
532
|
+
// Trim any leading backslashes
|
|
533
|
+
var toStart = 1;
|
|
534
|
+
for (; toStart < to.length; ++toStart) {
|
|
535
|
+
if (to.charCodeAt(toStart) !== 47 /*/*/) break;
|
|
536
|
+
}
|
|
537
|
+
var toEnd = to.length;
|
|
538
|
+
var toLen = toEnd - toStart;
|
|
539
|
+
|
|
540
|
+
// Compare paths to find the longest common path from root
|
|
541
|
+
var length = fromLen < toLen ? fromLen : toLen;
|
|
542
|
+
var lastCommonSep = -1;
|
|
543
|
+
var i = 0;
|
|
544
|
+
for (; i <= length; ++i) {
|
|
545
|
+
if (i === length) {
|
|
546
|
+
if (toLen > length) {
|
|
547
|
+
if (to.charCodeAt(toStart + i) === 47 /*/*/) {
|
|
548
|
+
// We get here if `from` is the exact base path for `to`.
|
|
549
|
+
// For example: from='/foo/bar'; to='/foo/bar/baz'
|
|
550
|
+
return to.slice(toStart + i + 1);
|
|
551
|
+
} else if (i === 0) {
|
|
552
|
+
// We get here if `from` is the root
|
|
553
|
+
// For example: from='/'; to='/foo'
|
|
554
|
+
return to.slice(toStart + i);
|
|
555
|
+
}
|
|
556
|
+
} else if (fromLen > length) {
|
|
557
|
+
if (from.charCodeAt(fromStart + i) === 47 /*/*/) {
|
|
558
|
+
// We get here if `to` is the exact base path for `from`.
|
|
559
|
+
// For example: from='/foo/bar/baz'; to='/foo/bar'
|
|
560
|
+
lastCommonSep = i;
|
|
561
|
+
} else if (i === 0) {
|
|
562
|
+
// We get here if `to` is the root.
|
|
563
|
+
// For example: from='/foo'; to='/'
|
|
564
|
+
lastCommonSep = 0;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
var fromCode = from.charCodeAt(fromStart + i);
|
|
570
|
+
var toCode = to.charCodeAt(toStart + i);
|
|
571
|
+
if (fromCode !== toCode) break;
|
|
572
|
+
else if (fromCode === 47 /*/*/) lastCommonSep = i;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
var out = '';
|
|
576
|
+
// Generate the relative path based on the path difference between `to`
|
|
577
|
+
// and `from`
|
|
578
|
+
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
|
|
579
|
+
if (i === fromEnd || from.charCodeAt(i) === 47 /*/*/) {
|
|
580
|
+
if (out.length === 0)
|
|
581
|
+
out += '..';
|
|
582
|
+
else
|
|
583
|
+
out += '/..';
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// Lastly, append the rest of the destination (`to`) path that comes after
|
|
588
|
+
// the common path parts
|
|
589
|
+
if (out.length > 0)
|
|
590
|
+
return out + to.slice(toStart + lastCommonSep);
|
|
591
|
+
else {
|
|
592
|
+
toStart += lastCommonSep;
|
|
593
|
+
if (to.charCodeAt(toStart) === 47 /*/*/) ++toStart;
|
|
594
|
+
return to.slice(toStart);
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
|
|
598
|
+
_makeLong: function _makeLong(path) {
|
|
599
|
+
return path;
|
|
600
|
+
},
|
|
601
|
+
|
|
602
|
+
dirname: function dirname(path) {
|
|
603
|
+
assertPath(path);
|
|
604
|
+
if (path.length === 0) return '.';
|
|
605
|
+
var code = path.charCodeAt(0);
|
|
606
|
+
var hasRoot = code === 47 /*/*/;
|
|
607
|
+
var end = -1;
|
|
608
|
+
var matchedSlash = true;
|
|
609
|
+
for(var i = path.length - 1; i >= 1; --i){
|
|
610
|
+
code = path.charCodeAt(i);
|
|
611
|
+
if(code === 47 /*/*/){
|
|
612
|
+
if(!matchedSlash){
|
|
613
|
+
end = i;
|
|
614
|
+
break;
|
|
615
|
+
}
|
|
616
|
+
}else{
|
|
617
|
+
// We saw the first non-path separator
|
|
618
|
+
matchedSlash = false;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
if (end === -1) return hasRoot ? '/' : '.';
|
|
622
|
+
if (hasRoot && end === 1) return '//';
|
|
623
|
+
return path.slice(0, end);
|
|
624
|
+
},
|
|
625
|
+
|
|
626
|
+
basename: function basename(path, ext) {
|
|
627
|
+
if (ext !== undefined && typeof ext !== 'string') throw new TypeError('"ext" argument must be a string');
|
|
628
|
+
assertPath(path);
|
|
629
|
+
|
|
630
|
+
var start = 0;
|
|
631
|
+
var end = -1;
|
|
632
|
+
var matchedSlash = true;
|
|
633
|
+
var i;
|
|
634
|
+
|
|
635
|
+
if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
|
|
636
|
+
if (ext.length === path.length && ext === path) return '';
|
|
637
|
+
var extIdx = ext.length - 1;
|
|
638
|
+
var firstNonSlashEnd = -1;
|
|
639
|
+
for (i = path.length - 1; i >= 0; --i) {
|
|
640
|
+
var code = path.charCodeAt(i);
|
|
641
|
+
if(code === 47 /*/*/){
|
|
642
|
+
// If we reached a path separator that was not part of a set of path
|
|
643
|
+
// separators at the end of the string, stop now
|
|
644
|
+
if (!matchedSlash) {
|
|
645
|
+
start = i + 1;
|
|
646
|
+
break;
|
|
647
|
+
}
|
|
648
|
+
} else {
|
|
649
|
+
if(firstNonSlashEnd === -1){
|
|
650
|
+
// We saw the first non-path separator, remember this index in case
|
|
651
|
+
// we need it if the extension ends up not matching
|
|
652
|
+
matchedSlash = false;
|
|
653
|
+
firstNonSlashEnd = i + 1;
|
|
654
|
+
}
|
|
655
|
+
if(extIdx >= 0){
|
|
656
|
+
// Try to match the explicit extension
|
|
657
|
+
if (code === ext.charCodeAt(extIdx)) {
|
|
658
|
+
if (--extIdx === -1){
|
|
659
|
+
// We matched the extension, so mark this as the end of our path
|
|
660
|
+
// component
|
|
661
|
+
end = i;
|
|
662
|
+
}
|
|
663
|
+
}else{
|
|
664
|
+
// Extension does not match, so our result is the entire path
|
|
665
|
+
// component
|
|
666
|
+
extIdx = -1;
|
|
667
|
+
end = firstNonSlashEnd;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
if (start === end) end = firstNonSlashEnd;else if (end === -1) end = path.length;
|
|
674
|
+
return path.slice(start, end);
|
|
675
|
+
} else {
|
|
676
|
+
for (i = path.length - 1; i >= 0; --i) {
|
|
677
|
+
if (path.charCodeAt(i) === 47 /*/*/) {
|
|
678
|
+
// If we reached a path separator that was not part of a set of path
|
|
679
|
+
// separators at the end of the string, stop now
|
|
680
|
+
if (!matchedSlash) {
|
|
681
|
+
start = i + 1;
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
} else if (end === -1) {
|
|
685
|
+
// We saw the first non-path separator, mark this as the end of our
|
|
686
|
+
// path component
|
|
687
|
+
matchedSlash = false;
|
|
688
|
+
end = i + 1;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
if (end === -1) return '';
|
|
693
|
+
return path.slice(start, end);
|
|
694
|
+
}
|
|
695
|
+
},
|
|
696
|
+
|
|
697
|
+
extname: function extname(path) {
|
|
698
|
+
assertPath(path);
|
|
699
|
+
var startDot = -1;
|
|
700
|
+
var startPart = 0;
|
|
701
|
+
var end = -1;
|
|
702
|
+
var matchedSlash = true;
|
|
703
|
+
// Track the state of characters (if any) we see before our first dot and
|
|
704
|
+
// after any path separator we find
|
|
705
|
+
var preDotState = 0;
|
|
706
|
+
for (var i = path.length - 1; i >= 0; --i) {
|
|
707
|
+
var code = path.charCodeAt(i);
|
|
708
|
+
if (code === 47 /*/*/) {
|
|
709
|
+
// If we reached a path separator that was not part of a set of path
|
|
710
|
+
// separators at the end of the string, stop now
|
|
711
|
+
if (!matchedSlash) {
|
|
712
|
+
startPart = i + 1;
|
|
713
|
+
break;
|
|
714
|
+
}
|
|
715
|
+
continue;
|
|
716
|
+
}
|
|
717
|
+
if (end === -1) {
|
|
718
|
+
// We saw the first non-path separator, mark this as the end of our
|
|
719
|
+
// extension
|
|
720
|
+
matchedSlash = false;
|
|
721
|
+
end = i + 1;
|
|
722
|
+
}
|
|
723
|
+
if(code === 46 /*.*/){
|
|
724
|
+
// If this is our first dot, mark it as the start of our extension
|
|
725
|
+
if (startDot === -1)
|
|
726
|
+
startDot = i;
|
|
727
|
+
else if (preDotState !== 1)
|
|
728
|
+
preDotState = 1;
|
|
729
|
+
}else if (startDot !== -1){
|
|
730
|
+
// We saw a non-dot and non-path separator before our dot, so we should
|
|
731
|
+
// have a good chance at having a non-empty extension
|
|
732
|
+
preDotState = -1;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
if(startDot === -1 || end === -1 ||
|
|
737
|
+
// We saw a non-dot character immediately before the dot
|
|
738
|
+
preDotState === 0 ||
|
|
739
|
+
// The (right-most) trimmed path component is exactly '..'
|
|
740
|
+
preDotState === 1 && startDot === end - 1 && startDot === startPart + 1
|
|
741
|
+
){
|
|
742
|
+
return '';
|
|
743
|
+
}
|
|
744
|
+
return path.slice(startDot, end);
|
|
745
|
+
},
|
|
746
|
+
|
|
747
|
+
format: function format(pathObject) {
|
|
748
|
+
if (pathObject === null || typeof pathObject !== 'object') {
|
|
749
|
+
throw new TypeError('The "pathObject" argument must be of type Object. Received type ' + typeof pathObject);
|
|
750
|
+
}
|
|
751
|
+
return _format('/', pathObject);
|
|
752
|
+
},
|
|
753
|
+
|
|
754
|
+
parse: function parse(path) {
|
|
755
|
+
assertPath(path);
|
|
756
|
+
|
|
757
|
+
var ret = { root: '', dir: '', base: '', ext: '', name: '' };
|
|
758
|
+
if (path.length === 0) return ret;
|
|
759
|
+
var code = path.charCodeAt(0);
|
|
760
|
+
var isAbsolute = code === 47 /*/*/;
|
|
761
|
+
var start;
|
|
762
|
+
if (isAbsolute) {
|
|
763
|
+
ret.root = '/';
|
|
764
|
+
start = 1;
|
|
765
|
+
} else {
|
|
766
|
+
start = 0;
|
|
767
|
+
}
|
|
768
|
+
var startDot = -1;
|
|
769
|
+
var startPart = 0;
|
|
770
|
+
var end = -1;
|
|
771
|
+
var matchedSlash = true;
|
|
772
|
+
var i = path.length - 1;
|
|
773
|
+
|
|
774
|
+
// Track the state of characters (if any) we see before our first dot and
|
|
775
|
+
// after any path separator we find
|
|
776
|
+
var preDotState = 0;
|
|
777
|
+
|
|
778
|
+
// Get non-dir info
|
|
779
|
+
for (; i >= start; --i) {
|
|
780
|
+
code = path.charCodeAt(i);
|
|
781
|
+
if(code === 47 /*/*/){
|
|
782
|
+
// If we reached a path separator that was not part of a set of path
|
|
783
|
+
// separators at the end of the string, stop now
|
|
784
|
+
if(!matchedSlash){
|
|
785
|
+
startPart = i + 1;
|
|
786
|
+
break;
|
|
787
|
+
}
|
|
788
|
+
continue;
|
|
789
|
+
}
|
|
790
|
+
if(end === -1){
|
|
791
|
+
// We saw the first non-path separator, mark this as the end of our
|
|
792
|
+
// extension
|
|
793
|
+
matchedSlash = false;
|
|
794
|
+
end = i + 1;
|
|
795
|
+
}
|
|
796
|
+
if(code === 46 /*.*/){
|
|
797
|
+
// If this is our first dot, mark it as the start of our extension
|
|
798
|
+
if (startDot === -1) startDot = i;else if (preDotState !== 1) preDotState = 1;
|
|
799
|
+
}else if (startDot !== -1){
|
|
800
|
+
// We saw a non-dot and non-path separator before our dot, so we should
|
|
801
|
+
// have a good chance at having a non-empty extension
|
|
802
|
+
preDotState = -1;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
if (startDot === -1 || end === -1 ||
|
|
807
|
+
// We saw a non-dot character immediately before the dot
|
|
808
|
+
preDotState === 0 ||
|
|
809
|
+
// The (right-most) trimmed path component is exactly '..'
|
|
810
|
+
preDotState === 1 && startDot === end - 1 && startDot === startPart + 1
|
|
811
|
+
){
|
|
812
|
+
if (end !== -1) {
|
|
813
|
+
if (startPart === 0 && isAbsolute) ret.base = ret.name = path.slice(1, end);
|
|
814
|
+
else ret.base = ret.name = path.slice(startPart, end);
|
|
815
|
+
}
|
|
816
|
+
} else {
|
|
817
|
+
if (startPart === 0 && isAbsolute) {
|
|
818
|
+
ret.name = path.slice(1, startDot);
|
|
819
|
+
ret.base = path.slice(1, end);
|
|
820
|
+
} else {
|
|
821
|
+
ret.name = path.slice(startPart, startDot);
|
|
822
|
+
ret.base = path.slice(startPart, end);
|
|
823
|
+
}
|
|
824
|
+
ret.ext = path.slice(startDot, end);
|
|
825
|
+
}
|
|
826
|
+
if (startPart > 0) ret.dir = path.slice(0, startPart - 1);else if (isAbsolute) ret.dir = '/';
|
|
827
|
+
return ret;
|
|
828
|
+
},
|
|
829
|
+
|
|
830
|
+
sep: '/',
|
|
831
|
+
delimiter: ':',
|
|
832
|
+
win32: null,
|
|
833
|
+
posix: null
|
|
834
|
+
};
|
|
835
|
+
|