-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve-with.mts
267 lines (236 loc) · 7.01 KB
/
resolve-with.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* @file resolveWith
* @module pathe/lib/resolveWith
*/
import { DRIVE_PATH_REGEX, sepWindows } from '#internal/constants'
import normalizeString from '#internal/normalize-string'
import process from '#internal/process'
import validateString from '#internal/validate-string'
import dot from '#lib/dot'
import isSep from '#lib/is-sep'
import sep from '#lib/sep'
import toPosix from '#lib/to-posix'
import type { Cwd, ResolveWithOptions } from '@flex-development/pathe'
/**
* Resolve a sequence of paths or path segments into an absolute path.
*
* The given sequence of paths is processed from right to left, with each
* subsequent path prepended until an absolute path is constructed.
*
* For instance, given the sequence of path segments: `/foo`, `/bar`, `baz`,
* calling `pathe.resolve('/foo', '/bar', 'baz')` would return `/bar/baz`
* because `'baz'` is not an absolute path, but `'/bar' + '/' + 'baz'` is.
*
* If, after processing all given `path` segments, an absolute path has not yet
* been generated, the current working directory is used.
*
* The resulting path is normalized and trailing separators are removed unless
* the path is resolved to the root directory.
*
* Zero-length `path` segments are ignored.
*
* If no `path` segments are passed, the absolute path of the current working
* directory is returned.
*
* @see {@linkcode ResolveWithOptions}
*
* @todo url support
*
* @category
* utils
*
* @this {void}
*
* @param {ReadonlyArray<string> | string} paths
* Sequence of paths or path segments
* @param {ResolveWithOptions | null | undefined} [options]
* Resolution options
* @return {string}
* Absolute path
*/
function resolveWith(
this: void,
paths: string | readonly string[],
options?: ResolveWithOptions | null | undefined
): string {
/**
* Get the path to the current working directory.
*
* @var {Cwd | null | undefined} cwd
*/
let cwd: Cwd | null | undefined = options?.cwd
/**
* Environment variables.
*
* @var {Partial<Record<string, string>> | null | undefined} env
*/
let env: Partial<Record<string, string>> | null | undefined = options?.env
if (typeof cwd !== 'function') cwd = process.cwd
if (typeof env !== 'object' || env === null) env = process.env
if (typeof paths === 'string') paths = [paths]
/**
* Absolute path check.
*
* @var {boolean} resolvedAbsolute
*/
let resolvedAbsolute: boolean = false
/**
* Resolved drive letter or UNC path, if any.
*
* @var {string} resolvedDevice
*/
let resolvedDevice: string = ''
/**
* Resolved path without {@linkcode resolvedDevice}.
*
* @var {string} resolvedTail
*/
let resolvedTail: string = ''
for (let i = paths.length - 1; i >= -1; i--) {
/**
* Current path segment.
*
* @var {string} path
*/
let path: string
if (i >= 0) {
path = paths[i]!
validateString(path, `paths[${i}]`)
// skip empty path segments
if (!path.length) continue
} else if (!resolvedDevice.length) {
path = cwd()
} else {
/*
* Windows has the concept of drive-specific current working directories.
*
* If a drive letter has been resolved before an absolute path, we get the
* current working directory (cwd) for that drive, or `cwd()` if the
* drive's current working directory is not defined.
*
* Note that at this point, {@linkcode device} is guaranteed to **not** be
* a UNC path because UNC path are always absolute.
*
* If a cwd was found, but doesn't point to the drive, we default to the
* drive's root.
*/
path = env[`=${resolvedDevice}`] || cwd()
// default to drive root if cwd was found but does not point to drive
if (
!path ||
(
path.slice(0, 2).toLowerCase() !== resolvedDevice.toLowerCase() &&
isSep(path[2])
)
) {
path = resolvedDevice + sep
}
}
/**
* Current absolute path check.
*
* @var {boolean} absolute
*/
let absolute: boolean = false
/**
* Current drive letter or UNC path, if any.
*
* @var {string} device
*/
let device: string = ''
/**
* End index of root.
*
* @var {number} rootEnd
*/
let rootEnd: number = 0
if (path.length === 1) {
if (isSep(path)) {
absolute = true
rootEnd = 1
}
} else if (isSep(path[0])) {
absolute = true
if (!isSep(path[1])) {
rootEnd = 1
} else {
/**
* Current position in {@linkcode path}.
*
* @var {number} j
*/
let j: number = 2
/**
* Last visited position in {@linkcode path}.
*
* @var {number} last
*/
let last: number = j
// match 1 or more non-path separators
while (j < path.length && !isSep(path[j])) j++
if (j < path.length && j !== last) {
/**
* Path component.
*
* @const {string} comp
*/
const comp: string = path.slice(last, j)
// matched!
last = j
// match 1 or more path separators
while (j < path.length && isSep(path[j])) j++
if (j < path.length && j !== last) {
// matched!
last = j
// match 1 or more non-path separators
while (j < path.length && !isSep(path[j])) j++
// matched device root or unc root
if (j === path.length || j !== last) {
device = sepWindows.repeat(2) + comp
if (comp !== dot && comp !== '?') {
// matched unc root
device += sepWindows + path.slice(last, j)
rootEnd = j
} else {
// matched device root (i.e. `//./PHYSICALDRIVE0`)
rootEnd = 4
}
}
}
}
}
} else if (DRIVE_PATH_REGEX.test(path)) {
rootEnd = 2
device = path.slice(0, rootEnd)
if (path.length > rootEnd && isSep(path[rootEnd])) {
// treat separator after drive name as absolute path indicator
absolute = true
rootEnd++
}
}
if (device) {
if (resolvedDevice) {
// path points to another device -> not applicable
if (device.toLowerCase() !== resolvedDevice.toLowerCase()) continue
} else {
resolvedDevice = device
}
}
if (resolvedAbsolute) {
if (resolvedDevice.length) break
} else {
resolvedTail = `${path.slice(rootEnd)}${sep}${resolvedTail}`
resolvedAbsolute = absolute
if (absolute && resolvedDevice.length) break
}
}
// the resolved path should be absolute at this point, but `resolvedTail` is
// normalized anyway in case of `cwd()` failure.
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute)
return toPosix(
resolvedAbsolute
? `${resolvedDevice}${sep}${resolvedTail}`
: `${resolvedDevice}${resolvedTail}` || dot
)
}
export default resolveWith