index.js 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.PathScurry = exports.Path = exports.PathScurryDarwin = exports.PathScurryPosix = exports.PathScurryWin32 = exports.PathScurryBase = exports.PathPosix = exports.PathWin32 = exports.PathBase = exports.ChildrenCache = exports.ResolveCache = void 0;
  27. const lru_cache_1 = require("lru-cache");
  28. const node_path_1 = require("node:path");
  29. const node_url_1 = require("node:url");
  30. const fs_1 = require("fs");
  31. const actualFS = __importStar(require("node:fs"));
  32. const realpathSync = fs_1.realpathSync.native;
  33. // TODO: test perf of fs/promises realpath vs realpathCB,
  34. // since the promises one uses realpath.native
  35. const promises_1 = require("node:fs/promises");
  36. const minipass_1 = require("minipass");
  37. const defaultFS = {
  38. lstatSync: fs_1.lstatSync,
  39. readdir: fs_1.readdir,
  40. readdirSync: fs_1.readdirSync,
  41. readlinkSync: fs_1.readlinkSync,
  42. realpathSync,
  43. promises: {
  44. lstat: promises_1.lstat,
  45. readdir: promises_1.readdir,
  46. readlink: promises_1.readlink,
  47. realpath: promises_1.realpath,
  48. },
  49. };
  50. // if they just gave us require('fs') then use our default
  51. const fsFromOption = (fsOption) => !fsOption || fsOption === defaultFS || fsOption === actualFS ?
  52. defaultFS
  53. : {
  54. ...defaultFS,
  55. ...fsOption,
  56. promises: {
  57. ...defaultFS.promises,
  58. ...(fsOption.promises || {}),
  59. },
  60. };
  61. // turn something like //?/c:/ into c:\
  62. const uncDriveRegexp = /^\\\\\?\\([a-z]:)\\?$/i;
  63. const uncToDrive = (rootPath) => rootPath.replace(/\//g, '\\').replace(uncDriveRegexp, '$1\\');
  64. // windows paths are separated by either / or \
  65. const eitherSep = /[\\\/]/;
  66. const UNKNOWN = 0; // may not even exist, for all we know
  67. const IFIFO = 0b0001;
  68. const IFCHR = 0b0010;
  69. const IFDIR = 0b0100;
  70. const IFBLK = 0b0110;
  71. const IFREG = 0b1000;
  72. const IFLNK = 0b1010;
  73. const IFSOCK = 0b1100;
  74. const IFMT = 0b1111;
  75. // mask to unset low 4 bits
  76. const IFMT_UNKNOWN = ~IFMT;
  77. // set after successfully calling readdir() and getting entries.
  78. const READDIR_CALLED = 0b0000_0001_0000;
  79. // set after a successful lstat()
  80. const LSTAT_CALLED = 0b0000_0010_0000;
  81. // set if an entry (or one of its parents) is definitely not a dir
  82. const ENOTDIR = 0b0000_0100_0000;
  83. // set if an entry (or one of its parents) does not exist
  84. // (can also be set on lstat errors like EACCES or ENAMETOOLONG)
  85. const ENOENT = 0b0000_1000_0000;
  86. // cannot have child entries -- also verify &IFMT is either IFDIR or IFLNK
  87. // set if we fail to readlink
  88. const ENOREADLINK = 0b0001_0000_0000;
  89. // set if we know realpath() will fail
  90. const ENOREALPATH = 0b0010_0000_0000;
  91. const ENOCHILD = ENOTDIR | ENOENT | ENOREALPATH;
  92. const TYPEMASK = 0b0011_1111_1111;
  93. const entToType = (s) => s.isFile() ? IFREG
  94. : s.isDirectory() ? IFDIR
  95. : s.isSymbolicLink() ? IFLNK
  96. : s.isCharacterDevice() ? IFCHR
  97. : s.isBlockDevice() ? IFBLK
  98. : s.isSocket() ? IFSOCK
  99. : s.isFIFO() ? IFIFO
  100. : UNKNOWN;
  101. // normalize unicode path names
  102. const normalizeCache = new Map();
  103. const normalize = (s) => {
  104. const c = normalizeCache.get(s);
  105. if (c)
  106. return c;
  107. const n = s.normalize('NFKD');
  108. normalizeCache.set(s, n);
  109. return n;
  110. };
  111. const normalizeNocaseCache = new Map();
  112. const normalizeNocase = (s) => {
  113. const c = normalizeNocaseCache.get(s);
  114. if (c)
  115. return c;
  116. const n = normalize(s.toLowerCase());
  117. normalizeNocaseCache.set(s, n);
  118. return n;
  119. };
  120. /**
  121. * An LRUCache for storing resolved path strings or Path objects.
  122. * @internal
  123. */
  124. class ResolveCache extends lru_cache_1.LRUCache {
  125. constructor() {
  126. super({ max: 256 });
  127. }
  128. }
  129. exports.ResolveCache = ResolveCache;
  130. // In order to prevent blowing out the js heap by allocating hundreds of
  131. // thousands of Path entries when walking extremely large trees, the "children"
  132. // in this tree are represented by storing an array of Path entries in an
  133. // LRUCache, indexed by the parent. At any time, Path.children() may return an
  134. // empty array, indicating that it doesn't know about any of its children, and
  135. // thus has to rebuild that cache. This is fine, it just means that we don't
  136. // benefit as much from having the cached entries, but huge directory walks
  137. // don't blow out the stack, and smaller ones are still as fast as possible.
  138. //
  139. //It does impose some complexity when building up the readdir data, because we
  140. //need to pass a reference to the children array that we started with.
  141. /**
  142. * an LRUCache for storing child entries.
  143. * @internal
  144. */
  145. class ChildrenCache extends lru_cache_1.LRUCache {
  146. constructor(maxSize = 16 * 1024) {
  147. super({
  148. maxSize,
  149. // parent + children
  150. sizeCalculation: a => a.length + 1,
  151. });
  152. }
  153. }
  154. exports.ChildrenCache = ChildrenCache;
  155. const setAsCwd = Symbol('PathScurry setAsCwd');
  156. /**
  157. * Path objects are sort of like a super-powered
  158. * {@link https://nodejs.org/docs/latest/api/fs.html#class-fsdirent fs.Dirent}
  159. *
  160. * Each one represents a single filesystem entry on disk, which may or may not
  161. * exist. It includes methods for reading various types of information via
  162. * lstat, readlink, and readdir, and caches all information to the greatest
  163. * degree possible.
  164. *
  165. * Note that fs operations that would normally throw will instead return an
  166. * "empty" value. This is in order to prevent excessive overhead from error
  167. * stack traces.
  168. */
  169. class PathBase {
  170. /**
  171. * the basename of this path
  172. *
  173. * **Important**: *always* test the path name against any test string
  174. * usingthe {@link isNamed} method, and not by directly comparing this
  175. * string. Otherwise, unicode path strings that the system sees as identical
  176. * will not be properly treated as the same path, leading to incorrect
  177. * behavior and possible security issues.
  178. */
  179. name;
  180. /**
  181. * the Path entry corresponding to the path root.
  182. *
  183. * @internal
  184. */
  185. root;
  186. /**
  187. * All roots found within the current PathScurry family
  188. *
  189. * @internal
  190. */
  191. roots;
  192. /**
  193. * a reference to the parent path, or undefined in the case of root entries
  194. *
  195. * @internal
  196. */
  197. parent;
  198. /**
  199. * boolean indicating whether paths are compared case-insensitively
  200. * @internal
  201. */
  202. nocase;
  203. /**
  204. * boolean indicating that this path is the current working directory
  205. * of the PathScurry collection that contains it.
  206. */
  207. isCWD = false;
  208. // potential default fs override
  209. #fs;
  210. // Stats fields
  211. #dev;
  212. get dev() {
  213. return this.#dev;
  214. }
  215. #mode;
  216. get mode() {
  217. return this.#mode;
  218. }
  219. #nlink;
  220. get nlink() {
  221. return this.#nlink;
  222. }
  223. #uid;
  224. get uid() {
  225. return this.#uid;
  226. }
  227. #gid;
  228. get gid() {
  229. return this.#gid;
  230. }
  231. #rdev;
  232. get rdev() {
  233. return this.#rdev;
  234. }
  235. #blksize;
  236. get blksize() {
  237. return this.#blksize;
  238. }
  239. #ino;
  240. get ino() {
  241. return this.#ino;
  242. }
  243. #size;
  244. get size() {
  245. return this.#size;
  246. }
  247. #blocks;
  248. get blocks() {
  249. return this.#blocks;
  250. }
  251. #atimeMs;
  252. get atimeMs() {
  253. return this.#atimeMs;
  254. }
  255. #mtimeMs;
  256. get mtimeMs() {
  257. return this.#mtimeMs;
  258. }
  259. #ctimeMs;
  260. get ctimeMs() {
  261. return this.#ctimeMs;
  262. }
  263. #birthtimeMs;
  264. get birthtimeMs() {
  265. return this.#birthtimeMs;
  266. }
  267. #atime;
  268. get atime() {
  269. return this.#atime;
  270. }
  271. #mtime;
  272. get mtime() {
  273. return this.#mtime;
  274. }
  275. #ctime;
  276. get ctime() {
  277. return this.#ctime;
  278. }
  279. #birthtime;
  280. get birthtime() {
  281. return this.#birthtime;
  282. }
  283. #matchName;
  284. #depth;
  285. #fullpath;
  286. #fullpathPosix;
  287. #relative;
  288. #relativePosix;
  289. #type;
  290. #children;
  291. #linkTarget;
  292. #realpath;
  293. /**
  294. * This property is for compatibility with the Dirent class as of
  295. * Node v20, where Dirent['parentPath'] refers to the path of the
  296. * directory that was passed to readdir. For root entries, it's the path
  297. * to the entry itself.
  298. */
  299. get parentPath() {
  300. return (this.parent || this).fullpath();
  301. }
  302. /**
  303. * Deprecated alias for Dirent['parentPath'] Somewhat counterintuitively,
  304. * this property refers to the *parent* path, not the path object itself.
  305. */
  306. get path() {
  307. return this.parentPath;
  308. }
  309. /**
  310. * Do not create new Path objects directly. They should always be accessed
  311. * via the PathScurry class or other methods on the Path class.
  312. *
  313. * @internal
  314. */
  315. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  316. this.name = name;
  317. this.#matchName = nocase ? normalizeNocase(name) : normalize(name);
  318. this.#type = type & TYPEMASK;
  319. this.nocase = nocase;
  320. this.roots = roots;
  321. this.root = root || this;
  322. this.#children = children;
  323. this.#fullpath = opts.fullpath;
  324. this.#relative = opts.relative;
  325. this.#relativePosix = opts.relativePosix;
  326. this.parent = opts.parent;
  327. if (this.parent) {
  328. this.#fs = this.parent.#fs;
  329. }
  330. else {
  331. this.#fs = fsFromOption(opts.fs);
  332. }
  333. }
  334. /**
  335. * Returns the depth of the Path object from its root.
  336. *
  337. * For example, a path at `/foo/bar` would have a depth of 2.
  338. */
  339. depth() {
  340. if (this.#depth !== undefined)
  341. return this.#depth;
  342. if (!this.parent)
  343. return (this.#depth = 0);
  344. return (this.#depth = this.parent.depth() + 1);
  345. }
  346. /**
  347. * @internal
  348. */
  349. childrenCache() {
  350. return this.#children;
  351. }
  352. /**
  353. * Get the Path object referenced by the string path, resolved from this Path
  354. */
  355. resolve(path) {
  356. if (!path) {
  357. return this;
  358. }
  359. const rootPath = this.getRootString(path);
  360. const dir = path.substring(rootPath.length);
  361. const dirParts = dir.split(this.splitSep);
  362. const result = rootPath ?
  363. this.getRoot(rootPath).#resolveParts(dirParts)
  364. : this.#resolveParts(dirParts);
  365. return result;
  366. }
  367. #resolveParts(dirParts) {
  368. let p = this;
  369. for (const part of dirParts) {
  370. p = p.child(part);
  371. }
  372. return p;
  373. }
  374. /**
  375. * Returns the cached children Path objects, if still available. If they
  376. * have fallen out of the cache, then returns an empty array, and resets the
  377. * READDIR_CALLED bit, so that future calls to readdir() will require an fs
  378. * lookup.
  379. *
  380. * @internal
  381. */
  382. children() {
  383. const cached = this.#children.get(this);
  384. if (cached) {
  385. return cached;
  386. }
  387. const children = Object.assign([], { provisional: 0 });
  388. this.#children.set(this, children);
  389. this.#type &= ~READDIR_CALLED;
  390. return children;
  391. }
  392. /**
  393. * Resolves a path portion and returns or creates the child Path.
  394. *
  395. * Returns `this` if pathPart is `''` or `'.'`, or `parent` if pathPart is
  396. * `'..'`.
  397. *
  398. * This should not be called directly. If `pathPart` contains any path
  399. * separators, it will lead to unsafe undefined behavior.
  400. *
  401. * Use `Path.resolve()` instead.
  402. *
  403. * @internal
  404. */
  405. child(pathPart, opts) {
  406. if (pathPart === '' || pathPart === '.') {
  407. return this;
  408. }
  409. if (pathPart === '..') {
  410. return this.parent || this;
  411. }
  412. // find the child
  413. const children = this.children();
  414. const name = this.nocase ? normalizeNocase(pathPart) : normalize(pathPart);
  415. for (const p of children) {
  416. if (p.#matchName === name) {
  417. return p;
  418. }
  419. }
  420. // didn't find it, create provisional child, since it might not
  421. // actually exist. If we know the parent isn't a dir, then
  422. // in fact it CAN'T exist.
  423. const s = this.parent ? this.sep : '';
  424. const fullpath = this.#fullpath ? this.#fullpath + s + pathPart : undefined;
  425. const pchild = this.newChild(pathPart, UNKNOWN, {
  426. ...opts,
  427. parent: this,
  428. fullpath,
  429. });
  430. if (!this.canReaddir()) {
  431. pchild.#type |= ENOENT;
  432. }
  433. // don't have to update provisional, because if we have real children,
  434. // then provisional is set to children.length, otherwise a lower number
  435. children.push(pchild);
  436. return pchild;
  437. }
  438. /**
  439. * The relative path from the cwd. If it does not share an ancestor with
  440. * the cwd, then this ends up being equivalent to the fullpath()
  441. */
  442. relative() {
  443. if (this.isCWD)
  444. return '';
  445. if (this.#relative !== undefined) {
  446. return this.#relative;
  447. }
  448. const name = this.name;
  449. const p = this.parent;
  450. if (!p) {
  451. return (this.#relative = this.name);
  452. }
  453. const pv = p.relative();
  454. return pv + (!pv || !p.parent ? '' : this.sep) + name;
  455. }
  456. /**
  457. * The relative path from the cwd, using / as the path separator.
  458. * If it does not share an ancestor with
  459. * the cwd, then this ends up being equivalent to the fullpathPosix()
  460. * On posix systems, this is identical to relative().
  461. */
  462. relativePosix() {
  463. if (this.sep === '/')
  464. return this.relative();
  465. if (this.isCWD)
  466. return '';
  467. if (this.#relativePosix !== undefined)
  468. return this.#relativePosix;
  469. const name = this.name;
  470. const p = this.parent;
  471. if (!p) {
  472. return (this.#relativePosix = this.fullpathPosix());
  473. }
  474. const pv = p.relativePosix();
  475. return pv + (!pv || !p.parent ? '' : '/') + name;
  476. }
  477. /**
  478. * The fully resolved path string for this Path entry
  479. */
  480. fullpath() {
  481. if (this.#fullpath !== undefined) {
  482. return this.#fullpath;
  483. }
  484. const name = this.name;
  485. const p = this.parent;
  486. if (!p) {
  487. return (this.#fullpath = this.name);
  488. }
  489. const pv = p.fullpath();
  490. const fp = pv + (!p.parent ? '' : this.sep) + name;
  491. return (this.#fullpath = fp);
  492. }
  493. /**
  494. * On platforms other than windows, this is identical to fullpath.
  495. *
  496. * On windows, this is overridden to return the forward-slash form of the
  497. * full UNC path.
  498. */
  499. fullpathPosix() {
  500. if (this.#fullpathPosix !== undefined)
  501. return this.#fullpathPosix;
  502. if (this.sep === '/')
  503. return (this.#fullpathPosix = this.fullpath());
  504. if (!this.parent) {
  505. const p = this.fullpath().replace(/\\/g, '/');
  506. if (/^[a-z]:\//i.test(p)) {
  507. return (this.#fullpathPosix = `//?/${p}`);
  508. }
  509. else {
  510. return (this.#fullpathPosix = p);
  511. }
  512. }
  513. const p = this.parent;
  514. const pfpp = p.fullpathPosix();
  515. const fpp = pfpp + (!pfpp || !p.parent ? '' : '/') + this.name;
  516. return (this.#fullpathPosix = fpp);
  517. }
  518. /**
  519. * Is the Path of an unknown type?
  520. *
  521. * Note that we might know *something* about it if there has been a previous
  522. * filesystem operation, for example that it does not exist, or is not a
  523. * link, or whether it has child entries.
  524. */
  525. isUnknown() {
  526. return (this.#type & IFMT) === UNKNOWN;
  527. }
  528. isType(type) {
  529. return this[`is${type}`]();
  530. }
  531. getType() {
  532. return (this.isUnknown() ? 'Unknown'
  533. : this.isDirectory() ? 'Directory'
  534. : this.isFile() ? 'File'
  535. : this.isSymbolicLink() ? 'SymbolicLink'
  536. : this.isFIFO() ? 'FIFO'
  537. : this.isCharacterDevice() ? 'CharacterDevice'
  538. : this.isBlockDevice() ? 'BlockDevice'
  539. : /* c8 ignore start */ this.isSocket() ? 'Socket'
  540. : 'Unknown');
  541. /* c8 ignore stop */
  542. }
  543. /**
  544. * Is the Path a regular file?
  545. */
  546. isFile() {
  547. return (this.#type & IFMT) === IFREG;
  548. }
  549. /**
  550. * Is the Path a directory?
  551. */
  552. isDirectory() {
  553. return (this.#type & IFMT) === IFDIR;
  554. }
  555. /**
  556. * Is the path a character device?
  557. */
  558. isCharacterDevice() {
  559. return (this.#type & IFMT) === IFCHR;
  560. }
  561. /**
  562. * Is the path a block device?
  563. */
  564. isBlockDevice() {
  565. return (this.#type & IFMT) === IFBLK;
  566. }
  567. /**
  568. * Is the path a FIFO pipe?
  569. */
  570. isFIFO() {
  571. return (this.#type & IFMT) === IFIFO;
  572. }
  573. /**
  574. * Is the path a socket?
  575. */
  576. isSocket() {
  577. return (this.#type & IFMT) === IFSOCK;
  578. }
  579. /**
  580. * Is the path a symbolic link?
  581. */
  582. isSymbolicLink() {
  583. return (this.#type & IFLNK) === IFLNK;
  584. }
  585. /**
  586. * Return the entry if it has been subject of a successful lstat, or
  587. * undefined otherwise.
  588. *
  589. * Does not read the filesystem, so an undefined result *could* simply
  590. * mean that we haven't called lstat on it.
  591. */
  592. lstatCached() {
  593. return this.#type & LSTAT_CALLED ? this : undefined;
  594. }
  595. /**
  596. * Return the cached link target if the entry has been the subject of a
  597. * successful readlink, or undefined otherwise.
  598. *
  599. * Does not read the filesystem, so an undefined result *could* just mean we
  600. * don't have any cached data. Only use it if you are very sure that a
  601. * readlink() has been called at some point.
  602. */
  603. readlinkCached() {
  604. return this.#linkTarget;
  605. }
  606. /**
  607. * Returns the cached realpath target if the entry has been the subject
  608. * of a successful realpath, or undefined otherwise.
  609. *
  610. * Does not read the filesystem, so an undefined result *could* just mean we
  611. * don't have any cached data. Only use it if you are very sure that a
  612. * realpath() has been called at some point.
  613. */
  614. realpathCached() {
  615. return this.#realpath;
  616. }
  617. /**
  618. * Returns the cached child Path entries array if the entry has been the
  619. * subject of a successful readdir(), or [] otherwise.
  620. *
  621. * Does not read the filesystem, so an empty array *could* just mean we
  622. * don't have any cached data. Only use it if you are very sure that a
  623. * readdir() has been called recently enough to still be valid.
  624. */
  625. readdirCached() {
  626. const children = this.children();
  627. return children.slice(0, children.provisional);
  628. }
  629. /**
  630. * Return true if it's worth trying to readlink. Ie, we don't (yet) have
  631. * any indication that readlink will definitely fail.
  632. *
  633. * Returns false if the path is known to not be a symlink, if a previous
  634. * readlink failed, or if the entry does not exist.
  635. */
  636. canReadlink() {
  637. if (this.#linkTarget)
  638. return true;
  639. if (!this.parent)
  640. return false;
  641. // cases where it cannot possibly succeed
  642. const ifmt = this.#type & IFMT;
  643. return !((ifmt !== UNKNOWN && ifmt !== IFLNK) ||
  644. this.#type & ENOREADLINK ||
  645. this.#type & ENOENT);
  646. }
  647. /**
  648. * Return true if readdir has previously been successfully called on this
  649. * path, indicating that cachedReaddir() is likely valid.
  650. */
  651. calledReaddir() {
  652. return !!(this.#type & READDIR_CALLED);
  653. }
  654. /**
  655. * Returns true if the path is known to not exist. That is, a previous lstat
  656. * or readdir failed to verify its existence when that would have been
  657. * expected, or a parent entry was marked either enoent or enotdir.
  658. */
  659. isENOENT() {
  660. return !!(this.#type & ENOENT);
  661. }
  662. /**
  663. * Return true if the path is a match for the given path name. This handles
  664. * case sensitivity and unicode normalization.
  665. *
  666. * Note: even on case-sensitive systems, it is **not** safe to test the
  667. * equality of the `.name` property to determine whether a given pathname
  668. * matches, due to unicode normalization mismatches.
  669. *
  670. * Always use this method instead of testing the `path.name` property
  671. * directly.
  672. */
  673. isNamed(n) {
  674. return !this.nocase ?
  675. this.#matchName === normalize(n)
  676. : this.#matchName === normalizeNocase(n);
  677. }
  678. /**
  679. * Return the Path object corresponding to the target of a symbolic link.
  680. *
  681. * If the Path is not a symbolic link, or if the readlink call fails for any
  682. * reason, `undefined` is returned.
  683. *
  684. * Result is cached, and thus may be outdated if the filesystem is mutated.
  685. */
  686. async readlink() {
  687. const target = this.#linkTarget;
  688. if (target) {
  689. return target;
  690. }
  691. if (!this.canReadlink()) {
  692. return undefined;
  693. }
  694. /* c8 ignore start */
  695. // already covered by the canReadlink test, here for ts grumples
  696. if (!this.parent) {
  697. return undefined;
  698. }
  699. /* c8 ignore stop */
  700. try {
  701. const read = await this.#fs.promises.readlink(this.fullpath());
  702. const linkTarget = (await this.parent.realpath())?.resolve(read);
  703. if (linkTarget) {
  704. return (this.#linkTarget = linkTarget);
  705. }
  706. }
  707. catch (er) {
  708. this.#readlinkFail(er.code);
  709. return undefined;
  710. }
  711. }
  712. /**
  713. * Synchronous {@link PathBase.readlink}
  714. */
  715. readlinkSync() {
  716. const target = this.#linkTarget;
  717. if (target) {
  718. return target;
  719. }
  720. if (!this.canReadlink()) {
  721. return undefined;
  722. }
  723. /* c8 ignore start */
  724. // already covered by the canReadlink test, here for ts grumples
  725. if (!this.parent) {
  726. return undefined;
  727. }
  728. /* c8 ignore stop */
  729. try {
  730. const read = this.#fs.readlinkSync(this.fullpath());
  731. const linkTarget = this.parent.realpathSync()?.resolve(read);
  732. if (linkTarget) {
  733. return (this.#linkTarget = linkTarget);
  734. }
  735. }
  736. catch (er) {
  737. this.#readlinkFail(er.code);
  738. return undefined;
  739. }
  740. }
  741. #readdirSuccess(children) {
  742. // succeeded, mark readdir called bit
  743. this.#type |= READDIR_CALLED;
  744. // mark all remaining provisional children as ENOENT
  745. for (let p = children.provisional; p < children.length; p++) {
  746. const c = children[p];
  747. if (c)
  748. c.#markENOENT();
  749. }
  750. }
  751. #markENOENT() {
  752. // mark as UNKNOWN and ENOENT
  753. if (this.#type & ENOENT)
  754. return;
  755. this.#type = (this.#type | ENOENT) & IFMT_UNKNOWN;
  756. this.#markChildrenENOENT();
  757. }
  758. #markChildrenENOENT() {
  759. // all children are provisional and do not exist
  760. const children = this.children();
  761. children.provisional = 0;
  762. for (const p of children) {
  763. p.#markENOENT();
  764. }
  765. }
  766. #markENOREALPATH() {
  767. this.#type |= ENOREALPATH;
  768. this.#markENOTDIR();
  769. }
  770. // save the information when we know the entry is not a dir
  771. #markENOTDIR() {
  772. // entry is not a directory, so any children can't exist.
  773. // this *should* be impossible, since any children created
  774. // after it's been marked ENOTDIR should be marked ENOENT,
  775. // so it won't even get to this point.
  776. /* c8 ignore start */
  777. if (this.#type & ENOTDIR)
  778. return;
  779. /* c8 ignore stop */
  780. let t = this.#type;
  781. // this could happen if we stat a dir, then delete it,
  782. // then try to read it or one of its children.
  783. if ((t & IFMT) === IFDIR)
  784. t &= IFMT_UNKNOWN;
  785. this.#type = t | ENOTDIR;
  786. this.#markChildrenENOENT();
  787. }
  788. #readdirFail(code = '') {
  789. // markENOTDIR and markENOENT also set provisional=0
  790. if (code === 'ENOTDIR' || code === 'EPERM') {
  791. this.#markENOTDIR();
  792. }
  793. else if (code === 'ENOENT') {
  794. this.#markENOENT();
  795. }
  796. else {
  797. this.children().provisional = 0;
  798. }
  799. }
  800. #lstatFail(code = '') {
  801. // Windows just raises ENOENT in this case, disable for win CI
  802. /* c8 ignore start */
  803. if (code === 'ENOTDIR') {
  804. // already know it has a parent by this point
  805. const p = this.parent;
  806. p.#markENOTDIR();
  807. }
  808. else if (code === 'ENOENT') {
  809. /* c8 ignore stop */
  810. this.#markENOENT();
  811. }
  812. }
  813. #readlinkFail(code = '') {
  814. let ter = this.#type;
  815. ter |= ENOREADLINK;
  816. if (code === 'ENOENT')
  817. ter |= ENOENT;
  818. // windows gets a weird error when you try to readlink a file
  819. if (code === 'EINVAL' || code === 'UNKNOWN') {
  820. // exists, but not a symlink, we don't know WHAT it is, so remove
  821. // all IFMT bits.
  822. ter &= IFMT_UNKNOWN;
  823. }
  824. this.#type = ter;
  825. // windows just gets ENOENT in this case. We do cover the case,
  826. // just disabled because it's impossible on Windows CI
  827. /* c8 ignore start */
  828. if (code === 'ENOTDIR' && this.parent) {
  829. this.parent.#markENOTDIR();
  830. }
  831. /* c8 ignore stop */
  832. }
  833. #readdirAddChild(e, c) {
  834. return (this.#readdirMaybePromoteChild(e, c) ||
  835. this.#readdirAddNewChild(e, c));
  836. }
  837. #readdirAddNewChild(e, c) {
  838. // alloc new entry at head, so it's never provisional
  839. const type = entToType(e);
  840. const child = this.newChild(e.name, type, { parent: this });
  841. const ifmt = child.#type & IFMT;
  842. if (ifmt !== IFDIR && ifmt !== IFLNK && ifmt !== UNKNOWN) {
  843. child.#type |= ENOTDIR;
  844. }
  845. c.unshift(child);
  846. c.provisional++;
  847. return child;
  848. }
  849. #readdirMaybePromoteChild(e, c) {
  850. for (let p = c.provisional; p < c.length; p++) {
  851. const pchild = c[p];
  852. const name = this.nocase ? normalizeNocase(e.name) : normalize(e.name);
  853. if (name !== pchild.#matchName) {
  854. continue;
  855. }
  856. return this.#readdirPromoteChild(e, pchild, p, c);
  857. }
  858. }
  859. #readdirPromoteChild(e, p, index, c) {
  860. const v = p.name;
  861. // retain any other flags, but set ifmt from dirent
  862. p.#type = (p.#type & IFMT_UNKNOWN) | entToType(e);
  863. // case sensitivity fixing when we learn the true name.
  864. if (v !== e.name)
  865. p.name = e.name;
  866. // just advance provisional index (potentially off the list),
  867. // otherwise we have to splice/pop it out and re-insert at head
  868. if (index !== c.provisional) {
  869. if (index === c.length - 1)
  870. c.pop();
  871. else
  872. c.splice(index, 1);
  873. c.unshift(p);
  874. }
  875. c.provisional++;
  876. return p;
  877. }
  878. /**
  879. * Call lstat() on this Path, and update all known information that can be
  880. * determined.
  881. *
  882. * Note that unlike `fs.lstat()`, the returned value does not contain some
  883. * information, such as `mode`, `dev`, `nlink`, and `ino`. If that
  884. * information is required, you will need to call `fs.lstat` yourself.
  885. *
  886. * If the Path refers to a nonexistent file, or if the lstat call fails for
  887. * any reason, `undefined` is returned. Otherwise the updated Path object is
  888. * returned.
  889. *
  890. * Results are cached, and thus may be out of date if the filesystem is
  891. * mutated.
  892. */
  893. async lstat() {
  894. if ((this.#type & ENOENT) === 0) {
  895. try {
  896. this.#applyStat(await this.#fs.promises.lstat(this.fullpath()));
  897. return this;
  898. }
  899. catch (er) {
  900. this.#lstatFail(er.code);
  901. }
  902. }
  903. }
  904. /**
  905. * synchronous {@link PathBase.lstat}
  906. */
  907. lstatSync() {
  908. if ((this.#type & ENOENT) === 0) {
  909. try {
  910. this.#applyStat(this.#fs.lstatSync(this.fullpath()));
  911. return this;
  912. }
  913. catch (er) {
  914. this.#lstatFail(er.code);
  915. }
  916. }
  917. }
  918. #applyStat(st) {
  919. const { atime, atimeMs, birthtime, birthtimeMs, blksize, blocks, ctime, ctimeMs, dev, gid, ino, mode, mtime, mtimeMs, nlink, rdev, size, uid, } = st;
  920. this.#atime = atime;
  921. this.#atimeMs = atimeMs;
  922. this.#birthtime = birthtime;
  923. this.#birthtimeMs = birthtimeMs;
  924. this.#blksize = blksize;
  925. this.#blocks = blocks;
  926. this.#ctime = ctime;
  927. this.#ctimeMs = ctimeMs;
  928. this.#dev = dev;
  929. this.#gid = gid;
  930. this.#ino = ino;
  931. this.#mode = mode;
  932. this.#mtime = mtime;
  933. this.#mtimeMs = mtimeMs;
  934. this.#nlink = nlink;
  935. this.#rdev = rdev;
  936. this.#size = size;
  937. this.#uid = uid;
  938. const ifmt = entToType(st);
  939. // retain any other flags, but set the ifmt
  940. this.#type = (this.#type & IFMT_UNKNOWN) | ifmt | LSTAT_CALLED;
  941. if (ifmt !== UNKNOWN && ifmt !== IFDIR && ifmt !== IFLNK) {
  942. this.#type |= ENOTDIR;
  943. }
  944. }
  945. #onReaddirCB = [];
  946. #readdirCBInFlight = false;
  947. #callOnReaddirCB(children) {
  948. this.#readdirCBInFlight = false;
  949. const cbs = this.#onReaddirCB.slice();
  950. this.#onReaddirCB.length = 0;
  951. cbs.forEach(cb => cb(null, children));
  952. }
  953. /**
  954. * Standard node-style callback interface to get list of directory entries.
  955. *
  956. * If the Path cannot or does not contain any children, then an empty array
  957. * is returned.
  958. *
  959. * Results are cached, and thus may be out of date if the filesystem is
  960. * mutated.
  961. *
  962. * @param cb The callback called with (er, entries). Note that the `er`
  963. * param is somewhat extraneous, as all readdir() errors are handled and
  964. * simply result in an empty set of entries being returned.
  965. * @param allowZalgo Boolean indicating that immediately known results should
  966. * *not* be deferred with `queueMicrotask`. Defaults to `false`. Release
  967. * zalgo at your peril, the dark pony lord is devious and unforgiving.
  968. */
  969. readdirCB(cb, allowZalgo = false) {
  970. if (!this.canReaddir()) {
  971. if (allowZalgo)
  972. cb(null, []);
  973. else
  974. queueMicrotask(() => cb(null, []));
  975. return;
  976. }
  977. const children = this.children();
  978. if (this.calledReaddir()) {
  979. const c = children.slice(0, children.provisional);
  980. if (allowZalgo)
  981. cb(null, c);
  982. else
  983. queueMicrotask(() => cb(null, c));
  984. return;
  985. }
  986. // don't have to worry about zalgo at this point.
  987. this.#onReaddirCB.push(cb);
  988. if (this.#readdirCBInFlight) {
  989. return;
  990. }
  991. this.#readdirCBInFlight = true;
  992. // else read the directory, fill up children
  993. // de-provisionalize any provisional children.
  994. const fullpath = this.fullpath();
  995. this.#fs.readdir(fullpath, { withFileTypes: true }, (er, entries) => {
  996. if (er) {
  997. this.#readdirFail(er.code);
  998. children.provisional = 0;
  999. }
  1000. else {
  1001. // if we didn't get an error, we always get entries.
  1002. //@ts-ignore
  1003. for (const e of entries) {
  1004. this.#readdirAddChild(e, children);
  1005. }
  1006. this.#readdirSuccess(children);
  1007. }
  1008. this.#callOnReaddirCB(children.slice(0, children.provisional));
  1009. return;
  1010. });
  1011. }
  1012. #asyncReaddirInFlight;
  1013. /**
  1014. * Return an array of known child entries.
  1015. *
  1016. * If the Path cannot or does not contain any children, then an empty array
  1017. * is returned.
  1018. *
  1019. * Results are cached, and thus may be out of date if the filesystem is
  1020. * mutated.
  1021. */
  1022. async readdir() {
  1023. if (!this.canReaddir()) {
  1024. return [];
  1025. }
  1026. const children = this.children();
  1027. if (this.calledReaddir()) {
  1028. return children.slice(0, children.provisional);
  1029. }
  1030. // else read the directory, fill up children
  1031. // de-provisionalize any provisional children.
  1032. const fullpath = this.fullpath();
  1033. if (this.#asyncReaddirInFlight) {
  1034. await this.#asyncReaddirInFlight;
  1035. }
  1036. else {
  1037. /* c8 ignore start */
  1038. let resolve = () => { };
  1039. /* c8 ignore stop */
  1040. this.#asyncReaddirInFlight = new Promise(res => (resolve = res));
  1041. try {
  1042. for (const e of await this.#fs.promises.readdir(fullpath, {
  1043. withFileTypes: true,
  1044. })) {
  1045. this.#readdirAddChild(e, children);
  1046. }
  1047. this.#readdirSuccess(children);
  1048. }
  1049. catch (er) {
  1050. this.#readdirFail(er.code);
  1051. children.provisional = 0;
  1052. }
  1053. this.#asyncReaddirInFlight = undefined;
  1054. resolve();
  1055. }
  1056. return children.slice(0, children.provisional);
  1057. }
  1058. /**
  1059. * synchronous {@link PathBase.readdir}
  1060. */
  1061. readdirSync() {
  1062. if (!this.canReaddir()) {
  1063. return [];
  1064. }
  1065. const children = this.children();
  1066. if (this.calledReaddir()) {
  1067. return children.slice(0, children.provisional);
  1068. }
  1069. // else read the directory, fill up children
  1070. // de-provisionalize any provisional children.
  1071. const fullpath = this.fullpath();
  1072. try {
  1073. for (const e of this.#fs.readdirSync(fullpath, {
  1074. withFileTypes: true,
  1075. })) {
  1076. this.#readdirAddChild(e, children);
  1077. }
  1078. this.#readdirSuccess(children);
  1079. }
  1080. catch (er) {
  1081. this.#readdirFail(er.code);
  1082. children.provisional = 0;
  1083. }
  1084. return children.slice(0, children.provisional);
  1085. }
  1086. canReaddir() {
  1087. if (this.#type & ENOCHILD)
  1088. return false;
  1089. const ifmt = IFMT & this.#type;
  1090. // we always set ENOTDIR when setting IFMT, so should be impossible
  1091. /* c8 ignore start */
  1092. if (!(ifmt === UNKNOWN || ifmt === IFDIR || ifmt === IFLNK)) {
  1093. return false;
  1094. }
  1095. /* c8 ignore stop */
  1096. return true;
  1097. }
  1098. shouldWalk(dirs, walkFilter) {
  1099. return ((this.#type & IFDIR) === IFDIR &&
  1100. !(this.#type & ENOCHILD) &&
  1101. !dirs.has(this) &&
  1102. (!walkFilter || walkFilter(this)));
  1103. }
  1104. /**
  1105. * Return the Path object corresponding to path as resolved
  1106. * by realpath(3).
  1107. *
  1108. * If the realpath call fails for any reason, `undefined` is returned.
  1109. *
  1110. * Result is cached, and thus may be outdated if the filesystem is mutated.
  1111. * On success, returns a Path object.
  1112. */
  1113. async realpath() {
  1114. if (this.#realpath)
  1115. return this.#realpath;
  1116. if ((ENOREALPATH | ENOREADLINK | ENOENT) & this.#type)
  1117. return undefined;
  1118. try {
  1119. const rp = await this.#fs.promises.realpath(this.fullpath());
  1120. return (this.#realpath = this.resolve(rp));
  1121. }
  1122. catch (_) {
  1123. this.#markENOREALPATH();
  1124. }
  1125. }
  1126. /**
  1127. * Synchronous {@link realpath}
  1128. */
  1129. realpathSync() {
  1130. if (this.#realpath)
  1131. return this.#realpath;
  1132. if ((ENOREALPATH | ENOREADLINK | ENOENT) & this.#type)
  1133. return undefined;
  1134. try {
  1135. const rp = this.#fs.realpathSync(this.fullpath());
  1136. return (this.#realpath = this.resolve(rp));
  1137. }
  1138. catch (_) {
  1139. this.#markENOREALPATH();
  1140. }
  1141. }
  1142. /**
  1143. * Internal method to mark this Path object as the scurry cwd,
  1144. * called by {@link PathScurry#chdir}
  1145. *
  1146. * @internal
  1147. */
  1148. [setAsCwd](oldCwd) {
  1149. if (oldCwd === this)
  1150. return;
  1151. oldCwd.isCWD = false;
  1152. this.isCWD = true;
  1153. const changed = new Set([]);
  1154. let rp = [];
  1155. let p = this;
  1156. while (p && p.parent) {
  1157. changed.add(p);
  1158. p.#relative = rp.join(this.sep);
  1159. p.#relativePosix = rp.join('/');
  1160. p = p.parent;
  1161. rp.push('..');
  1162. }
  1163. // now un-memoize parents of old cwd
  1164. p = oldCwd;
  1165. while (p && p.parent && !changed.has(p)) {
  1166. p.#relative = undefined;
  1167. p.#relativePosix = undefined;
  1168. p = p.parent;
  1169. }
  1170. }
  1171. }
  1172. exports.PathBase = PathBase;
  1173. /**
  1174. * Path class used on win32 systems
  1175. *
  1176. * Uses `'\\'` as the path separator for returned paths, either `'\\'` or `'/'`
  1177. * as the path separator for parsing paths.
  1178. */
  1179. class PathWin32 extends PathBase {
  1180. /**
  1181. * Separator for generating path strings.
  1182. */
  1183. sep = '\\';
  1184. /**
  1185. * Separator for parsing path strings.
  1186. */
  1187. splitSep = eitherSep;
  1188. /**
  1189. * Do not create new Path objects directly. They should always be accessed
  1190. * via the PathScurry class or other methods on the Path class.
  1191. *
  1192. * @internal
  1193. */
  1194. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  1195. super(name, type, root, roots, nocase, children, opts);
  1196. }
  1197. /**
  1198. * @internal
  1199. */
  1200. newChild(name, type = UNKNOWN, opts = {}) {
  1201. return new PathWin32(name, type, this.root, this.roots, this.nocase, this.childrenCache(), opts);
  1202. }
  1203. /**
  1204. * @internal
  1205. */
  1206. getRootString(path) {
  1207. return node_path_1.win32.parse(path).root;
  1208. }
  1209. /**
  1210. * @internal
  1211. */
  1212. getRoot(rootPath) {
  1213. rootPath = uncToDrive(rootPath.toUpperCase());
  1214. if (rootPath === this.root.name) {
  1215. return this.root;
  1216. }
  1217. // ok, not that one, check if it matches another we know about
  1218. for (const [compare, root] of Object.entries(this.roots)) {
  1219. if (this.sameRoot(rootPath, compare)) {
  1220. return (this.roots[rootPath] = root);
  1221. }
  1222. }
  1223. // otherwise, have to create a new one.
  1224. return (this.roots[rootPath] = new PathScurryWin32(rootPath, this).root);
  1225. }
  1226. /**
  1227. * @internal
  1228. */
  1229. sameRoot(rootPath, compare = this.root.name) {
  1230. // windows can (rarely) have case-sensitive filesystem, but
  1231. // UNC and drive letters are always case-insensitive, and canonically
  1232. // represented uppercase.
  1233. rootPath = rootPath
  1234. .toUpperCase()
  1235. .replace(/\//g, '\\')
  1236. .replace(uncDriveRegexp, '$1\\');
  1237. return rootPath === compare;
  1238. }
  1239. }
  1240. exports.PathWin32 = PathWin32;
  1241. /**
  1242. * Path class used on all posix systems.
  1243. *
  1244. * Uses `'/'` as the path separator.
  1245. */
  1246. class PathPosix extends PathBase {
  1247. /**
  1248. * separator for parsing path strings
  1249. */
  1250. splitSep = '/';
  1251. /**
  1252. * separator for generating path strings
  1253. */
  1254. sep = '/';
  1255. /**
  1256. * Do not create new Path objects directly. They should always be accessed
  1257. * via the PathScurry class or other methods on the Path class.
  1258. *
  1259. * @internal
  1260. */
  1261. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  1262. super(name, type, root, roots, nocase, children, opts);
  1263. }
  1264. /**
  1265. * @internal
  1266. */
  1267. getRootString(path) {
  1268. return path.startsWith('/') ? '/' : '';
  1269. }
  1270. /**
  1271. * @internal
  1272. */
  1273. getRoot(_rootPath) {
  1274. return this.root;
  1275. }
  1276. /**
  1277. * @internal
  1278. */
  1279. newChild(name, type = UNKNOWN, opts = {}) {
  1280. return new PathPosix(name, type, this.root, this.roots, this.nocase, this.childrenCache(), opts);
  1281. }
  1282. }
  1283. exports.PathPosix = PathPosix;
  1284. /**
  1285. * The base class for all PathScurry classes, providing the interface for path
  1286. * resolution and filesystem operations.
  1287. *
  1288. * Typically, you should *not* instantiate this class directly, but rather one
  1289. * of the platform-specific classes, or the exported {@link PathScurry} which
  1290. * defaults to the current platform.
  1291. */
  1292. class PathScurryBase {
  1293. /**
  1294. * The root Path entry for the current working directory of this Scurry
  1295. */
  1296. root;
  1297. /**
  1298. * The string path for the root of this Scurry's current working directory
  1299. */
  1300. rootPath;
  1301. /**
  1302. * A collection of all roots encountered, referenced by rootPath
  1303. */
  1304. roots;
  1305. /**
  1306. * The Path entry corresponding to this PathScurry's current working directory.
  1307. */
  1308. cwd;
  1309. #resolveCache;
  1310. #resolvePosixCache;
  1311. #children;
  1312. /**
  1313. * Perform path comparisons case-insensitively.
  1314. *
  1315. * Defaults true on Darwin and Windows systems, false elsewhere.
  1316. */
  1317. nocase;
  1318. #fs;
  1319. /**
  1320. * This class should not be instantiated directly.
  1321. *
  1322. * Use PathScurryWin32, PathScurryDarwin, PathScurryPosix, or PathScurry
  1323. *
  1324. * @internal
  1325. */
  1326. constructor(cwd = process.cwd(), pathImpl, sep, { nocase, childrenCacheSize = 16 * 1024, fs = defaultFS, } = {}) {
  1327. this.#fs = fsFromOption(fs);
  1328. if (cwd instanceof URL || cwd.startsWith('file://')) {
  1329. cwd = (0, node_url_1.fileURLToPath)(cwd);
  1330. }
  1331. // resolve and split root, and then add to the store.
  1332. // this is the only time we call path.resolve()
  1333. const cwdPath = pathImpl.resolve(cwd);
  1334. this.roots = Object.create(null);
  1335. this.rootPath = this.parseRootPath(cwdPath);
  1336. this.#resolveCache = new ResolveCache();
  1337. this.#resolvePosixCache = new ResolveCache();
  1338. this.#children = new ChildrenCache(childrenCacheSize);
  1339. const split = cwdPath.substring(this.rootPath.length).split(sep);
  1340. // resolve('/') leaves '', splits to [''], we don't want that.
  1341. if (split.length === 1 && !split[0]) {
  1342. split.pop();
  1343. }
  1344. /* c8 ignore start */
  1345. if (nocase === undefined) {
  1346. throw new TypeError('must provide nocase setting to PathScurryBase ctor');
  1347. }
  1348. /* c8 ignore stop */
  1349. this.nocase = nocase;
  1350. this.root = this.newRoot(this.#fs);
  1351. this.roots[this.rootPath] = this.root;
  1352. let prev = this.root;
  1353. let len = split.length - 1;
  1354. const joinSep = pathImpl.sep;
  1355. let abs = this.rootPath;
  1356. let sawFirst = false;
  1357. for (const part of split) {
  1358. const l = len--;
  1359. prev = prev.child(part, {
  1360. relative: new Array(l).fill('..').join(joinSep),
  1361. relativePosix: new Array(l).fill('..').join('/'),
  1362. fullpath: (abs += (sawFirst ? '' : joinSep) + part),
  1363. });
  1364. sawFirst = true;
  1365. }
  1366. this.cwd = prev;
  1367. }
  1368. /**
  1369. * Get the depth of a provided path, string, or the cwd
  1370. */
  1371. depth(path = this.cwd) {
  1372. if (typeof path === 'string') {
  1373. path = this.cwd.resolve(path);
  1374. }
  1375. return path.depth();
  1376. }
  1377. /**
  1378. * Return the cache of child entries. Exposed so subclasses can create
  1379. * child Path objects in a platform-specific way.
  1380. *
  1381. * @internal
  1382. */
  1383. childrenCache() {
  1384. return this.#children;
  1385. }
  1386. /**
  1387. * Resolve one or more path strings to a resolved string
  1388. *
  1389. * Same interface as require('path').resolve.
  1390. *
  1391. * Much faster than path.resolve() when called multiple times for the same
  1392. * path, because the resolved Path objects are cached. Much slower
  1393. * otherwise.
  1394. */
  1395. resolve(...paths) {
  1396. // first figure out the minimum number of paths we have to test
  1397. // we always start at cwd, but any absolutes will bump the start
  1398. let r = '';
  1399. for (let i = paths.length - 1; i >= 0; i--) {
  1400. const p = paths[i];
  1401. if (!p || p === '.')
  1402. continue;
  1403. r = r ? `${p}/${r}` : p;
  1404. if (this.isAbsolute(p)) {
  1405. break;
  1406. }
  1407. }
  1408. const cached = this.#resolveCache.get(r);
  1409. if (cached !== undefined) {
  1410. return cached;
  1411. }
  1412. const result = this.cwd.resolve(r).fullpath();
  1413. this.#resolveCache.set(r, result);
  1414. return result;
  1415. }
  1416. /**
  1417. * Resolve one or more path strings to a resolved string, returning
  1418. * the posix path. Identical to .resolve() on posix systems, but on
  1419. * windows will return a forward-slash separated UNC path.
  1420. *
  1421. * Same interface as require('path').resolve.
  1422. *
  1423. * Much faster than path.resolve() when called multiple times for the same
  1424. * path, because the resolved Path objects are cached. Much slower
  1425. * otherwise.
  1426. */
  1427. resolvePosix(...paths) {
  1428. // first figure out the minimum number of paths we have to test
  1429. // we always start at cwd, but any absolutes will bump the start
  1430. let r = '';
  1431. for (let i = paths.length - 1; i >= 0; i--) {
  1432. const p = paths[i];
  1433. if (!p || p === '.')
  1434. continue;
  1435. r = r ? `${p}/${r}` : p;
  1436. if (this.isAbsolute(p)) {
  1437. break;
  1438. }
  1439. }
  1440. const cached = this.#resolvePosixCache.get(r);
  1441. if (cached !== undefined) {
  1442. return cached;
  1443. }
  1444. const result = this.cwd.resolve(r).fullpathPosix();
  1445. this.#resolvePosixCache.set(r, result);
  1446. return result;
  1447. }
  1448. /**
  1449. * find the relative path from the cwd to the supplied path string or entry
  1450. */
  1451. relative(entry = this.cwd) {
  1452. if (typeof entry === 'string') {
  1453. entry = this.cwd.resolve(entry);
  1454. }
  1455. return entry.relative();
  1456. }
  1457. /**
  1458. * find the relative path from the cwd to the supplied path string or
  1459. * entry, using / as the path delimiter, even on Windows.
  1460. */
  1461. relativePosix(entry = this.cwd) {
  1462. if (typeof entry === 'string') {
  1463. entry = this.cwd.resolve(entry);
  1464. }
  1465. return entry.relativePosix();
  1466. }
  1467. /**
  1468. * Return the basename for the provided string or Path object
  1469. */
  1470. basename(entry = this.cwd) {
  1471. if (typeof entry === 'string') {
  1472. entry = this.cwd.resolve(entry);
  1473. }
  1474. return entry.name;
  1475. }
  1476. /**
  1477. * Return the dirname for the provided string or Path object
  1478. */
  1479. dirname(entry = this.cwd) {
  1480. if (typeof entry === 'string') {
  1481. entry = this.cwd.resolve(entry);
  1482. }
  1483. return (entry.parent || entry).fullpath();
  1484. }
  1485. async readdir(entry = this.cwd, opts = {
  1486. withFileTypes: true,
  1487. }) {
  1488. if (typeof entry === 'string') {
  1489. entry = this.cwd.resolve(entry);
  1490. }
  1491. else if (!(entry instanceof PathBase)) {
  1492. opts = entry;
  1493. entry = this.cwd;
  1494. }
  1495. const { withFileTypes } = opts;
  1496. if (!entry.canReaddir()) {
  1497. return [];
  1498. }
  1499. else {
  1500. const p = await entry.readdir();
  1501. return withFileTypes ? p : p.map(e => e.name);
  1502. }
  1503. }
  1504. readdirSync(entry = this.cwd, opts = {
  1505. withFileTypes: true,
  1506. }) {
  1507. if (typeof entry === 'string') {
  1508. entry = this.cwd.resolve(entry);
  1509. }
  1510. else if (!(entry instanceof PathBase)) {
  1511. opts = entry;
  1512. entry = this.cwd;
  1513. }
  1514. const { withFileTypes = true } = opts;
  1515. if (!entry.canReaddir()) {
  1516. return [];
  1517. }
  1518. else if (withFileTypes) {
  1519. return entry.readdirSync();
  1520. }
  1521. else {
  1522. return entry.readdirSync().map(e => e.name);
  1523. }
  1524. }
  1525. /**
  1526. * Call lstat() on the string or Path object, and update all known
  1527. * information that can be determined.
  1528. *
  1529. * Note that unlike `fs.lstat()`, the returned value does not contain some
  1530. * information, such as `mode`, `dev`, `nlink`, and `ino`. If that
  1531. * information is required, you will need to call `fs.lstat` yourself.
  1532. *
  1533. * If the Path refers to a nonexistent file, or if the lstat call fails for
  1534. * any reason, `undefined` is returned. Otherwise the updated Path object is
  1535. * returned.
  1536. *
  1537. * Results are cached, and thus may be out of date if the filesystem is
  1538. * mutated.
  1539. */
  1540. async lstat(entry = this.cwd) {
  1541. if (typeof entry === 'string') {
  1542. entry = this.cwd.resolve(entry);
  1543. }
  1544. return entry.lstat();
  1545. }
  1546. /**
  1547. * synchronous {@link PathScurryBase.lstat}
  1548. */
  1549. lstatSync(entry = this.cwd) {
  1550. if (typeof entry === 'string') {
  1551. entry = this.cwd.resolve(entry);
  1552. }
  1553. return entry.lstatSync();
  1554. }
  1555. async readlink(entry = this.cwd, { withFileTypes } = {
  1556. withFileTypes: false,
  1557. }) {
  1558. if (typeof entry === 'string') {
  1559. entry = this.cwd.resolve(entry);
  1560. }
  1561. else if (!(entry instanceof PathBase)) {
  1562. withFileTypes = entry.withFileTypes;
  1563. entry = this.cwd;
  1564. }
  1565. const e = await entry.readlink();
  1566. return withFileTypes ? e : e?.fullpath();
  1567. }
  1568. readlinkSync(entry = this.cwd, { withFileTypes } = {
  1569. withFileTypes: false,
  1570. }) {
  1571. if (typeof entry === 'string') {
  1572. entry = this.cwd.resolve(entry);
  1573. }
  1574. else if (!(entry instanceof PathBase)) {
  1575. withFileTypes = entry.withFileTypes;
  1576. entry = this.cwd;
  1577. }
  1578. const e = entry.readlinkSync();
  1579. return withFileTypes ? e : e?.fullpath();
  1580. }
  1581. async realpath(entry = this.cwd, { withFileTypes } = {
  1582. withFileTypes: false,
  1583. }) {
  1584. if (typeof entry === 'string') {
  1585. entry = this.cwd.resolve(entry);
  1586. }
  1587. else if (!(entry instanceof PathBase)) {
  1588. withFileTypes = entry.withFileTypes;
  1589. entry = this.cwd;
  1590. }
  1591. const e = await entry.realpath();
  1592. return withFileTypes ? e : e?.fullpath();
  1593. }
  1594. realpathSync(entry = this.cwd, { withFileTypes } = {
  1595. withFileTypes: false,
  1596. }) {
  1597. if (typeof entry === 'string') {
  1598. entry = this.cwd.resolve(entry);
  1599. }
  1600. else if (!(entry instanceof PathBase)) {
  1601. withFileTypes = entry.withFileTypes;
  1602. entry = this.cwd;
  1603. }
  1604. const e = entry.realpathSync();
  1605. return withFileTypes ? e : e?.fullpath();
  1606. }
  1607. async walk(entry = this.cwd, opts = {}) {
  1608. if (typeof entry === 'string') {
  1609. entry = this.cwd.resolve(entry);
  1610. }
  1611. else if (!(entry instanceof PathBase)) {
  1612. opts = entry;
  1613. entry = this.cwd;
  1614. }
  1615. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  1616. const results = [];
  1617. if (!filter || filter(entry)) {
  1618. results.push(withFileTypes ? entry : entry.fullpath());
  1619. }
  1620. const dirs = new Set();
  1621. const walk = (dir, cb) => {
  1622. dirs.add(dir);
  1623. dir.readdirCB((er, entries) => {
  1624. /* c8 ignore start */
  1625. if (er) {
  1626. return cb(er);
  1627. }
  1628. /* c8 ignore stop */
  1629. let len = entries.length;
  1630. if (!len)
  1631. return cb();
  1632. const next = () => {
  1633. if (--len === 0) {
  1634. cb();
  1635. }
  1636. };
  1637. for (const e of entries) {
  1638. if (!filter || filter(e)) {
  1639. results.push(withFileTypes ? e : e.fullpath());
  1640. }
  1641. if (follow && e.isSymbolicLink()) {
  1642. e.realpath()
  1643. .then(r => (r?.isUnknown() ? r.lstat() : r))
  1644. .then(r => r?.shouldWalk(dirs, walkFilter) ? walk(r, next) : next());
  1645. }
  1646. else {
  1647. if (e.shouldWalk(dirs, walkFilter)) {
  1648. walk(e, next);
  1649. }
  1650. else {
  1651. next();
  1652. }
  1653. }
  1654. }
  1655. }, true); // zalgooooooo
  1656. };
  1657. const start = entry;
  1658. return new Promise((res, rej) => {
  1659. walk(start, er => {
  1660. /* c8 ignore start */
  1661. if (er)
  1662. return rej(er);
  1663. /* c8 ignore stop */
  1664. res(results);
  1665. });
  1666. });
  1667. }
  1668. walkSync(entry = this.cwd, opts = {}) {
  1669. if (typeof entry === 'string') {
  1670. entry = this.cwd.resolve(entry);
  1671. }
  1672. else if (!(entry instanceof PathBase)) {
  1673. opts = entry;
  1674. entry = this.cwd;
  1675. }
  1676. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  1677. const results = [];
  1678. if (!filter || filter(entry)) {
  1679. results.push(withFileTypes ? entry : entry.fullpath());
  1680. }
  1681. const dirs = new Set([entry]);
  1682. for (const dir of dirs) {
  1683. const entries = dir.readdirSync();
  1684. for (const e of entries) {
  1685. if (!filter || filter(e)) {
  1686. results.push(withFileTypes ? e : e.fullpath());
  1687. }
  1688. let r = e;
  1689. if (e.isSymbolicLink()) {
  1690. if (!(follow && (r = e.realpathSync())))
  1691. continue;
  1692. if (r.isUnknown())
  1693. r.lstatSync();
  1694. }
  1695. if (r.shouldWalk(dirs, walkFilter)) {
  1696. dirs.add(r);
  1697. }
  1698. }
  1699. }
  1700. return results;
  1701. }
  1702. /**
  1703. * Support for `for await`
  1704. *
  1705. * Alias for {@link PathScurryBase.iterate}
  1706. *
  1707. * Note: As of Node 19, this is very slow, compared to other methods of
  1708. * walking. Consider using {@link PathScurryBase.stream} if memory overhead
  1709. * and backpressure are concerns, or {@link PathScurryBase.walk} if not.
  1710. */
  1711. [Symbol.asyncIterator]() {
  1712. return this.iterate();
  1713. }
  1714. iterate(entry = this.cwd, options = {}) {
  1715. // iterating async over the stream is significantly more performant,
  1716. // especially in the warm-cache scenario, because it buffers up directory
  1717. // entries in the background instead of waiting for a yield for each one.
  1718. if (typeof entry === 'string') {
  1719. entry = this.cwd.resolve(entry);
  1720. }
  1721. else if (!(entry instanceof PathBase)) {
  1722. options = entry;
  1723. entry = this.cwd;
  1724. }
  1725. return this.stream(entry, options)[Symbol.asyncIterator]();
  1726. }
  1727. /**
  1728. * Iterating over a PathScurry performs a synchronous walk.
  1729. *
  1730. * Alias for {@link PathScurryBase.iterateSync}
  1731. */
  1732. [Symbol.iterator]() {
  1733. return this.iterateSync();
  1734. }
  1735. *iterateSync(entry = this.cwd, opts = {}) {
  1736. if (typeof entry === 'string') {
  1737. entry = this.cwd.resolve(entry);
  1738. }
  1739. else if (!(entry instanceof PathBase)) {
  1740. opts = entry;
  1741. entry = this.cwd;
  1742. }
  1743. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  1744. if (!filter || filter(entry)) {
  1745. yield withFileTypes ? entry : entry.fullpath();
  1746. }
  1747. const dirs = new Set([entry]);
  1748. for (const dir of dirs) {
  1749. const entries = dir.readdirSync();
  1750. for (const e of entries) {
  1751. if (!filter || filter(e)) {
  1752. yield withFileTypes ? e : e.fullpath();
  1753. }
  1754. let r = e;
  1755. if (e.isSymbolicLink()) {
  1756. if (!(follow && (r = e.realpathSync())))
  1757. continue;
  1758. if (r.isUnknown())
  1759. r.lstatSync();
  1760. }
  1761. if (r.shouldWalk(dirs, walkFilter)) {
  1762. dirs.add(r);
  1763. }
  1764. }
  1765. }
  1766. }
  1767. stream(entry = this.cwd, opts = {}) {
  1768. if (typeof entry === 'string') {
  1769. entry = this.cwd.resolve(entry);
  1770. }
  1771. else if (!(entry instanceof PathBase)) {
  1772. opts = entry;
  1773. entry = this.cwd;
  1774. }
  1775. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  1776. const results = new minipass_1.Minipass({ objectMode: true });
  1777. if (!filter || filter(entry)) {
  1778. results.write(withFileTypes ? entry : entry.fullpath());
  1779. }
  1780. const dirs = new Set();
  1781. const queue = [entry];
  1782. let processing = 0;
  1783. const process = () => {
  1784. let paused = false;
  1785. while (!paused) {
  1786. const dir = queue.shift();
  1787. if (!dir) {
  1788. if (processing === 0)
  1789. results.end();
  1790. return;
  1791. }
  1792. processing++;
  1793. dirs.add(dir);
  1794. const onReaddir = (er, entries, didRealpaths = false) => {
  1795. /* c8 ignore start */
  1796. if (er)
  1797. return results.emit('error', er);
  1798. /* c8 ignore stop */
  1799. if (follow && !didRealpaths) {
  1800. const promises = [];
  1801. for (const e of entries) {
  1802. if (e.isSymbolicLink()) {
  1803. promises.push(e
  1804. .realpath()
  1805. .then((r) => r?.isUnknown() ? r.lstat() : r));
  1806. }
  1807. }
  1808. if (promises.length) {
  1809. Promise.all(promises).then(() => onReaddir(null, entries, true));
  1810. return;
  1811. }
  1812. }
  1813. for (const e of entries) {
  1814. if (e && (!filter || filter(e))) {
  1815. if (!results.write(withFileTypes ? e : e.fullpath())) {
  1816. paused = true;
  1817. }
  1818. }
  1819. }
  1820. processing--;
  1821. for (const e of entries) {
  1822. const r = e.realpathCached() || e;
  1823. if (r.shouldWalk(dirs, walkFilter)) {
  1824. queue.push(r);
  1825. }
  1826. }
  1827. if (paused && !results.flowing) {
  1828. results.once('drain', process);
  1829. }
  1830. else if (!sync) {
  1831. process();
  1832. }
  1833. };
  1834. // zalgo containment
  1835. let sync = true;
  1836. dir.readdirCB(onReaddir, true);
  1837. sync = false;
  1838. }
  1839. };
  1840. process();
  1841. return results;
  1842. }
  1843. streamSync(entry = this.cwd, opts = {}) {
  1844. if (typeof entry === 'string') {
  1845. entry = this.cwd.resolve(entry);
  1846. }
  1847. else if (!(entry instanceof PathBase)) {
  1848. opts = entry;
  1849. entry = this.cwd;
  1850. }
  1851. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  1852. const results = new minipass_1.Minipass({ objectMode: true });
  1853. const dirs = new Set();
  1854. if (!filter || filter(entry)) {
  1855. results.write(withFileTypes ? entry : entry.fullpath());
  1856. }
  1857. const queue = [entry];
  1858. let processing = 0;
  1859. const process = () => {
  1860. let paused = false;
  1861. while (!paused) {
  1862. const dir = queue.shift();
  1863. if (!dir) {
  1864. if (processing === 0)
  1865. results.end();
  1866. return;
  1867. }
  1868. processing++;
  1869. dirs.add(dir);
  1870. const entries = dir.readdirSync();
  1871. for (const e of entries) {
  1872. if (!filter || filter(e)) {
  1873. if (!results.write(withFileTypes ? e : e.fullpath())) {
  1874. paused = true;
  1875. }
  1876. }
  1877. }
  1878. processing--;
  1879. for (const e of entries) {
  1880. let r = e;
  1881. if (e.isSymbolicLink()) {
  1882. if (!(follow && (r = e.realpathSync())))
  1883. continue;
  1884. if (r.isUnknown())
  1885. r.lstatSync();
  1886. }
  1887. if (r.shouldWalk(dirs, walkFilter)) {
  1888. queue.push(r);
  1889. }
  1890. }
  1891. }
  1892. if (paused && !results.flowing)
  1893. results.once('drain', process);
  1894. };
  1895. process();
  1896. return results;
  1897. }
  1898. chdir(path = this.cwd) {
  1899. const oldCwd = this.cwd;
  1900. this.cwd = typeof path === 'string' ? this.cwd.resolve(path) : path;
  1901. this.cwd[setAsCwd](oldCwd);
  1902. }
  1903. }
  1904. exports.PathScurryBase = PathScurryBase;
  1905. /**
  1906. * Windows implementation of {@link PathScurryBase}
  1907. *
  1908. * Defaults to case insensitve, uses `'\\'` to generate path strings. Uses
  1909. * {@link PathWin32} for Path objects.
  1910. */
  1911. class PathScurryWin32 extends PathScurryBase {
  1912. /**
  1913. * separator for generating path strings
  1914. */
  1915. sep = '\\';
  1916. constructor(cwd = process.cwd(), opts = {}) {
  1917. const { nocase = true } = opts;
  1918. super(cwd, node_path_1.win32, '\\', { ...opts, nocase });
  1919. this.nocase = nocase;
  1920. for (let p = this.cwd; p; p = p.parent) {
  1921. p.nocase = this.nocase;
  1922. }
  1923. }
  1924. /**
  1925. * @internal
  1926. */
  1927. parseRootPath(dir) {
  1928. // if the path starts with a single separator, it's not a UNC, and we'll
  1929. // just get separator as the root, and driveFromUNC will return \
  1930. // In that case, mount \ on the root from the cwd.
  1931. return node_path_1.win32.parse(dir).root.toUpperCase();
  1932. }
  1933. /**
  1934. * @internal
  1935. */
  1936. newRoot(fs) {
  1937. return new PathWin32(this.rootPath, IFDIR, undefined, this.roots, this.nocase, this.childrenCache(), { fs });
  1938. }
  1939. /**
  1940. * Return true if the provided path string is an absolute path
  1941. */
  1942. isAbsolute(p) {
  1943. return (p.startsWith('/') || p.startsWith('\\') || /^[a-z]:(\/|\\)/i.test(p));
  1944. }
  1945. }
  1946. exports.PathScurryWin32 = PathScurryWin32;
  1947. /**
  1948. * {@link PathScurryBase} implementation for all posix systems other than Darwin.
  1949. *
  1950. * Defaults to case-sensitive matching, uses `'/'` to generate path strings.
  1951. *
  1952. * Uses {@link PathPosix} for Path objects.
  1953. */
  1954. class PathScurryPosix extends PathScurryBase {
  1955. /**
  1956. * separator for generating path strings
  1957. */
  1958. sep = '/';
  1959. constructor(cwd = process.cwd(), opts = {}) {
  1960. const { nocase = false } = opts;
  1961. super(cwd, node_path_1.posix, '/', { ...opts, nocase });
  1962. this.nocase = nocase;
  1963. }
  1964. /**
  1965. * @internal
  1966. */
  1967. parseRootPath(_dir) {
  1968. return '/';
  1969. }
  1970. /**
  1971. * @internal
  1972. */
  1973. newRoot(fs) {
  1974. return new PathPosix(this.rootPath, IFDIR, undefined, this.roots, this.nocase, this.childrenCache(), { fs });
  1975. }
  1976. /**
  1977. * Return true if the provided path string is an absolute path
  1978. */
  1979. isAbsolute(p) {
  1980. return p.startsWith('/');
  1981. }
  1982. }
  1983. exports.PathScurryPosix = PathScurryPosix;
  1984. /**
  1985. * {@link PathScurryBase} implementation for Darwin (macOS) systems.
  1986. *
  1987. * Defaults to case-insensitive matching, uses `'/'` for generating path
  1988. * strings.
  1989. *
  1990. * Uses {@link PathPosix} for Path objects.
  1991. */
  1992. class PathScurryDarwin extends PathScurryPosix {
  1993. constructor(cwd = process.cwd(), opts = {}) {
  1994. const { nocase = true } = opts;
  1995. super(cwd, { ...opts, nocase });
  1996. }
  1997. }
  1998. exports.PathScurryDarwin = PathScurryDarwin;
  1999. /**
  2000. * Default {@link PathBase} implementation for the current platform.
  2001. *
  2002. * {@link PathWin32} on Windows systems, {@link PathPosix} on all others.
  2003. */
  2004. exports.Path = process.platform === 'win32' ? PathWin32 : PathPosix;
  2005. /**
  2006. * Default {@link PathScurryBase} implementation for the current platform.
  2007. *
  2008. * {@link PathScurryWin32} on Windows systems, {@link PathScurryDarwin} on
  2009. * Darwin (macOS) systems, {@link PathScurryPosix} on all others.
  2010. */
  2011. exports.PathScurry = process.platform === 'win32' ? PathScurryWin32
  2012. : process.platform === 'darwin' ? PathScurryDarwin
  2013. : PathScurryPosix;
  2014. //# sourceMappingURL=index.js.map