index.js 65 KB

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