path.js 52 KB

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