keep-a-changelog 0.10.3 → 2.0.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.
Files changed (92) hide show
  1. package/CHANGELOG.md +13 -185
  2. package/LICENSE +1 -1
  3. package/README.md +93 -63
  4. package/esm/_dnt.shims.js +62 -0
  5. package/esm/bin.js +114 -0
  6. package/esm/deps/deno_land/std_0.120.0/_util/assert.js +13 -0
  7. package/esm/deps/deno_land/std_0.120.0/_util/os.js +15 -0
  8. package/esm/deps/deno_land/std_0.120.0/flags/mod.js +255 -0
  9. package/esm/deps/deno_land/std_0.120.0/path/_constants.js +45 -0
  10. package/esm/deps/deno_land/std_0.120.0/path/_interface.js +3 -0
  11. package/esm/deps/deno_land/std_0.120.0/path/_util.js +114 -0
  12. package/esm/deps/deno_land/std_0.120.0/path/common.js +36 -0
  13. package/esm/deps/deno_land/std_0.120.0/path/glob.js +361 -0
  14. package/esm/deps/deno_land/std_0.120.0/path/mod.js +14 -0
  15. package/esm/deps/deno_land/std_0.120.0/path/posix.js +493 -0
  16. package/esm/deps/deno_land/std_0.120.0/path/separator.js +5 -0
  17. package/esm/deps/deno_land/std_0.120.0/path/win32.js +963 -0
  18. package/esm/deps/deno_land/std_0.51.0/fs/eol.js +29 -0
  19. package/esm/deps/deno_land/x/ini_v2.1.0/ini.js +226 -0
  20. package/esm/deps/deno_land/x/ini_v2.1.0/mod.js +1 -0
  21. package/esm/deps/deno_land/x/semver_v1.4.0/mod.js +1527 -0
  22. package/esm/mod.js +5 -0
  23. package/esm/package.json +3 -0
  24. package/esm/src/Change.js +48 -0
  25. package/esm/src/Changelog.js +137 -0
  26. package/esm/src/Release.js +204 -0
  27. package/esm/src/deps.js +1 -0
  28. package/esm/src/parser.js +159 -0
  29. package/esm/test/changelog.custom.type.md +24 -0
  30. package/esm/test/changelog.expected.md +169 -0
  31. package/esm/test/changelog.md +200 -0
  32. package/esm/test/empty.expected.md +6 -0
  33. package/package.json +26 -24
  34. package/types/_dnt.shims.d.ts +9 -0
  35. package/types/bin.d.ts +2 -0
  36. package/types/deps/deno_land/std_0.120.0/_util/assert.d.ts +5 -0
  37. package/types/deps/deno_land/std_0.120.0/_util/os.d.ts +3 -0
  38. package/types/deps/deno_land/std_0.120.0/flags/mod.d.ts +50 -0
  39. package/types/deps/deno_land/std_0.120.0/path/_constants.d.ts +39 -0
  40. package/types/deps/deno_land/std_0.120.0/path/_interface.d.ts +26 -0
  41. package/types/deps/deno_land/std_0.120.0/path/_util.d.ts +8 -0
  42. package/types/deps/deno_land/std_0.120.0/path/common.d.ts +13 -0
  43. package/types/deps/deno_land/std_0.120.0/path/glob.d.ts +78 -0
  44. package/types/deps/deno_land/std_0.120.0/path/mod.d.ts +9 -0
  45. package/types/deps/deno_land/std_0.120.0/path/posix.d.ts +80 -0
  46. package/types/deps/deno_land/std_0.120.0/path/separator.d.ts +2 -0
  47. package/types/deps/deno_land/std_0.120.0/path/win32.d.ts +87 -0
  48. package/types/deps/deno_land/std_0.51.0/fs/eol.d.ts +12 -0
  49. package/types/deps/deno_land/x/ini_v2.1.0/ini.d.ts +53 -0
  50. package/types/deps/deno_land/x/ini_v2.1.0/mod.d.ts +1 -0
  51. package/types/deps/deno_land/x/semver_v1.4.0/mod.d.ts +116 -0
  52. package/types/mod.d.ts +5 -0
  53. package/types/src/Change.d.ts +8 -0
  54. package/types/src/Changelog.d.ts +18 -0
  55. package/types/src/Release.d.ts +25 -0
  56. package/types/src/deps.d.ts +1 -0
  57. package/types/src/parser.d.ts +11 -0
  58. package/umd/_dnt.shims.js +76 -0
  59. package/umd/bin.js +145 -0
  60. package/umd/deps/deno_land/std_0.120.0/_util/assert.js +28 -0
  61. package/umd/deps/deno_land/std_0.120.0/_util/os.js +47 -0
  62. package/umd/deps/deno_land/std_0.120.0/flags/mod.js +269 -0
  63. package/umd/deps/deno_land/std_0.120.0/path/_constants.js +58 -0
  64. package/umd/deps/deno_land/std_0.120.0/path/_interface.js +14 -0
  65. package/umd/deps/deno_land/std_0.120.0/path/_util.js +134 -0
  66. package/umd/deps/deno_land/std_0.120.0/path/common.js +50 -0
  67. package/umd/deps/deno_land/std_0.120.0/path/glob.js +397 -0
  68. package/umd/deps/deno_land/std_0.120.0/path/mod.js +51 -0
  69. package/umd/deps/deno_land/std_0.120.0/path/posix.js +538 -0
  70. package/umd/deps/deno_land/std_0.120.0/path/separator.js +18 -0
  71. package/umd/deps/deno_land/std_0.120.0/path/win32.js +1008 -0
  72. package/umd/deps/deno_land/std_0.51.0/fs/eol.js +44 -0
  73. package/umd/deps/deno_land/x/ini_v2.1.0/ini.js +262 -0
  74. package/umd/deps/deno_land/x/ini_v2.1.0/mod.js +23 -0
  75. package/umd/deps/deno_land/x/semver_v1.4.0/mod.js +1578 -0
  76. package/umd/mod.js +24 -0
  77. package/umd/package.json +3 -0
  78. package/umd/src/Change.js +61 -0
  79. package/umd/src/Changelog.js +150 -0
  80. package/umd/src/Release.js +220 -0
  81. package/umd/src/deps.js +19 -0
  82. package/umd/src/parser.js +175 -0
  83. package/umd/test/changelog.custom.type.md +24 -0
  84. package/umd/test/changelog.expected.md +169 -0
  85. package/umd/test/changelog.md +200 -0
  86. package/umd/test/empty.expected.md +6 -0
  87. package/bin.js +0 -114
  88. package/src/Change.js +0 -40
  89. package/src/Changelog.js +0 -121
  90. package/src/Release.js +0 -220
  91. package/src/index.js +0 -6
  92. package/src/parser.js +0 -182
