Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/vs/base/common/path.ts
13405 views
1
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'
2
3
/*---------------------------------------------------------------------------------------------
4
* Copyright (c) Microsoft Corporation. All rights reserved.
5
* Licensed under the MIT License. See License.txt in the project root for license information.
6
*--------------------------------------------------------------------------------------------*/
7
8
// NOTE: VSCode's copy of nodejs path library to be usable in common (non-node) namespace
9
// Copied from: https://github.com/nodejs/node/commits/v22.15.0/lib/path.js
10
// Excluding: the change that adds primordials
11
// (https://github.com/nodejs/node/commit/187a862d221dec42fa9a5c4214e7034d9092792f and others)
12
// Excluding: the change that adds glob matching
13
// (https://github.com/nodejs/node/commit/57b8b8e18e5e2007114c63b71bf0baedc01936a6)
14
15
/**
16
* Copyright Joyent, Inc. and other Node contributors.
17
*
18
* Permission is hereby granted, free of charge, to any person obtaining a
19
* copy of this software and associated documentation files (the
20
* "Software"), to deal in the Software without restriction, including
21
* without limitation the rights to use, copy, modify, merge, publish,
22
* distribute, sublicense, and/or sell copies of the Software, and to permit
23
* persons to whom the Software is furnished to do so, subject to the
24
* following conditions:
25
*
26
* The above copyright notice and this permission notice shall be included
27
* in all copies or substantial portions of the Software.
28
*
29
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
32
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
33
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
34
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
35
* USE OR OTHER DEALINGS IN THE SOFTWARE.
36
*/
37
38
import * as process from './process';
39
40
const CHAR_UPPERCASE_A = 65;/* A */
41
const CHAR_LOWERCASE_A = 97; /* a */
42
const CHAR_UPPERCASE_Z = 90; /* Z */
43
const CHAR_LOWERCASE_Z = 122; /* z */
44
const CHAR_DOT = 46; /* . */
45
const CHAR_FORWARD_SLASH = 47; /* / */
46
const CHAR_BACKWARD_SLASH = 92; /* \ */
47
const CHAR_COLON = 58; /* : */
48
const CHAR_QUESTION_MARK = 63; /* ? */
49
50
class ErrorInvalidArgType extends Error {
51
code: 'ERR_INVALID_ARG_TYPE';
52
constructor(name: string, expected: string, actual: unknown) {
53
// determiner: 'must be' or 'must not be'
54
let determiner;
55
if (typeof expected === 'string' && expected.indexOf('not ') === 0) {
56
determiner = 'must not be';
57
expected = expected.replace(/^not /, '');
58
} else {
59
determiner = 'must be';
60
}
61
62
const type = name.indexOf('.') !== -1 ? 'property' : 'argument';
63
let msg = `The "${name}" ${type} ${determiner} of type ${expected}`;
64
65
msg += `. Received type ${typeof actual}`;
66
super(msg);
67
68
this.code = 'ERR_INVALID_ARG_TYPE';
69
}
70
}
71
72
function validateObject(pathObject: object, name: string) {
73
if (pathObject === null || typeof pathObject !== 'object') {
74
throw new ErrorInvalidArgType(name, 'Object', pathObject);
75
}
76
}
77
78
function validateString(value: string, name: string) {
79
if (typeof value !== 'string') {
80
throw new ErrorInvalidArgType(name, 'string', value);
81
}
82
}
83
84
const platformIsWin32 = (process.platform === 'win32');
85
86
function isPathSeparator(code: number | undefined) {
87
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
88
}
89
90
function isPosixPathSeparator(code: number | undefined) {
91
return code === CHAR_FORWARD_SLASH;
92
}
93
94
function isWindowsDeviceRoot(code: number) {
95
return (code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) ||
96
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z);
97
}
98
99
// Resolves . and .. elements in a path with directory names
100
function normalizeString(path: string, allowAboveRoot: boolean, separator: string, isPathSeparator: (code?: number) => boolean) {
101
let res = '';
102
let lastSegmentLength = 0;
103
let lastSlash = -1;
104
let dots = 0;
105
let code = 0;
106
for (let i = 0; i <= path.length; ++i) {
107
if (i < path.length) {
108
code = path.charCodeAt(i);
109
}
110
else if (isPathSeparator(code)) {
111
break;
112
}
113
else {
114
code = CHAR_FORWARD_SLASH;
115
}
116
117
if (isPathSeparator(code)) {
118
if (lastSlash === i - 1 || dots === 1) {
119
// NOOP
120
} else if (dots === 2) {
121
if (res.length < 2 || lastSegmentLength !== 2 ||
122
res.charCodeAt(res.length - 1) !== CHAR_DOT ||
123
res.charCodeAt(res.length - 2) !== CHAR_DOT) {
124
if (res.length > 2) {
125
const lastSlashIndex = res.lastIndexOf(separator);
126
if (lastSlashIndex === -1) {
127
res = '';
128
lastSegmentLength = 0;
129
} else {
130
res = res.slice(0, lastSlashIndex);
131
lastSegmentLength = res.length - 1 - res.lastIndexOf(separator);
132
}
133
lastSlash = i;
134
dots = 0;
135
continue;
136
} else if (res.length !== 0) {
137
res = '';
138
lastSegmentLength = 0;
139
lastSlash = i;
140
dots = 0;
141
continue;
142
}
143
}
144
if (allowAboveRoot) {
145
res += res.length > 0 ? `${separator}..` : '..';
146
lastSegmentLength = 2;
147
}
148
} else {
149
if (res.length > 0) {
150
res += `${separator}${path.slice(lastSlash + 1, i)}`;
151
}
152
else {
153
res = path.slice(lastSlash + 1, i);
154
}
155
lastSegmentLength = i - lastSlash - 1;
156
}
157
lastSlash = i;
158
dots = 0;
159
} else if (code === CHAR_DOT && dots !== -1) {
160
++dots;
161
} else {
162
dots = -1;
163
}
164
}
165
return res;
166
}
167
168
function formatExt(ext: string): string {
169
return ext ? `${ext[0] === '.' ? '' : '.'}${ext}` : '';
170
}
171
172
function _format(sep: string, pathObject: ParsedPath) {
173
validateObject(pathObject, 'pathObject');
174
const dir = pathObject.dir || pathObject.root;
175
const base = pathObject.base ||
176
`${pathObject.name || ''}${formatExt(pathObject.ext)}`;
177
if (!dir) {
178
return base;
179
}
180
return dir === pathObject.root ? `${dir}${base}` : `${dir}${sep}${base}`;
181
}
182
183
export interface ParsedPath {
184
root: string;
185
dir: string;
186
base: string;
187
ext: string;
188
name: string;
189
}
190
191
export interface IPath {
192
normalize(path: string): string;
193
isAbsolute(path: string): boolean;
194
join(...paths: string[]): string;
195
resolve(...pathSegments: string[]): string;
196
relative(from: string, to: string): string;
197
dirname(path: string): string;
198
basename(path: string, suffix?: string): string;
199
extname(path: string): string;
200
format(pathObject: ParsedPath): string;
201
parse(path: string): ParsedPath;
202
toNamespacedPath(path: string): string;
203
sep: '\\' | '/';
204
delimiter: string;
205
win32: IPath | null;
206
posix: IPath | null;
207
}
208
209
export const win32: IPath = {
210
// path.resolve([from ...], to)
211
resolve(...pathSegments: string[]): string {
212
let resolvedDevice = '';
213
let resolvedTail = '';
214
let resolvedAbsolute = false;
215
216
for (let i = pathSegments.length - 1; i >= -1; i--) {
217
let path;
218
if (i >= 0) {
219
path = pathSegments[i];
220
validateString(path, `paths[${i}]`);
221
222
// Skip empty entries
223
if (path.length === 0) {
224
continue;
225
}
226
} else if (resolvedDevice.length === 0) {
227
path = process.cwd();
228
} else {
229
// Windows has the concept of drive-specific current working
230
// directories. If we've resolved a drive letter but not yet an
231
// absolute path, get cwd for that drive, or the process cwd if
232
// the drive cwd is not available. We're sure the device is not
233
// a UNC path at this points, because UNC paths are always absolute.
234
path = process.env[`=${resolvedDevice}`] || process.cwd();
235
236
// Verify that a cwd was found and that it actually points
237
// to our drive. If not, default to the drive's root.
238
if (path === undefined ||
239
(path.slice(0, 2).toLowerCase() !== resolvedDevice.toLowerCase() &&
240
path.charCodeAt(2) === CHAR_BACKWARD_SLASH)) {
241
path = `${resolvedDevice}\\`;
242
}
243
}
244
245
const len = path.length;
246
let rootEnd = 0;
247
let device = '';
248
let isAbsolute = false;
249
const code = path.charCodeAt(0);
250
251
// Try to match a root
252
if (len === 1) {
253
if (isPathSeparator(code)) {
254
// `path` contains just a path separator
255
rootEnd = 1;
256
isAbsolute = true;
257
}
258
} else if (isPathSeparator(code)) {
259
// Possible UNC root
260
261
// If we started with a separator, we know we at least have an
262
// absolute path of some kind (UNC or otherwise)
263
isAbsolute = true;
264
265
if (isPathSeparator(path.charCodeAt(1))) {
266
// Matched double path separator at beginning
267
let j = 2;
268
let last = j;
269
// Match 1 or more non-path separators
270
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
271
j++;
272
}
273
if (j < len && j !== last) {
274
const firstPart = path.slice(last, j);
275
// Matched!
276
last = j;
277
// Match 1 or more path separators
278
while (j < len && isPathSeparator(path.charCodeAt(j))) {
279
j++;
280
}
281
if (j < len && j !== last) {
282
// Matched!
283
last = j;
284
// Match 1 or more non-path separators
285
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
286
j++;
287
}
288
if (j === len || j !== last) {
289
// We matched a UNC root
290
device = `\\\\${firstPart}\\${path.slice(last, j)}`;
291
rootEnd = j;
292
}
293
}
294
}
295
} else {
296
rootEnd = 1;
297
}
298
} else if (isWindowsDeviceRoot(code) &&
299
path.charCodeAt(1) === CHAR_COLON) {
300
// Possible device root
301
device = path.slice(0, 2);
302
rootEnd = 2;
303
if (len > 2 && isPathSeparator(path.charCodeAt(2))) {
304
// Treat separator following drive name as an absolute path
305
// indicator
306
isAbsolute = true;
307
rootEnd = 3;
308
}
309
}
310
311
if (device.length > 0) {
312
if (resolvedDevice.length > 0) {
313
if (device.toLowerCase() !== resolvedDevice.toLowerCase()) {
314
// This path points to another device so it is not applicable
315
continue;
316
}
317
} else {
318
resolvedDevice = device;
319
}
320
}
321
322
if (resolvedAbsolute) {
323
if (resolvedDevice.length > 0) {
324
break;
325
}
326
} else {
327
resolvedTail = `${path.slice(rootEnd)}\\${resolvedTail}`;
328
resolvedAbsolute = isAbsolute;
329
if (isAbsolute && resolvedDevice.length > 0) {
330
break;
331
}
332
}
333
}
334
335
// At this point the path should be resolved to a full absolute path,
336
// but handle relative paths to be safe (might happen when process.cwd()
337
// fails)
338
339
// Normalize the tail path
340
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\',
341
isPathSeparator);
342
343
return resolvedAbsolute ?
344
`${resolvedDevice}\\${resolvedTail}` :
345
`${resolvedDevice}${resolvedTail}` || '.';
346
},
347
348
normalize(path: string): string {
349
validateString(path, 'path');
350
const len = path.length;
351
if (len === 0) {
352
return '.';
353
}
354
let rootEnd = 0;
355
let device;
356
let isAbsolute = false;
357
const code = path.charCodeAt(0);
358
359
// Try to match a root
360
if (len === 1) {
361
// `path` contains just a single char, exit early to avoid
362
// unnecessary work
363
return isPosixPathSeparator(code) ? '\\' : path;
364
}
365
if (isPathSeparator(code)) {
366
// Possible UNC root
367
368
// If we started with a separator, we know we at least have an absolute
369
// path of some kind (UNC or otherwise)
370
isAbsolute = true;
371
372
if (isPathSeparator(path.charCodeAt(1))) {
373
// Matched double path separator at beginning
374
let j = 2;
375
let last = j;
376
// Match 1 or more non-path separators
377
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
378
j++;
379
}
380
if (j < len && j !== last) {
381
const firstPart = path.slice(last, j);
382
// Matched!
383
last = j;
384
// Match 1 or more path separators
385
while (j < len && isPathSeparator(path.charCodeAt(j))) {
386
j++;
387
}
388
if (j < len && j !== last) {
389
// Matched!
390
last = j;
391
// Match 1 or more non-path separators
392
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
393
j++;
394
}
395
if (j === len) {
396
// We matched a UNC root only
397
// Return the normalized version of the UNC root since there
398
// is nothing left to process
399
return `\\\\${firstPart}\\${path.slice(last)}\\`;
400
}
401
if (j !== last) {
402
// We matched a UNC root with leftovers
403
device = `\\\\${firstPart}\\${path.slice(last, j)}`;
404
rootEnd = j;
405
}
406
}
407
}
408
} else {
409
rootEnd = 1;
410
}
411
} else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
412
// Possible device root
413
device = path.slice(0, 2);
414
rootEnd = 2;
415
if (len > 2 && isPathSeparator(path.charCodeAt(2))) {
416
// Treat separator following drive name as an absolute path
417
// indicator
418
isAbsolute = true;
419
rootEnd = 3;
420
}
421
}
422
423
let tail = rootEnd < len ?
424
normalizeString(path.slice(rootEnd), !isAbsolute, '\\', isPathSeparator) :
425
'';
426
if (tail.length === 0 && !isAbsolute) {
427
tail = '.';
428
}
429
if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1))) {
430
tail += '\\';
431
}
432
if (!isAbsolute && device === undefined && path.includes(':')) {
433
// If the original path was not absolute and if we have not been able to
434
// resolve it relative to a particular device, we need to ensure that the
435
// `tail` has not become something that Windows might interpret as an
436
// absolute path. See CVE-2024-36139.
437
if (tail.length >= 2 &&
438
isWindowsDeviceRoot(tail.charCodeAt(0)) &&
439
tail.charCodeAt(1) === CHAR_COLON) {
440
return `.\\${tail}`;
441
}
442
let index = path.indexOf(':');
443
do {
444
if (index === len - 1 || isPathSeparator(path.charCodeAt(index + 1))) {
445
return `.\\${tail}`;
446
}
447
} while ((index = path.indexOf(':', index + 1)) !== -1);
448
}
449
if (device === undefined) {
450
return isAbsolute ? `\\${tail}` : tail;
451
}
452
return isAbsolute ? `${device}\\${tail}` : `${device}${tail}`;
453
},
454
455
isAbsolute(path: string): boolean {
456
validateString(path, 'path');
457
const len = path.length;
458
if (len === 0) {
459
return false;
460
}
461
462
const code = path.charCodeAt(0);
463
return isPathSeparator(code) ||
464
// Possible device root
465
(len > 2 &&
466
isWindowsDeviceRoot(code) &&
467
path.charCodeAt(1) === CHAR_COLON &&
468
isPathSeparator(path.charCodeAt(2)));
469
},
470
471
join(...paths: string[]): string {
472
if (paths.length === 0) {
473
return '.';
474
}
475
476
let joined;
477
let firstPart: string | undefined;
478
for (let i = 0; i < paths.length; ++i) {
479
const arg = paths[i];
480
validateString(arg, 'path');
481
if (arg.length > 0) {
482
if (joined === undefined) {
483
joined = firstPart = arg;
484
}
485
else {
486
joined += `\\${arg}`;
487
}
488
}
489
}
490
491
if (joined === undefined) {
492
return '.';
493
}
494
495
// Make sure that the joined path doesn't start with two slashes, because
496
// normalize() will mistake it for a UNC path then.
497
//
498
// This step is skipped when it is very clear that the user actually
499
// intended to point at a UNC path. This is assumed when the first
500
// non-empty string arguments starts with exactly two slashes followed by
501
// at least one more non-slash character.
502
//
503
// Note that for normalize() to treat a path as a UNC path it needs to
504
// have at least 2 components, so we don't filter for that here.
505
// This means that the user can use join to construct UNC paths from
506
// a server name and a share name; for example:
507
// path.join('//server', 'share') -> '\\\\server\\share\\')
508
let needsReplace = true;
509
let slashCount = 0;
510
if (typeof firstPart === 'string' && isPathSeparator(firstPart.charCodeAt(0))) {
511
++slashCount;
512
const firstLen = firstPart.length;
513
if (firstLen > 1 && isPathSeparator(firstPart.charCodeAt(1))) {
514
++slashCount;
515
if (firstLen > 2) {
516
if (isPathSeparator(firstPart.charCodeAt(2))) {
517
++slashCount;
518
} else {
519
// We matched a UNC path in the first part
520
needsReplace = false;
521
}
522
}
523
}
524
}
525
if (needsReplace) {
526
// Find any more consecutive slashes we need to replace
527
while (slashCount < joined.length &&
528
isPathSeparator(joined.charCodeAt(slashCount))) {
529
slashCount++;
530
}
531
532
// Replace the slashes if needed
533
if (slashCount >= 2) {
534
joined = `\\${joined.slice(slashCount)}`;
535
}
536
}
537
538
return win32.normalize(joined);
539
},
540
541
542
// It will solve the relative path from `from` to `to`, for instance:
543
// from = 'C:\\orandea\\test\\aaa'
544
// to = 'C:\\orandea\\impl\\bbb'
545
// The output of the function should be: '..\\..\\impl\\bbb'
546
relative(from: string, to: string): string {
547
validateString(from, 'from');
548
validateString(to, 'to');
549
550
if (from === to) {
551
return '';
552
}
553
554
const fromOrig = win32.resolve(from);
555
const toOrig = win32.resolve(to);
556
557
if (fromOrig === toOrig) {
558
return '';
559
}
560
561
from = fromOrig.toLowerCase();
562
to = toOrig.toLowerCase();
563
564
if (from === to) {
565
return '';
566
}
567
568
if (fromOrig.length !== from.length || toOrig.length !== to.length) {
569
const fromSplit = fromOrig.split('\\');
570
const toSplit = toOrig.split('\\');
571
if (fromSplit[fromSplit.length - 1] === '') {
572
fromSplit.pop();
573
}
574
if (toSplit[toSplit.length - 1] === '') {
575
toSplit.pop();
576
}
577
578
const fromLen = fromSplit.length;
579
const toLen = toSplit.length;
580
const length = fromLen < toLen ? fromLen : toLen;
581
582
let i;
583
for (i = 0; i < length; i++) {
584
if (fromSplit[i].toLowerCase() !== toSplit[i].toLowerCase()) {
585
break;
586
}
587
}
588
589
if (i === 0) {
590
return toOrig;
591
} else if (i === length) {
592
if (toLen > length) {
593
return toSplit.slice(i).join('\\');
594
}
595
if (fromLen > length) {
596
return '..\\'.repeat(fromLen - 1 - i) + '..';
597
}
598
return '';
599
}
600
601
return '..\\'.repeat(fromLen - i) + toSplit.slice(i).join('\\');
602
}
603
604
// Trim any leading backslashes
605
let fromStart = 0;
606
while (fromStart < from.length &&
607
from.charCodeAt(fromStart) === CHAR_BACKWARD_SLASH) {
608
fromStart++;
609
}
610
// Trim trailing backslashes (applicable to UNC paths only)
611
let fromEnd = from.length;
612
while (fromEnd - 1 > fromStart &&
613
from.charCodeAt(fromEnd - 1) === CHAR_BACKWARD_SLASH) {
614
fromEnd--;
615
}
616
const fromLen = fromEnd - fromStart;
617
618
// Trim any leading backslashes
619
let toStart = 0;
620
while (toStart < to.length &&
621
to.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) {
622
toStart++;
623
}
624
// Trim trailing backslashes (applicable to UNC paths only)
625
let toEnd = to.length;
626
while (toEnd - 1 > toStart &&
627
to.charCodeAt(toEnd - 1) === CHAR_BACKWARD_SLASH) {
628
toEnd--;
629
}
630
const toLen = toEnd - toStart;
631
632
// Compare paths to find the longest common path from root
633
const length = fromLen < toLen ? fromLen : toLen;
634
let lastCommonSep = -1;
635
let i = 0;
636
for (; i < length; i++) {
637
const fromCode = from.charCodeAt(fromStart + i);
638
if (fromCode !== to.charCodeAt(toStart + i)) {
639
break;
640
} else if (fromCode === CHAR_BACKWARD_SLASH) {
641
lastCommonSep = i;
642
}
643
}
644
645
// We found a mismatch before the first common path separator was seen, so
646
// return the original `to`.
647
if (i !== length) {
648
if (lastCommonSep === -1) {
649
return toOrig;
650
}
651
} else {
652
if (toLen > length) {
653
if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
654
// We get here if `from` is the exact base path for `to`.
655
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
656
return toOrig.slice(toStart + i + 1);
657
}
658
if (i === 2) {
659
// We get here if `from` is the device root.
660
// For example: from='C:\\'; to='C:\\foo'
661
return toOrig.slice(toStart + i);
662
}
663
}
664
if (fromLen > length) {
665
if (from.charCodeAt(fromStart + i) === CHAR_BACKWARD_SLASH) {
666
// We get here if `to` is the exact base path for `from`.
667
// For example: from='C:\\foo\\bar'; to='C:\\foo'
668
lastCommonSep = i;
669
} else if (i === 2) {
670
// We get here if `to` is the device root.
671
// For example: from='C:\\foo\\bar'; to='C:\\'
672
lastCommonSep = 3;
673
}
674
}
675
if (lastCommonSep === -1) {
676
lastCommonSep = 0;
677
}
678
}
679
680
let out = '';
681
// Generate the relative path based on the path difference between `to` and
682
// `from`
683
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
684
if (i === fromEnd || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
685
out += out.length === 0 ? '..' : '\\..';
686
}
687
}
688
689
toStart += lastCommonSep;
690
691
// Lastly, append the rest of the destination (`to`) path that comes after
692
// the common path parts
693
if (out.length > 0) {
694
return `${out}${toOrig.slice(toStart, toEnd)}`;
695
}
696
697
if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) {
698
++toStart;
699
}
700
701
return toOrig.slice(toStart, toEnd);
702
},
703
704
toNamespacedPath(path: string): string {
705
// Note: this will *probably* throw somewhere.
706
if (typeof path !== 'string' || path.length === 0) {
707
return path;
708
}
709
710
const resolvedPath = win32.resolve(path);
711
712
if (resolvedPath.length <= 2) {
713
return path;
714
}
715
716
if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) {
717
// Possible UNC root
718
if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) {
719
const code = resolvedPath.charCodeAt(2);
720
if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) {
721
// Matched non-long UNC root, convert the path to a long UNC path
722
return `\\\\?\\UNC\\${resolvedPath.slice(2)}`;
723
}
724
}
725
} else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0)) &&
726
resolvedPath.charCodeAt(1) === CHAR_COLON &&
727
resolvedPath.charCodeAt(2) === CHAR_BACKWARD_SLASH) {
728
// Matched device root, convert the path to a long UNC path
729
return `\\\\?\\${resolvedPath}`;
730
}
731
732
return resolvedPath;
733
},
734
735
dirname(path: string): string {
736
validateString(path, 'path');
737
const len = path.length;
738
if (len === 0) {
739
return '.';
740
}
741
let rootEnd = -1;
742
let offset = 0;
743
const code = path.charCodeAt(0);
744
745
if (len === 1) {
746
// `path` contains just a path separator, exit early to avoid
747
// unnecessary work or a dot.
748
return isPathSeparator(code) ? path : '.';
749
}
750
751
// Try to match a root
752
if (isPathSeparator(code)) {
753
// Possible UNC root
754
755
rootEnd = offset = 1;
756
757
if (isPathSeparator(path.charCodeAt(1))) {
758
// Matched double path separator at beginning
759
let j = 2;
760
let last = j;
761
// Match 1 or more non-path separators
762
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
763
j++;
764
}
765
if (j < len && j !== last) {
766
// Matched!
767
last = j;
768
// Match 1 or more path separators
769
while (j < len && isPathSeparator(path.charCodeAt(j))) {
770
j++;
771
}
772
if (j < len && j !== last) {
773
// Matched!
774
last = j;
775
// Match 1 or more non-path separators
776
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
777
j++;
778
}
779
if (j === len) {
780
// We matched a UNC root only
781
return path;
782
}
783
if (j !== last) {
784
// We matched a UNC root with leftovers
785
786
// Offset by 1 to include the separator after the UNC root to
787
// treat it as a "normal root" on top of a (UNC) root
788
rootEnd = offset = j + 1;
789
}
790
}
791
}
792
}
793
// Possible device root
794
} else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
795
rootEnd = len > 2 && isPathSeparator(path.charCodeAt(2)) ? 3 : 2;
796
offset = rootEnd;
797
}
798
799
let end = -1;
800
let matchedSlash = true;
801
for (let i = len - 1; i >= offset; --i) {
802
if (isPathSeparator(path.charCodeAt(i))) {
803
if (!matchedSlash) {
804
end = i;
805
break;
806
}
807
} else {
808
// We saw the first non-path separator
809
matchedSlash = false;
810
}
811
}
812
813
if (end === -1) {
814
if (rootEnd === -1) {
815
return '.';
816
}
817
818
end = rootEnd;
819
}
820
return path.slice(0, end);
821
},
822
823
basename(path: string, suffix?: string): string {
824
if (suffix !== undefined) {
825
validateString(suffix, 'suffix');
826
}
827
validateString(path, 'path');
828
let start = 0;
829
let end = -1;
830
let matchedSlash = true;
831
let i;
832
833
// Check for a drive letter prefix so as not to mistake the following
834
// path separator as an extra separator at the end of the path that can be
835
// disregarded
836
if (path.length >= 2 &&
837
isWindowsDeviceRoot(path.charCodeAt(0)) &&
838
path.charCodeAt(1) === CHAR_COLON) {
839
start = 2;
840
}
841
842
if (suffix !== undefined && suffix.length > 0 && suffix.length <= path.length) {
843
if (suffix === path) {
844
return '';
845
}
846
let extIdx = suffix.length - 1;
847
let firstNonSlashEnd = -1;
848
for (i = path.length - 1; i >= start; --i) {
849
const code = path.charCodeAt(i);
850
if (isPathSeparator(code)) {
851
// If we reached a path separator that was not part of a set of path
852
// separators at the end of the string, stop now
853
if (!matchedSlash) {
854
start = i + 1;
855
break;
856
}
857
} else {
858
if (firstNonSlashEnd === -1) {
859
// We saw the first non-path separator, remember this index in case
860
// we need it if the extension ends up not matching
861
matchedSlash = false;
862
firstNonSlashEnd = i + 1;
863
}
864
if (extIdx >= 0) {
865
// Try to match the explicit extension
866
if (code === suffix.charCodeAt(extIdx)) {
867
if (--extIdx === -1) {
868
// We matched the extension, so mark this as the end of our path
869
// component
870
end = i;
871
}
872
} else {
873
// Extension does not match, so our result is the entire path
874
// component
875
extIdx = -1;
876
end = firstNonSlashEnd;
877
}
878
}
879
}
880
}
881
882
if (start === end) {
883
end = firstNonSlashEnd;
884
} else if (end === -1) {
885
end = path.length;
886
}
887
return path.slice(start, end);
888
}
889
for (i = path.length - 1; i >= start; --i) {
890
if (isPathSeparator(path.charCodeAt(i))) {
891
// If we reached a path separator that was not part of a set of path
892
// separators at the end of the string, stop now
893
if (!matchedSlash) {
894
start = i + 1;
895
break;
896
}
897
} else if (end === -1) {
898
// We saw the first non-path separator, mark this as the end of our
899
// path component
900
matchedSlash = false;
901
end = i + 1;
902
}
903
}
904
905
if (end === -1) {
906
return '';
907
}
908
return path.slice(start, end);
909
},
910
911
extname(path: string): string {
912
validateString(path, 'path');
913
let start = 0;
914
let startDot = -1;
915
let startPart = 0;
916
let end = -1;
917
let matchedSlash = true;
918
// Track the state of characters (if any) we see before our first dot and
919
// after any path separator we find
920
let preDotState = 0;
921
922
// Check for a drive letter prefix so as not to mistake the following
923
// path separator as an extra separator at the end of the path that can be
924
// disregarded
925
926
if (path.length >= 2 &&
927
path.charCodeAt(1) === CHAR_COLON &&
928
isWindowsDeviceRoot(path.charCodeAt(0))) {
929
start = startPart = 2;
930
}
931
932
for (let i = path.length - 1; i >= start; --i) {
933
const code = path.charCodeAt(i);
934
if (isPathSeparator(code)) {
935
// If we reached a path separator that was not part of a set of path
936
// separators at the end of the string, stop now
937
if (!matchedSlash) {
938
startPart = i + 1;
939
break;
940
}
941
continue;
942
}
943
if (end === -1) {
944
// We saw the first non-path separator, mark this as the end of our
945
// extension
946
matchedSlash = false;
947
end = i + 1;
948
}
949
if (code === CHAR_DOT) {
950
// If this is our first dot, mark it as the start of our extension
951
if (startDot === -1) {
952
startDot = i;
953
}
954
else if (preDotState !== 1) {
955
preDotState = 1;
956
}
957
} else if (startDot !== -1) {
958
// We saw a non-dot and non-path separator before our dot, so we should
959
// have a good chance at having a non-empty extension
960
preDotState = -1;
961
}
962
}
963
964
if (startDot === -1 ||
965
end === -1 ||
966
// We saw a non-dot character immediately before the dot
967
preDotState === 0 ||
968
// The (right-most) trimmed path component is exactly '..'
969
(preDotState === 1 &&
970
startDot === end - 1 &&
971
startDot === startPart + 1)) {
972
return '';
973
}
974
return path.slice(startDot, end);
975
},
976
977
format: _format.bind(null, '\\'),
978
979
parse(path) {
980
validateString(path, 'path');
981
982
const ret = { root: '', dir: '', base: '', ext: '', name: '' };
983
if (path.length === 0) {
984
return ret;
985
}
986
987
const len = path.length;
988
let rootEnd = 0;
989
let code = path.charCodeAt(0);
990
991
if (len === 1) {
992
if (isPathSeparator(code)) {
993
// `path` contains just a path separator, exit early to avoid
994
// unnecessary work
995
ret.root = ret.dir = path;
996
return ret;
997
}
998
ret.base = ret.name = path;
999
return ret;
1000
}
1001
// Try to match a root
1002
if (isPathSeparator(code)) {
1003
// Possible UNC root
1004
1005
rootEnd = 1;
1006
if (isPathSeparator(path.charCodeAt(1))) {
1007
// Matched double path separator at beginning
1008
let j = 2;
1009
let last = j;
1010
// Match 1 or more non-path separators
1011
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
1012
j++;
1013
}
1014
if (j < len && j !== last) {
1015
// Matched!
1016
last = j;
1017
// Match 1 or more path separators
1018
while (j < len && isPathSeparator(path.charCodeAt(j))) {
1019
j++;
1020
}
1021
if (j < len && j !== last) {
1022
// Matched!
1023
last = j;
1024
// Match 1 or more non-path separators
1025
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
1026
j++;
1027
}
1028
if (j === len) {
1029
// We matched a UNC root only
1030
rootEnd = j;
1031
} else if (j !== last) {
1032
// We matched a UNC root with leftovers
1033
rootEnd = j + 1;
1034
}
1035
}
1036
}
1037
}
1038
} else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
1039
// Possible device root
1040
if (len <= 2) {
1041
// `path` contains just a drive root, exit early to avoid
1042
// unnecessary work
1043
ret.root = ret.dir = path;
1044
return ret;
1045
}
1046
rootEnd = 2;
1047
if (isPathSeparator(path.charCodeAt(2))) {
1048
if (len === 3) {
1049
// `path` contains just a drive root, exit early to avoid
1050
// unnecessary work
1051
ret.root = ret.dir = path;
1052
return ret;
1053
}
1054
rootEnd = 3;
1055
}
1056
}
1057
if (rootEnd > 0) {
1058
ret.root = path.slice(0, rootEnd);
1059
}
1060
1061
let startDot = -1;
1062
let startPart = rootEnd;
1063
let end = -1;
1064
let matchedSlash = true;
1065
let i = path.length - 1;
1066
1067
// Track the state of characters (if any) we see before our first dot and
1068
// after any path separator we find
1069
let preDotState = 0;
1070
1071
// Get non-dir info
1072
for (; i >= rootEnd; --i) {
1073
code = path.charCodeAt(i);
1074
if (isPathSeparator(code)) {
1075
// If we reached a path separator that was not part of a set of path
1076
// separators at the end of the string, stop now
1077
if (!matchedSlash) {
1078
startPart = i + 1;
1079
break;
1080
}
1081
continue;
1082
}
1083
if (end === -1) {
1084
// We saw the first non-path separator, mark this as the end of our
1085
// extension
1086
matchedSlash = false;
1087
end = i + 1;
1088
}
1089
if (code === CHAR_DOT) {
1090
// If this is our first dot, mark it as the start of our extension
1091
if (startDot === -1) {
1092
startDot = i;
1093
} else if (preDotState !== 1) {
1094
preDotState = 1;
1095
}
1096
} else if (startDot !== -1) {
1097
// We saw a non-dot and non-path separator before our dot, so we should
1098
// have a good chance at having a non-empty extension
1099
preDotState = -1;
1100
}
1101
}
1102
1103
if (end !== -1) {
1104
if (startDot === -1 ||
1105
// We saw a non-dot character immediately before the dot
1106
preDotState === 0 ||
1107
// The (right-most) trimmed path component is exactly '..'
1108
(preDotState === 1 &&
1109
startDot === end - 1 &&
1110
startDot === startPart + 1)) {
1111
ret.base = ret.name = path.slice(startPart, end);
1112
} else {
1113
ret.name = path.slice(startPart, startDot);
1114
ret.base = path.slice(startPart, end);
1115
ret.ext = path.slice(startDot, end);
1116
}
1117
}
1118
1119
// If the directory is the root, use the entire root as the `dir` including
1120
// the trailing slash if any (`C:\abc` -> `C:\`). Otherwise, strip out the
1121
// trailing slash (`C:\abc\def` -> `C:\abc`).
1122
if (startPart > 0 && startPart !== rootEnd) {
1123
ret.dir = path.slice(0, startPart - 1);
1124
} else {
1125
ret.dir = ret.root;
1126
}
1127
1128
return ret;
1129
},
1130
1131
sep: '\\',
1132
delimiter: ';',
1133
win32: null,
1134
posix: null
1135
};
1136
1137
const posixCwd = (() => {
1138
if (platformIsWin32) {
1139
// Converts Windows' backslash path separators to POSIX forward slashes
1140
// and truncates any drive indicator
1141
const regexp = /\\/g;
1142
return () => {
1143
const cwd = process.cwd().replace(regexp, '/');
1144
return cwd.slice(cwd.indexOf('/'));
1145
};
1146
}
1147
1148
// We're already on POSIX, no need for any transformations
1149
return () => process.cwd();
1150
})();
1151
1152
export const posix: IPath = {
1153
// path.resolve([from ...], to)
1154
resolve(...pathSegments: string[]): string {
1155
let resolvedPath = '';
1156
let resolvedAbsolute = false;
1157
1158
for (let i = pathSegments.length - 1; i >= 0 && !resolvedAbsolute; i--) {
1159
const path = pathSegments[i];
1160
validateString(path, `paths[${i}]`);
1161
1162
// Skip empty entries
1163
if (path.length === 0) {
1164
continue;
1165
}
1166
1167
resolvedPath = `${path}/${resolvedPath}`;
1168
resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
1169
}
1170
1171
if (!resolvedAbsolute) {
1172
const cwd = posixCwd();
1173
resolvedPath = `${cwd}/${resolvedPath}`;
1174
resolvedAbsolute =
1175
cwd.charCodeAt(0) === CHAR_FORWARD_SLASH;
1176
}
1177
1178
// At this point the path should be resolved to a full absolute path, but
1179
// handle relative paths to be safe (might happen when process.cwd() fails)
1180
1181
// Normalize the path
1182
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/',
1183
isPosixPathSeparator);
1184
1185
if (resolvedAbsolute) {
1186
return `/${resolvedPath}`;
1187
}
1188
return resolvedPath.length > 0 ? resolvedPath : '.';
1189
},
1190
1191
normalize(path: string): string {
1192
validateString(path, 'path');
1193
1194
if (path.length === 0) {
1195
return '.';
1196
}
1197
1198
const isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
1199
const trailingSeparator =
1200
path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;
1201
1202
// Normalize the path
1203
path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);
1204
1205
if (path.length === 0) {
1206
if (isAbsolute) {
1207
return '/';
1208
}
1209
return trailingSeparator ? './' : '.';
1210
}
1211
if (trailingSeparator) {
1212
path += '/';
1213
}
1214
1215
return isAbsolute ? `/${path}` : path;
1216
},
1217
1218
isAbsolute(path: string): boolean {
1219
validateString(path, 'path');
1220
return path.length > 0 && path.charCodeAt(0) === CHAR_FORWARD_SLASH;
1221
},
1222
1223
join(...paths: string[]): string {
1224
if (paths.length === 0) {
1225
return '.';
1226
}
1227
1228
const path = [];
1229
for (let i = 0; i < paths.length; ++i) {
1230
const arg = paths[i];
1231
validateString(arg, 'path');
1232
if (arg.length > 0) {
1233
path.push(arg);
1234
}
1235
}
1236
1237
if (path.length === 0) {
1238
return '.';
1239
}
1240
1241
return posix.normalize(path.join('/'));
1242
},
1243
1244
relative(from: string, to: string): string {
1245
validateString(from, 'from');
1246
validateString(to, 'to');
1247
1248
if (from === to) {
1249
return '';
1250
}
1251
1252
// Trim leading forward slashes.
1253
from = posix.resolve(from);
1254
to = posix.resolve(to);
1255
1256
if (from === to) {
1257
return '';
1258
}
1259
1260
const fromStart = 1;
1261
const fromEnd = from.length;
1262
const fromLen = fromEnd - fromStart;
1263
const toStart = 1;
1264
const toLen = to.length - toStart;
1265
1266
// Compare paths to find the longest common path from root
1267
const length = (fromLen < toLen ? fromLen : toLen);
1268
let lastCommonSep = -1;
1269
let i = 0;
1270
for (; i < length; i++) {
1271
const fromCode = from.charCodeAt(fromStart + i);
1272
if (fromCode !== to.charCodeAt(toStart + i)) {
1273
break;
1274
} else if (fromCode === CHAR_FORWARD_SLASH) {
1275
lastCommonSep = i;
1276
}
1277
}
1278
if (i === length) {
1279
if (toLen > length) {
1280
if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
1281
// We get here if `from` is the exact base path for `to`.
1282
// For example: from='/foo/bar'; to='/foo/bar/baz'
1283
return to.slice(toStart + i + 1);
1284
}
1285
if (i === 0) {
1286
// We get here if `from` is the root
1287
// For example: from='/'; to='/foo'
1288
return to.slice(toStart + i);
1289
}
1290
} else if (fromLen > length) {
1291
if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
1292
// We get here if `to` is the exact base path for `from`.
1293
// For example: from='/foo/bar/baz'; to='/foo/bar'
1294
lastCommonSep = i;
1295
} else if (i === 0) {
1296
// We get here if `to` is the root.
1297
// For example: from='/foo/bar'; to='/'
1298
lastCommonSep = 0;
1299
}
1300
}
1301
}
1302
1303
let out = '';
1304
// Generate the relative path based on the path difference between `to`
1305
// and `from`.
1306
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
1307
if (i === fromEnd || from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
1308
out += out.length === 0 ? '..' : '/..';
1309
}
1310
}
1311
1312
// Lastly, append the rest of the destination (`to`) path that comes after
1313
// the common path parts.
1314
return `${out}${to.slice(toStart + lastCommonSep)}`;
1315
},
1316
1317
toNamespacedPath(path: string): string {
1318
// Non-op on posix systems
1319
return path;
1320
},
1321
1322
dirname(path: string): string {
1323
validateString(path, 'path');
1324
if (path.length === 0) {
1325
return '.';
1326
}
1327
const hasRoot = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
1328
let end = -1;
1329
let matchedSlash = true;
1330
for (let i = path.length - 1; i >= 1; --i) {
1331
if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
1332
if (!matchedSlash) {
1333
end = i;
1334
break;
1335
}
1336
} else {
1337
// We saw the first non-path separator
1338
matchedSlash = false;
1339
}
1340
}
1341
1342
if (end === -1) {
1343
return hasRoot ? '/' : '.';
1344
}
1345
if (hasRoot && end === 1) {
1346
return '//';
1347
}
1348
return path.slice(0, end);
1349
},
1350
1351
basename(path: string, suffix?: string): string {
1352
if (suffix !== undefined) {
1353
validateString(suffix, 'suffix');
1354
}
1355
validateString(path, 'path');
1356
1357
let start = 0;
1358
let end = -1;
1359
let matchedSlash = true;
1360
let i;
1361
1362
if (suffix !== undefined && suffix.length > 0 && suffix.length <= path.length) {
1363
if (suffix === path) {
1364
return '';
1365
}
1366
let extIdx = suffix.length - 1;
1367
let firstNonSlashEnd = -1;
1368
for (i = path.length - 1; i >= 0; --i) {
1369
const code = path.charCodeAt(i);
1370
if (code === CHAR_FORWARD_SLASH) {
1371
// If we reached a path separator that was not part of a set of path
1372
// separators at the end of the string, stop now
1373
if (!matchedSlash) {
1374
start = i + 1;
1375
break;
1376
}
1377
} else {
1378
if (firstNonSlashEnd === -1) {
1379
// We saw the first non-path separator, remember this index in case
1380
// we need it if the extension ends up not matching
1381
matchedSlash = false;
1382
firstNonSlashEnd = i + 1;
1383
}
1384
if (extIdx >= 0) {
1385
// Try to match the explicit extension
1386
if (code === suffix.charCodeAt(extIdx)) {
1387
if (--extIdx === -1) {
1388
// We matched the extension, so mark this as the end of our path
1389
// component
1390
end = i;
1391
}
1392
} else {
1393
// Extension does not match, so our result is the entire path
1394
// component
1395
extIdx = -1;
1396
end = firstNonSlashEnd;
1397
}
1398
}
1399
}
1400
}
1401
1402
if (start === end) {
1403
end = firstNonSlashEnd;
1404
} else if (end === -1) {
1405
end = path.length;
1406
}
1407
return path.slice(start, end);
1408
}
1409
for (i = path.length - 1; i >= 0; --i) {
1410
if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
1411
// If we reached a path separator that was not part of a set of path
1412
// separators at the end of the string, stop now
1413
if (!matchedSlash) {
1414
start = i + 1;
1415
break;
1416
}
1417
} else if (end === -1) {
1418
// We saw the first non-path separator, mark this as the end of our
1419
// path component
1420
matchedSlash = false;
1421
end = i + 1;
1422
}
1423
}
1424
1425
if (end === -1) {
1426
return '';
1427
}
1428
return path.slice(start, end);
1429
},
1430
1431
extname(path: string): string {
1432
validateString(path, 'path');
1433
let startDot = -1;
1434
let startPart = 0;
1435
let end = -1;
1436
let matchedSlash = true;
1437
// Track the state of characters (if any) we see before our first dot and
1438
// after any path separator we find
1439
let preDotState = 0;
1440
for (let i = path.length - 1; i >= 0; --i) {
1441
const char = path[i];
1442
if (char === '/') {
1443
// If we reached a path separator that was not part of a set of path
1444
// separators at the end of the string, stop now
1445
if (!matchedSlash) {
1446
startPart = i + 1;
1447
break;
1448
}
1449
continue;
1450
}
1451
if (end === -1) {
1452
// We saw the first non-path separator, mark this as the end of our
1453
// extension
1454
matchedSlash = false;
1455
end = i + 1;
1456
}
1457
if (char === '.') {
1458
// If this is our first dot, mark it as the start of our extension
1459
if (startDot === -1) {
1460
startDot = i;
1461
}
1462
else if (preDotState !== 1) {
1463
preDotState = 1;
1464
}
1465
} else if (startDot !== -1) {
1466
// We saw a non-dot and non-path separator before our dot, so we should
1467
// have a good chance at having a non-empty extension
1468
preDotState = -1;
1469
}
1470
}
1471
1472
if (startDot === -1 ||
1473
end === -1 ||
1474
// We saw a non-dot character immediately before the dot
1475
preDotState === 0 ||
1476
// The (right-most) trimmed path component is exactly '..'
1477
(preDotState === 1 &&
1478
startDot === end - 1 &&
1479
startDot === startPart + 1)) {
1480
return '';
1481
}
1482
return path.slice(startDot, end);
1483
},
1484
1485
format: _format.bind(null, '/'),
1486
1487
parse(path: string): ParsedPath {
1488
validateString(path, 'path');
1489
1490
const ret = { root: '', dir: '', base: '', ext: '', name: '' };
1491
if (path.length === 0) {
1492
return ret;
1493
}
1494
const isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
1495
let start;
1496
if (isAbsolute) {
1497
ret.root = '/';
1498
start = 1;
1499
} else {
1500
start = 0;
1501
}
1502
let startDot = -1;
1503
let startPart = 0;
1504
let end = -1;
1505
let matchedSlash = true;
1506
let i = path.length - 1;
1507
1508
// Track the state of characters (if any) we see before our first dot and
1509
// after any path separator we find
1510
let preDotState = 0;
1511
1512
// Get non-dir info
1513
for (; i >= start; --i) {
1514
const code = path.charCodeAt(i);
1515
if (code === CHAR_FORWARD_SLASH) {
1516
// If we reached a path separator that was not part of a set of path
1517
// separators at the end of the string, stop now
1518
if (!matchedSlash) {
1519
startPart = i + 1;
1520
break;
1521
}
1522
continue;
1523
}
1524
if (end === -1) {
1525
// We saw the first non-path separator, mark this as the end of our
1526
// extension
1527
matchedSlash = false;
1528
end = i + 1;
1529
}
1530
if (code === CHAR_DOT) {
1531
// If this is our first dot, mark it as the start of our extension
1532
if (startDot === -1) {
1533
startDot = i;
1534
} else if (preDotState !== 1) {
1535
preDotState = 1;
1536
}
1537
} else if (startDot !== -1) {
1538
// We saw a non-dot and non-path separator before our dot, so we should
1539
// have a good chance at having a non-empty extension
1540
preDotState = -1;
1541
}
1542
}
1543
1544
if (end !== -1) {
1545
const start = startPart === 0 && isAbsolute ? 1 : startPart;
1546
if (startDot === -1 ||
1547
// We saw a non-dot character immediately before the dot
1548
preDotState === 0 ||
1549
// The (right-most) trimmed path component is exactly '..'
1550
(preDotState === 1 &&
1551
startDot === end - 1 &&
1552
startDot === startPart + 1)) {
1553
ret.base = ret.name = path.slice(start, end);
1554
} else {
1555
ret.name = path.slice(start, startDot);
1556
ret.base = path.slice(start, end);
1557
ret.ext = path.slice(startDot, end);
1558
}
1559
}
1560
1561
if (startPart > 0) {
1562
ret.dir = path.slice(0, startPart - 1);
1563
} else if (isAbsolute) {
1564
ret.dir = '/';
1565
}
1566
1567
return ret;
1568
},
1569
1570
sep: '/',
1571
delimiter: ':',
1572
win32: null,
1573
posix: null
1574
};
1575
1576
posix.win32 = win32.win32 = win32;
1577
posix.posix = win32.posix = posix;
1578
1579
export const normalize = (platformIsWin32 ? win32.normalize : posix.normalize);
1580
export const isAbsolute = (platformIsWin32 ? win32.isAbsolute : posix.isAbsolute);
1581
export const join = (platformIsWin32 ? win32.join : posix.join);
1582
export const resolve = (platformIsWin32 ? win32.resolve : posix.resolve);
1583
export const relative = (platformIsWin32 ? win32.relative : posix.relative);
1584
export const dirname = (platformIsWin32 ? win32.dirname : posix.dirname);
1585
export const basename = (platformIsWin32 ? win32.basename : posix.basename);
1586
export const extname = (platformIsWin32 ? win32.extname : posix.extname);
1587
export const format = (platformIsWin32 ? win32.format : posix.format);
1588
export const parse = (platformIsWin32 ? win32.parse : posix.parse);
1589
export const toNamespacedPath = (platformIsWin32 ? win32.toNamespacedPath : posix.toNamespacedPath);
1590
export const sep = (platformIsWin32 ? win32.sep : posix.sep);
1591
export const delimiter = (platformIsWin32 ? win32.delimiter : posix.delimiter);
1592
1593