@@ -0,0 +1,963 @@
1
+ import * as dntShim from "../../../../_dnt.shims.js";
2
+ import { CHAR_BACKWARD_SLASH, CHAR_COLON, CHAR_DOT, CHAR_QUESTION_MARK, } from "./_constants.js";
3
+ import { _format, assertPath, encodeWhitespace, isPathSeparator, isWindowsDeviceRoot, normalizeString, } from "./_util.js";
4
+ import { assert } from "../_util/assert.js";
5
+ export const sep = "\\";
6
+ export const delimiter = ";";
7
+ /**
8
+ * Resolves path segments into a `path`
9
+ * @param pathSegments to process to path
10
+ */
11
+ export function resolve(...pathSegments) {
12
+ let resolvedDevice = "";
13
+ let resolvedTail = "";
14
+ let resolvedAbsolute = false;
15
+ for (let i = pathSegments.length - 1; i >= -1; i--) {
16
+ let path;
17
+ // deno-lint-ignore no-explicit-any
18
+ const { Deno } = dntShim.dntGlobalThis;
19
+ if (i >= 0) {
20
+ path = pathSegments[i];
21
+ }
22
+ else if (!resolvedDevice) {
23
+ if (typeof Deno?.cwd !== "function") {
24
+ throw new TypeError("Resolved a drive-letter-less path without a CWD.");
25
+ }
26
+ path = Deno.cwd();
27
+ }
28
+ else {
29
+ if (typeof Deno?.env?.get !== "function" || typeof Deno?.cwd !== "function") {
30
+ throw new TypeError("Resolved a relative path without a CWD.");
31
+ }
32
+ path = Deno.cwd();
33
+ // Verify that a cwd was found and that it actually points
34
+ // to our drive. If not, default to the drive's root.
35
+ if (path === undefined ||
36
+ path.slice(0, 3).toLowerCase() !== `${resolvedDevice.toLowerCase()}\\`) {
37
+ path = `${resolvedDevice}\\`;
38
+ }
39
+ }
40
+ assertPath(path);
41
+ const len = path.length;
42
+ // Skip empty entries
43
+ if (len === 0)
44
+ continue;
45
+ let rootEnd = 0;
46
+ let device = "";
47
+ let isAbsolute = false;
48
+ const code = path.charCodeAt(0);
49
+ // Try to match a root
50
+ if (len > 1) {
51
+ if (isPathSeparator(code)) {
52
+ // Possible UNC root
53
+ // If we started with a separator, we know we at least have an
54
+ // absolute path of some kind (UNC or otherwise)
55
+ isAbsolute = true;
56
+ if (isPathSeparator(path.charCodeAt(1))) {
57
+ // Matched double path separator at beginning
58
+ let j = 2;
59
+ let last = j;
60
+ // Match 1 or more non-path separators
61
+ for (; j < len; ++j) {
62
+ if (isPathSeparator(path.charCodeAt(j)))
63
+ break;
64
+ }
65
+ if (j < len && j !== last) {
66
+ const firstPart = path.slice(last, j);
67
+ // Matched!
68
+ last = j;
69
+ // Match 1 or more path separators
70
+ for (; j < len; ++j) {
71
+ if (!isPathSeparator(path.charCodeAt(j)))
72
+ break;
73
+ }
74
+ if (j < len && j !== last) {
75
+ // Matched!
76
+ last = j;
77
+ // Match 1 or more non-path separators
78
+ for (; j < len; ++j) {
79
+ if (isPathSeparator(path.charCodeAt(j)))
80
+ break;
81
+ }
82
+ if (j === len) {
83
+ // We matched a UNC root only
84
+ device = `\\\\${firstPart}\\${path.slice(last)}`;
85
+ rootEnd = j;
86
+ }
87
+ else if (j !== last) {
88
+ // We matched a UNC root with leftovers
89
+ device = `\\\\${firstPart}\\${path.slice(last, j)}`;
90
+ rootEnd = j;
91
+ }
92
+ }
93
+ }
94
+ }
95
+ else {
96
+ rootEnd = 1;
97
+ }
98
+ }
99
+ else if (isWindowsDeviceRoot(code)) {
100
+ // Possible device root
101
+ if (path.charCodeAt(1) === CHAR_COLON) {
102
+ device = path.slice(0, 2);
103
+ rootEnd = 2;
104
+ if (len > 2) {
105
+ if (isPathSeparator(path.charCodeAt(2))) {
106
+ // Treat separator following drive name as an absolute path
107
+ // indicator
108
+ isAbsolute = true;
109
+ rootEnd = 3;
110
+ }
111
+ }
112
+ }
113
+ }
114
+ }
115
+ else if (isPathSeparator(code)) {
116
+ // `path` contains just a path separator
117
+ rootEnd = 1;
118
+ isAbsolute = true;
119
+ }
120
+ if (device.length > 0 &&
121
+ resolvedDevice.length > 0 &&
122
+ device.toLowerCase() !== resolvedDevice.toLowerCase()) {
123
+ // This path points to another device so it is not applicable
124
+ continue;
125
+ }
126
+ if (resolvedDevice.length === 0 && device.length > 0) {
127
+ resolvedDevice = device;
128
+ }
129
+ if (!resolvedAbsolute) {
130
+ resolvedTail = `${path.slice(rootEnd)}\\${resolvedTail}`;
131
+ resolvedAbsolute = isAbsolute;
132
+ }
133
+ if (resolvedAbsolute && resolvedDevice.length > 0)
134
+ break;
135
+ }
136
+ // At this point the path should be resolved to a full absolute path,
137
+ // but handle relative paths to be safe (might happen when process.cwd()
138
+ // fails)
139
+ // Normalize the tail path
140
+ resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, "\\", isPathSeparator);
141
+ return resolvedDevice + (resolvedAbsolute ? "\\" : "") + resolvedTail || ".";
142
+ }
143
+ /**
144
+ * Normalizes a `path`
145
+ * @param path to normalize
146
+ */
147
+ export function normalize(path) {
148
+ assertPath(path);
149
+ const len = path.length;
150
+ if (len === 0)
151
+ return ".";
152
+ let rootEnd = 0;
153
+ let device;
154
+ let isAbsolute = false;
155
+ const code = path.charCodeAt(0);
156
+ // Try to match a root
157
+ if (len > 1) {
158
+ if (isPathSeparator(code)) {
159
+ // Possible UNC root
160
+ // If we started with a separator, we know we at least have an absolute
161
+ // path of some kind (UNC or otherwise)
162
+ isAbsolute = true;
163
+ if (isPathSeparator(path.charCodeAt(1))) {
164
+ // Matched double path separator at beginning
165
+ let j = 2;
166
+ let last = j;
167
+ // Match 1 or more non-path separators
168
+ for (; j < len; ++j) {
169
+ if (isPathSeparator(path.charCodeAt(j)))
170
+ break;
171
+ }
172
+ if (j < len && j !== last) {
173
+ const firstPart = path.slice(last, j);
174
+ // Matched!
175
+ last = j;
176
+ // Match 1 or more path separators
177
+ for (; j < len; ++j) {
178
+ if (!isPathSeparator(path.charCodeAt(j)))
179
+ break;
180
+ }
181
+ if (j < len && j !== last) {
182
+ // Matched!
183
+ last = j;
184
+ // Match 1 or more non-path separators
185
+ for (; j < len; ++j) {
186
+ if (isPathSeparator(path.charCodeAt(j)))
187
+ break;
188
+ }
189
+ if (j === len) {
190
+ // We matched a UNC root only
191
+ // Return the normalized version of the UNC root since there
192
+ // is nothing left to process
193
+ return `\\\\${firstPart}\\${path.slice(last)}\\`;
194
+ }
195
+ else if (j !== last) {
196
+ // We matched a UNC root with leftovers
197
+ device = `\\\\${firstPart}\\${path.slice(last, j)}`;
198
+ rootEnd = j;
199
+ }
200
+ }
201
+ }
202
+ }
203
+ else {
204
+ rootEnd = 1;
205
+ }
206
+ }
207
+ else if (isWindowsDeviceRoot(code)) {
208
+ // Possible device root
209
+ if (path.charCodeAt(1) === CHAR_COLON) {
210
+ device = path.slice(0, 2);
211
+ rootEnd = 2;
212
+ if (len > 2) {
213
+ if (isPathSeparator(path.charCodeAt(2))) {
214
+ // Treat separator following drive name as an absolute path
215
+ // indicator
216
+ isAbsolute = true;
217
+ rootEnd = 3;
218
+ }
219
+ }
220
+ }
221
+ }
222
+ }
223
+ else if (isPathSeparator(code)) {
224
+ // `path` contains just a path separator, exit early to avoid unnecessary
225
+ // work
226
+ return "\\";
227
+ }
228
+ let tail;
229
+ if (rootEnd < len) {
230
+ tail = normalizeString(path.slice(rootEnd), !isAbsolute, "\\", isPathSeparator);
231
+ }
232
+ else {
233
+ tail = "";
234
+ }
235
+ if (tail.length === 0 && !isAbsolute)
236
+ tail = ".";
237
+ if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1))) {
238
+ tail += "\\";
239
+ }
240
+ if (device === undefined) {
241
+ if (isAbsolute) {
242
+ if (tail.length > 0)
243
+ return `\\${tail}`;
244
+ else
245
+ return "\\";
246
+ }
247
+ else if (tail.length > 0) {
248
+ return tail;
249
+ }
250
+ else {
251
+ return "";
252
+ }
253
+ }
254
+ else if (isAbsolute) {
255
+ if (tail.length > 0)
256
+ return `${device}\\${tail}`;
257
+ else
258
+ return `${device}\\`;
259
+ }
260
+ else if (tail.length > 0) {
261
+ return device + tail;
262
+ }
263
+ else {
264
+ return device;
265
+ }
266
+ }
267
+ /**
268
+ * Verifies whether path is absolute
269
+ * @param path to verify
270
+ */
271
+ export function isAbsolute(path) {
272
+ assertPath(path);
273
+ const len = path.length;
274
+ if (len === 0)
275
+ return false;
276
+ const code = path.charCodeAt(0);
277
+ if (isPathSeparator(code)) {
278
+ return true;
279
+ }
280
+ else if (isWindowsDeviceRoot(code)) {
281
+ // Possible device root
282
+ if (len > 2 && path.charCodeAt(1) === CHAR_COLON) {
283
+ if (isPathSeparator(path.charCodeAt(2)))
284
+ return true;
285
+ }
286
+ }
287
+ return false;
288
+ }
289
+ /**
290
+ * Join all given a sequence of `paths`,then normalizes the resulting path.
291
+ * @param paths to be joined and normalized
292
+ */
293
+ export function join(...paths) {
294
+ const pathsCount = paths.length;
295
+ if (pathsCount === 0)
296
+ return ".";
297
+ let joined;
298
+ let firstPart = null;
299
+ for (let i = 0; i < pathsCount; ++i) {
300
+ const path = paths[i];
301
+ assertPath(path);
302
+ if (path.length > 0) {
303
+ if (joined === undefined)
304
+ joined = firstPart = path;
305
+ else
306
+ joined += `\\${path}`;
307
+ }
308
+ }
309
+ if (joined === undefined)
310
+ return ".";
311
+ // Make sure that the joined path doesn't start with two slashes, because
312
+ // normalize() will mistake it for an UNC path then.
313
+ //
314
+ // This step is skipped when it is very clear that the user actually
315
+ // intended to point at an UNC path. This is assumed when the first
316
+ // non-empty string arguments starts with exactly two slashes followed by
317
+ // at least one more non-slash character.
318
+ //
319
+ // Note that for normalize() to treat a path as an UNC path it needs to
320
+ // have at least 2 components, so we don't filter for that here.
321
+ // This means that the user can use join to construct UNC paths from
322
+ // a server name and a share name; for example:
323
+ // path.join('//server', 'share') -> '\\\\server\\share\\')
324
+ let needsReplace = true;
325
+ let slashCount = 0;
326
+ assert(firstPart != null);
327
+ if (isPathSeparator(firstPart.charCodeAt(0))) {
328
+ ++slashCount;
329
+ const firstLen = firstPart.length;
330
+ if (firstLen > 1) {
331
+ if (isPathSeparator(firstPart.charCodeAt(1))) {
332
+ ++slashCount;
333
+ if (firstLen > 2) {
334
+ if (isPathSeparator(firstPart.charCodeAt(2)))
335
+ ++slashCount;
336
+ else {
337
+ // We matched a UNC path in the first part
338
+ needsReplace = false;
339
+ }
340
+ }
341
+ }
342
+ }
343
+ }
344
+ if (needsReplace) {
345
+ // Find any more consecutive slashes we need to replace
346
+ for (; slashCount < joined.length; ++slashCount) {
347
+ if (!isPathSeparator(joined.charCodeAt(slashCount)))
348
+ break;
349
+ }
350
+ // Replace the slashes if needed
351
+ if (slashCount >= 2)
352
+ joined = `\\${joined.slice(slashCount)}`;
353
+ }
354
+ return normalize(joined);
355
+ }
356
+ /**
357
+ * It will solve the relative path from `from` to `to`, for instance:
358
+ * from = 'C:\\orandea\\test\\aaa'
359
+ * to = 'C:\\orandea\\impl\\bbb'
360
+ * The output of the function should be: '..\\..\\impl\\bbb'
361
+ * @param from relative path
362
+ * @param to relative path
363
+ */
364
+ export function relative(from, to) {
365
+ assertPath(from);
366
+ assertPath(to);
367
+ if (from === to)
368
+ return "";
369
+ const fromOrig = resolve(from);
370
+ const toOrig = resolve(to);
371
+ if (fromOrig === toOrig)
372
+ return "";
373
+ from = fromOrig.toLowerCase();
374
+ to = toOrig.toLowerCase();
375
+ if (from === to)
376
+ return "";
377
+ // Trim any leading backslashes
378
+ let fromStart = 0;
379
+ let fromEnd = from.length;
380
+ for (; fromStart < fromEnd; ++fromStart) {
381
+ if (from.charCodeAt(fromStart) !== CHAR_BACKWARD_SLASH)
382
+ break;
383
+ }
384
+ // Trim trailing backslashes (applicable to UNC paths only)
385
+ for (; fromEnd - 1 > fromStart; --fromEnd) {
386
+ if (from.charCodeAt(fromEnd - 1) !== CHAR_BACKWARD_SLASH)
387
+ break;
388
+ }
389
+ const fromLen = fromEnd - fromStart;
390
+ // Trim any leading backslashes
391
+ let toStart = 0;
392
+ let toEnd = to.length;
393
+ for (; toStart < toEnd; ++toStart) {
394
+ if (to.charCodeAt(toStart) !== CHAR_BACKWARD_SLASH)
395
+ break;
396
+ }
397
+ // Trim trailing backslashes (applicable to UNC paths only)
398
+ for (; toEnd - 1 > toStart; --toEnd) {
399
+ if (to.charCodeAt(toEnd - 1) !== CHAR_BACKWARD_SLASH)
400
+ break;
401
+ }
402
+ const toLen = toEnd - toStart;
403
+ // Compare paths to find the longest common path from root
404
+ const length = fromLen < toLen ? fromLen : toLen;
405
+ let lastCommonSep = -1;
406
+ let i = 0;
407
+ for (; i <= length; ++i) {
408
+ if (i === length) {
409
+ if (toLen > length) {
410
+ if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
411
+ // We get here if `from` is the exact base path for `to`.
412
+ // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
413
+ return toOrig.slice(toStart + i + 1);
414
+ }
415
+ else if (i === 2) {
416
+ // We get here if `from` is the device root.
417
+ // For example: from='C:\\'; to='C:\\foo'
418
+ return toOrig.slice(toStart + i);
419
+ }
420
+ }
421
+ if (fromLen > length) {
422
+ if (from.charCodeAt(fromStart + i) === CHAR_BACKWARD_SLASH) {
423
+ // We get here if `to` is the exact base path for `from`.
424
+ // For example: from='C:\\foo\\bar'; to='C:\\foo'
425
+ lastCommonSep = i;
426
+ }
427
+ else if (i === 2) {
428
+ // We get here if `to` is the device root.
429
+ // For example: from='C:\\foo\\bar'; to='C:\\'
430
+ lastCommonSep = 3;
431
+ }
432
+ }
433
+ break;
434
+ }
435
+ const fromCode = from.charCodeAt(fromStart + i);
436
+ const toCode = to.charCodeAt(toStart + i);
437
+ if (fromCode !== toCode)
438
+ break;
439
+ else if (fromCode === CHAR_BACKWARD_SLASH)
440
+ lastCommonSep = i;
441
+ }
442
+ // We found a mismatch before the first common path separator was seen, so
443
+ // return the original `to`.
444
+ if (i !== length && lastCommonSep === -1) {
445
+ return toOrig;
446
+ }
447
+ let out = "";
448
+ if (lastCommonSep === -1)
449
+ lastCommonSep = 0;
450
+ // Generate the relative path based on the path difference between `to` and
451
+ // `from`
452
+ for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
453
+ if (i === fromEnd || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
454
+ if (out.length === 0)
455
+ out += "..";
456
+ else
457
+ out += "\\..";
458
+ }
459
+ }
460
+ // Lastly, append the rest of the destination (`to`) path that comes after
461
+ // the common path parts
462
+ if (out.length > 0) {
463
+ return out + toOrig.slice(toStart + lastCommonSep, toEnd);
464
+ }
465
+ else {
466
+ toStart += lastCommonSep;
467
+ if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH)
468
+ ++toStart;
469
+ return toOrig.slice(toStart, toEnd);
470
+ }
471
+ }
472
+ /**
473
+ * Resolves path to a namespace path
474
+ * @param path to resolve to namespace
475
+ */
476
+ export function toNamespacedPath(path) {
477
+ // Note: this will *probably* throw somewhere.
478
+ if (typeof path !== "string")
479
+ return path;
480
+ if (path.length === 0)
481
+ return "";
482
+ const resolvedPath = resolve(path);
483
+ if (resolvedPath.length >= 3) {
484
+ if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) {
485
+ // Possible UNC root
486
+ if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) {
487
+ const code = resolvedPath.charCodeAt(2);
488
+ if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) {
489
+ // Matched non-long UNC root, convert the path to a long UNC path
490
+ return `\\\\?\\UNC\\${resolvedPath.slice(2)}`;
491
+ }
492
+ }
493
+ }
494
+ else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0))) {
495
+ // Possible device root
496
+ if (resolvedPath.charCodeAt(1) === CHAR_COLON &&
497
+ resolvedPath.charCodeAt(2) === CHAR_BACKWARD_SLASH) {
498
+ // Matched device root, convert the path to a long UNC path
499
+ return `\\\\?\\${resolvedPath}`;
500
+ }
501
+ }
502
+ }
503
+ return path;
504
+ }
505
+ /**
506
+ * Return the directory name of a `path`.
507
+ * @param path to determine name for
508
+ */
509
+ export function dirname(path) {
510
+ assertPath(path);
511
+ const len = path.length;
512
+ if (len === 0)
513
+ return ".";
514
+ let rootEnd = -1;
515
+ let end = -1;
516
+ let matchedSlash = true;
517
+ let offset = 0;
518
+ const code = path.charCodeAt(0);
519
+ // Try to match a root
520
+ if (len > 1) {
521
+ if (isPathSeparator(code)) {
522
+ // Possible UNC root
523
+ rootEnd = offset = 1;
524
+ if (isPathSeparator(path.charCodeAt(1))) {
525
+ // Matched double path separator at beginning
526
+ let j = 2;
527
+ let last = j;
528
+ // Match 1 or more non-path separators
529
+ for (; j < len; ++j) {
530
+ if (isPathSeparator(path.charCodeAt(j)))
531
+ break;
532
+ }
533
+ if (j < len && j !== last) {
534
+ // Matched!
535
+ last = j;
536
+ // Match 1 or more path separators
537
+ for (; j < len; ++j) {
538
+ if (!isPathSeparator(path.charCodeAt(j)))
539
+ break;
540
+ }
541
+ if (j < len && j !== last) {
542
+ // Matched!
543
+ last = j;
544
+ // Match 1 or more non-path separators
545
+ for (; j < len; ++j) {
546
+ if (isPathSeparator(path.charCodeAt(j)))
547
+ break;
548
+ }
549
+ if (j === len) {
550
+ // We matched a UNC root only
551
+ return path;
552
+ }
553
+ if (j !== last) {
554
+ // We matched a UNC root with leftovers
555
+ // Offset by 1 to include the separator after the UNC root to
556
+ // treat it as a "normal root" on top of a (UNC) root
557
+ rootEnd = offset = j + 1;
558
+ }
559
+ }
560
+ }
561
+ }
562
+ }
563
+ else if (isWindowsDeviceRoot(code)) {
564
+ // Possible device root
565
+ if (path.charCodeAt(1) === CHAR_COLON) {
566
+ rootEnd = offset = 2;
567
+ if (len > 2) {
568
+ if (isPathSeparator(path.charCodeAt(2)))
569
+ rootEnd = offset = 3;
570
+ }
571
+ }
572
+ }
573
+ }
574
+ else if (isPathSeparator(code)) {
575
+ // `path` contains just a path separator, exit early to avoid
576
+ // unnecessary work
577
+ return path;
578
+ }
579
+ for (let i = len - 1; i >= offset; --i) {
580
+ if (isPathSeparator(path.charCodeAt(i))) {
581
+ if (!matchedSlash) {
582
+ end = i;
583
+ break;
584
+ }
585
+ }
586
+ else {
587
+ // We saw the first non-path separator
588
+ matchedSlash = false;
589
+ }
590
+ }
591
+ if (end === -1) {
592
+ if (rootEnd === -1)
593
+ return ".";
594
+ else
595
+ end = rootEnd;
596
+ }
597
+ return path.slice(0, end);
598
+ }
599
+ /**
600
+ * Return the last portion of a `path`. Trailing directory separators are ignored.
601
+ * @param path to process
602
+ * @param ext of path directory
603
+ */
604
+ export function basename(path, ext = "") {
605
+ if (ext !== undefined && typeof ext !== "string") {
606
+ throw new TypeError('"ext" argument must be a string');
607
+ }
608
+ assertPath(path);
609
+ let start = 0;
610
+ let end = -1;
611
+ let matchedSlash = true;
612
+ let i;
613
+ // Check for a drive letter prefix so as not to mistake the following
614
+ // path separator as an extra separator at the end of the path that can be
615
+ // disregarded
616
+ if (path.length >= 2) {
617
+ const drive = path.charCodeAt(0);
618
+ if (isWindowsDeviceRoot(drive)) {
619
+ if (path.charCodeAt(1) === CHAR_COLON)
620
+ start = 2;
621
+ }
622
+ }
623
+ if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
624
+ if (ext.length === path.length && ext === path)
625
+ return "";
626
+ let extIdx = ext.length - 1;
627
+ let firstNonSlashEnd = -1;
628
+ for (i = path.length - 1; i >= start; --i) {
629
+ const code = path.charCodeAt(i);
630
+ if (isPathSeparator(code)) {
631
+ // If we reached a path separator that was not part of a set of path
632
+ // separators at the end of the string, stop now
633
+ if (!matchedSlash) {
634
+ start = i + 1;
635
+ break;
636
+ }
637
+ }
638
+ else {
639
+ if (firstNonSlashEnd === -1) {
640
+ // We saw the first non-path separator, remember this index in case
641
+ // we need it if the extension ends up not matching
642
+ matchedSlash = false;
643
+ firstNonSlashEnd = i + 1;
644
+ }
645
+ if (extIdx >= 0) {
646
+ // Try to match the explicit extension
647
+ if (code === ext.charCodeAt(extIdx)) {
648
+ if (--extIdx === -1) {
649
+ // We matched the extension, so mark this as the end of our path
650
+ // component
651
+ end = i;
652
+ }
653
+ }
654
+ else {
655
+ // Extension does not match, so our result is the entire path
656
+ // component
657
+ extIdx = -1;
658
+ end = firstNonSlashEnd;
659
+ }
660
+ }
661
+ }
662
+ }
663
+ if (start === end)
664
+ end = firstNonSlashEnd;
665
+ else if (end === -1)
666
+ end = path.length;
667
+ return path.slice(start, end);
668
+ }
669
+ else {
670
+ for (i = path.length - 1; i >= start; --i) {
671
+ if (isPathSeparator(path.charCodeAt(i))) {
672
+ // If we reached a path separator that was not part of a set of path
673
+ // separators at the end of the string, stop now
674
+ if (!matchedSlash) {
675
+ start = i + 1;
676
+ break;
677
+ }
678
+ }
679
+ else if (end === -1) {
680
+ // We saw the first non-path separator, mark this as the end of our
681
+ // path component
682
+ matchedSlash = false;
683
+ end = i + 1;
684
+ }
685
+ }
686
+ if (end === -1)
687
+ return "";
688
+ return path.slice(start, end);
689
+ }
690
+ }
691
+ /**
692
+ * Return the extension of the `path`.
693
+ * @param path with extension
694
+ */
695
+ export function extname(path) {
696
+ assertPath(path);
697
+ let start = 0;
698
+ let startDot = -1;
699
+ let startPart = 0;
700
+ let end = -1;
701
+ let matchedSlash = true;
702
+ // Track the state of characters (if any) we see before our first dot and
703
+ // after any path separator we find
704
+ let preDotState = 0;
705
+ // Check for a drive letter prefix so as not to mistake the following
706
+ // path separator as an extra separator at the end of the path that can be
707
+ // disregarded
708
+ if (path.length >= 2 &&
709
+ path.charCodeAt(1) === CHAR_COLON &&
710
+ isWindowsDeviceRoot(path.charCodeAt(0))) {
711
+ start = startPart = 2;
712
+ }
713
+ for (let i = path.length - 1; i >= start; --i) {
714
+ const code = path.charCodeAt(i);
715
+ if (isPathSeparator(code)) {
716
+ // If we reached a path separator that was not part of a set of path
717
+ // separators at the end of the string, stop now
718
+ if (!matchedSlash) {
719
+ startPart = i + 1;
720
+ break;
721
+ }
722
+ continue;
723
+ }
724
+ if (end === -1) {
725
+ // We saw the first non-path separator, mark this as the end of our
726
+ // extension
727
+ matchedSlash = false;
728
+ end = i + 1;
729
+ }
730
+ if (code === CHAR_DOT) {
731
+ // If this is our first dot, mark it as the start of our extension
732
+ if (startDot === -1)
733
+ startDot = i;
734
+ else if (preDotState !== 1)
735
+ preDotState = 1;
736
+ }
737
+ else if (startDot !== -1) {
738
+ // We saw a non-dot and non-path separator before our dot, so we should
739
+ // have a good chance at having a non-empty extension
740
+ preDotState = -1;
741
+ }
742
+ }
743
+ if (startDot === -1 ||
744
+ end === -1 ||
745
+ // We saw a non-dot character immediately before the dot
746
+ preDotState === 0 ||
747
+ // The (right-most) trimmed path component is exactly '..'
748
+ (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)) {
749
+ return "";
750
+ }
751
+ return path.slice(startDot, end);
752
+ }
753
+ /**
754
+ * Generate a path from `FormatInputPathObject` object.
755
+ * @param pathObject with path
756
+ */
757
+ export function format(pathObject) {
758
+ if (pathObject === null || typeof pathObject !== "object") {
759
+ throw new TypeError(`The "pathObject" argument must be of type Object. Received type ${typeof pathObject}`);
760
+ }
761
+ return _format("\\", pathObject);
762
+ }
763
+ /**
764
+ * Return a `ParsedPath` object of the `path`.
765
+ * @param path to process
766
+ */
767
+ export function parse(path) {
768
+ assertPath(path);
769
+ const ret = { root: "", dir: "", base: "", ext: "", name: "" };
770
+ const len = path.length;
771
+ if (len === 0)
772
+ return ret;
773
+ let rootEnd = 0;
774
+ let code = path.charCodeAt(0);
775
+ // Try to match a root
776
+ if (len > 1) {
777
+ if (isPathSeparator(code)) {
778
+ // Possible UNC root
779
+ rootEnd = 1;
780
+ if (isPathSeparator(path.charCodeAt(1))) {
781
+ // Matched double path separator at beginning
782
+ let j = 2;
783
+ let last = j;
784
+ // Match 1 or more non-path separators
785
+ for (; j < len; ++j) {
786
+ if (isPathSeparator(path.charCodeAt(j)))
787
+ break;
788
+ }
789
+ if (j < len && j !== last) {
790
+ // Matched!
791
+ last = j;
792
+ // Match 1 or more path separators
793
+ for (; j < len; ++j) {
794
+ if (!isPathSeparator(path.charCodeAt(j)))
795
+ break;
796
+ }
797
+ if (j < len && j !== last) {
798
+ // Matched!
799
+ last = j;
800
+ // Match 1 or more non-path separators
801
+ for (; j < len; ++j) {
802
+ if (isPathSeparator(path.charCodeAt(j)))
803
+ break;
804
+ }
805
+ if (j === len) {
806
+ // We matched a UNC root only
807
+ rootEnd = j;
808
+ }
809
+ else if (j !== last) {
810
+ // We matched a UNC root with leftovers
811
+ rootEnd = j + 1;
812
+ }
813
+ }
814
+ }
815
+ }
816
+ }
817
+ else if (isWindowsDeviceRoot(code)) {
818
+ // Possible device root
819
+ if (path.charCodeAt(1) === CHAR_COLON) {
820
+ rootEnd = 2;
821
+ if (len > 2) {
822
+ if (isPathSeparator(path.charCodeAt(2))) {
823
+ if (len === 3) {
824
+ // `path` contains just a drive root, exit early to avoid
825
+ // unnecessary work
826
+ ret.root = ret.dir = path;
827
+ return ret;
828
+ }
829
+ rootEnd = 3;
830
+ }
831
+ }
832
+ else {
833
+ // `path` contains just a drive root, exit early to avoid
834
+ // unnecessary work
835
+ ret.root = ret.dir = path;
836
+ return ret;
837
+ }
838
+ }
839
+ }
840
+ }
841
+ else if (isPathSeparator(code)) {
842
+ // `path` contains just a path separator, exit early to avoid
843
+ // unnecessary work
844
+ ret.root = ret.dir = path;
845
+ return ret;
846
+ }
847
+ if (rootEnd > 0)
848
+ ret.root = path.slice(0, rootEnd);
849
+ let startDot = -1;
850
+ let startPart = rootEnd;
851
+ let end = -1;
852
+ let matchedSlash = true;
853
+ let i = path.length - 1;
854
+ // Track the state of characters (if any) we see before our first dot and
855
+ // after any path separator we find
856
+ let preDotState = 0;
857
+ // Get non-dir info
858
+ for (; i >= rootEnd; --i) {
859
+ code = path.charCodeAt(i);
860
+ if (isPathSeparator(code)) {
861
+ // If we reached a path separator that was not part of a set of path
862
+ // separators at the end of the string, stop now
863
+ if (!matchedSlash) {
864
+ startPart = i + 1;
865
+ break;
866
+ }
867
+ continue;
868
+ }
869
+ if (end === -1) {
870
+ // We saw the first non-path separator, mark this as the end of our
871
+ // extension
872
+ matchedSlash = false;
873
+ end = i + 1;
874
+ }
875
+ if (code === CHAR_DOT) {
876
+ // If this is our first dot, mark it as the start of our extension
877
+ if (startDot === -1)
878
+ startDot = i;
879
+ else if (preDotState !== 1)
880
+ preDotState = 1;
881
+ }
882
+ else if (startDot !== -1) {
883
+ // We saw a non-dot and non-path separator before our dot, so we should
884
+ // have a good chance at having a non-empty extension
885
+ preDotState = -1;
886
+ }
887
+ }
888
+ if (startDot === -1 ||
889
+ end === -1 ||
890
+ // We saw a non-dot character immediately before the dot
891
+ preDotState === 0 ||
892
+ // The (right-most) trimmed path component is exactly '..'
893
+ (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)) {
894
+ if (end !== -1) {
895
+ ret.base = ret.name = path.slice(startPart, end);
896
+ }
897
+ }
898
+ else {
899
+ ret.name = path.slice(startPart, startDot);
900
+ ret.base = path.slice(startPart, end);
901
+ ret.ext = path.slice(startDot, end);
902
+ }
903
+ // If the directory is the root, use the entire root as the `dir` including
904
+ // the trailing slash if any (`C:\abc` -> `C:\`). Otherwise, strip out the
905
+ // trailing slash (`C:\abc\def` -> `C:\abc`).
906
+ if (startPart > 0 && startPart !== rootEnd) {
907
+ ret.dir = path.slice(0, startPart - 1);
908
+ }
909
+ else
910
+ ret.dir = ret.root;
911
+ return ret;
912
+ }
913
+ /**
914
+ * Converts a file URL to a path string.
915
+ *
916
+ * ```ts
917
+ * import { fromFileUrl } from "./win32.ts";
918
+ * fromFileUrl("file:///home/foo"); // "\\home\\foo"
919
+ * fromFileUrl("file:///C:/Users/foo"); // "C:\\Users\\foo"
920
+ * fromFileUrl("file://localhost/home/foo"); // "\\\\localhost\\home\\foo"
921
+ * ```
922
+ * @param url of a file URL
923
+ */
924
+ export function fromFileUrl(url) {
925
+ url = url instanceof URL ? url : new URL(url);
926
+ if (url.protocol != "file:") {
927
+ throw new TypeError("Must be a file URL.");
928
+ }
929
+ let path = decodeURIComponent(url.pathname.replace(/\//g, "\\").replace(/%(?![0-9A-Fa-f]{2})/g, "%25")).replace(/^\\*([A-Za-z]:)(\\|$)/, "$1\\");
930
+ if (url.hostname != "") {
931
+ // Note: The `URL` implementation guarantees that the drive letter and
932
+ // hostname are mutually exclusive. Otherwise it would not have been valid
933
+ // to append the hostname and path like this.
934
+ path = `\\\\${url.hostname}${path}`;
935
+ }
936
+ return path;
937
+ }
938
+ /**
939
+ * Converts a path string to a file URL.
940
+ *
941
+ * ```ts
942
+ * import { toFileUrl } from "./win32.ts";
943
+ * toFileUrl("\\home\\foo"); // new URL("file:///home/foo")
944
+ * toFileUrl("C:\\Users\\foo"); // new URL("file:///C:/Users/foo")
945
+ * toFileUrl("\\\\127.0.0.1\\home\\foo"); // new URL("file://127.0.0.1/home/foo")
946
+ * ```
947
+ * @param path to convert to file URL
948
+ */
949
+ export function toFileUrl(path) {
950
+ if (!isAbsolute(path)) {
951
+ throw new TypeError("Must be an absolute path.");
952
+ }
953
+ const [, hostname, pathname] = path.match(/^(?:[/\\]{2}([^/\\]+)(?=[/\\](?:[^/\\]|$)))?(.*)/);
954
+ const url = new URL("file:///");
955
+ url.pathname = encodeWhitespace(pathname.replace(/%/g, "%25"));
956
+ if (hostname != null && hostname != "localhost") {
957
+ url.hostname = hostname;
958
+ if (!url.hostname) {
959
+ throw new TypeError("Invalid hostname.");
960
+ }
961
+ }
962
+ return url;
963
+ }