index.d.ts 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. import { LRUCache } from 'lru-cache';
  2. import { posix, win32 } from 'node:path';
  3. import { Minipass } from 'minipass';
  4. import type { Dirent, Stats } from 'node:fs';
  5. /**
  6. * An object that will be used to override the default `fs`
  7. * methods. Any methods that are not overridden will use Node's
  8. * built-in implementations.
  9. *
  10. * - lstatSync
  11. * - readdir (callback `withFileTypes` Dirent variant, used for
  12. * readdirCB and most walks)
  13. * - readdirSync
  14. * - readlinkSync
  15. * - realpathSync
  16. * - promises: Object containing the following async methods:
  17. * - lstat
  18. * - readdir (Dirent variant only)
  19. * - readlink
  20. * - realpath
  21. */
  22. export interface FSOption {
  23. lstatSync?: (path: string) => Stats;
  24. readdir?: (path: string, options: {
  25. withFileTypes: true;
  26. }, cb: (er: NodeJS.ErrnoException | null, entries?: Dirent[]) => any) => void;
  27. readdirSync?: (path: string, options: {
  28. withFileTypes: true;
  29. }) => Dirent[];
  30. readlinkSync?: (path: string) => string;
  31. realpathSync?: (path: string) => string;
  32. promises?: {
  33. lstat?: (path: string) => Promise<Stats>;
  34. readdir?: (path: string, options: {
  35. withFileTypes: true;
  36. }) => Promise<Dirent[]>;
  37. readlink?: (path: string) => Promise<string>;
  38. realpath?: (path: string) => Promise<string>;
  39. [k: string]: any;
  40. };
  41. [k: string]: any;
  42. }
  43. interface FSValue {
  44. lstatSync: (path: string) => Stats;
  45. readdir: (path: string, options: {
  46. withFileTypes: true;
  47. }, cb: (er: NodeJS.ErrnoException | null, entries?: Dirent[]) => any) => void;
  48. readdirSync: (path: string, options: {
  49. withFileTypes: true;
  50. }) => Dirent[];
  51. readlinkSync: (path: string) => string;
  52. realpathSync: (path: string) => string;
  53. promises: {
  54. lstat: (path: string) => Promise<Stats>;
  55. readdir: (path: string, options: {
  56. withFileTypes: true;
  57. }) => Promise<Dirent[]>;
  58. readlink: (path: string) => Promise<string>;
  59. realpath: (path: string) => Promise<string>;
  60. [k: string]: any;
  61. };
  62. [k: string]: any;
  63. }
  64. export type Type = 'Unknown' | 'FIFO' | 'CharacterDevice' | 'Directory' | 'BlockDevice' | 'File' | 'SymbolicLink' | 'Socket';
  65. /**
  66. * Options that may be provided to the Path constructor
  67. */
  68. export interface PathOpts {
  69. fullpath?: string;
  70. relative?: string;
  71. relativePosix?: string;
  72. parent?: PathBase;
  73. /**
  74. * See {@link FSOption}
  75. */
  76. fs?: FSOption;
  77. }
  78. /**
  79. * An LRUCache for storing resolved path strings or Path objects.
  80. * @internal
  81. */
  82. export declare class ResolveCache extends LRUCache<string, string> {
  83. constructor();
  84. }
  85. /**
  86. * an LRUCache for storing child entries.
  87. * @internal
  88. */
  89. export declare class ChildrenCache extends LRUCache<PathBase, Children> {
  90. constructor(maxSize?: number);
  91. }
  92. /**
  93. * Array of Path objects, plus a marker indicating the first provisional entry
  94. *
  95. * @internal
  96. */
  97. export type Children = PathBase[] & {
  98. provisional: number;
  99. };
  100. declare const setAsCwd: unique symbol;
  101. /**
  102. * Path objects are sort of like a super-powered
  103. * {@link https://nodejs.org/docs/latest/api/fs.html#class-fsdirent fs.Dirent}
  104. *
  105. * Each one represents a single filesystem entry on disk, which may or may not
  106. * exist. It includes methods for reading various types of information via
  107. * lstat, readlink, and readdir, and caches all information to the greatest
  108. * degree possible.
  109. *
  110. * Note that fs operations that would normally throw will instead return an
  111. * "empty" value. This is in order to prevent excessive overhead from error
  112. * stack traces.
  113. */
  114. export declare abstract class PathBase implements Dirent {
  115. #private;
  116. /**
  117. * the basename of this path
  118. *
  119. * **Important**: *always* test the path name against any test string
  120. * usingthe {@link isNamed} method, and not by directly comparing this
  121. * string. Otherwise, unicode path strings that the system sees as identical
  122. * will not be properly treated as the same path, leading to incorrect
  123. * behavior and possible security issues.
  124. */
  125. name: string;
  126. /**
  127. * the Path entry corresponding to the path root.
  128. *
  129. * @internal
  130. */
  131. root: PathBase;
  132. /**
  133. * All roots found within the current PathScurry family
  134. *
  135. * @internal
  136. */
  137. roots: {
  138. [k: string]: PathBase;
  139. };
  140. /**
  141. * a reference to the parent path, or undefined in the case of root entries
  142. *
  143. * @internal
  144. */
  145. parent?: PathBase;
  146. /**
  147. * boolean indicating whether paths are compared case-insensitively
  148. * @internal
  149. */
  150. nocase: boolean;
  151. /**
  152. * boolean indicating that this path is the current working directory
  153. * of the PathScurry collection that contains it.
  154. */
  155. isCWD: boolean;
  156. /**
  157. * the string or regexp used to split paths. On posix, it is `'/'`, and on
  158. * windows it is a RegExp matching either `'/'` or `'\\'`
  159. */
  160. abstract splitSep: string | RegExp;
  161. /**
  162. * The path separator string to use when joining paths
  163. */
  164. abstract sep: string;
  165. get dev(): number | undefined;
  166. get mode(): number | undefined;
  167. get nlink(): number | undefined;
  168. get uid(): number | undefined;
  169. get gid(): number | undefined;
  170. get rdev(): number | undefined;
  171. get blksize(): number | undefined;
  172. get ino(): number | undefined;
  173. get size(): number | undefined;
  174. get blocks(): number | undefined;
  175. get atimeMs(): number | undefined;
  176. get mtimeMs(): number | undefined;
  177. get ctimeMs(): number | undefined;
  178. get birthtimeMs(): number | undefined;
  179. get atime(): Date | undefined;
  180. get mtime(): Date | undefined;
  181. get ctime(): Date | undefined;
  182. get birthtime(): Date | undefined;
  183. /**
  184. * This property is for compatibility with the Dirent class as of
  185. * Node v20, where Dirent['parentPath'] refers to the path of the
  186. * directory that was passed to readdir. For root entries, it's the path
  187. * to the entry itself.
  188. */
  189. get parentPath(): string;
  190. /**
  191. * Deprecated alias for Dirent['parentPath'] Somewhat counterintuitively,
  192. * this property refers to the *parent* path, not the path object itself.
  193. *
  194. * @deprecated
  195. */
  196. get path(): string;
  197. /**
  198. * Do not create new Path objects directly. They should always be accessed
  199. * via the PathScurry class or other methods on the Path class.
  200. *
  201. * @internal
  202. */
  203. constructor(name: string, type: number | undefined, root: PathBase | undefined, roots: {
  204. [k: string]: PathBase;
  205. }, nocase: boolean, children: ChildrenCache, opts: PathOpts);
  206. /**
  207. * Returns the depth of the Path object from its root.
  208. *
  209. * For example, a path at `/foo/bar` would have a depth of 2.
  210. */
  211. depth(): number;
  212. /**
  213. * @internal
  214. */
  215. abstract getRootString(path: string): string;
  216. /**
  217. * @internal
  218. */
  219. abstract getRoot(rootPath: string): PathBase;
  220. /**
  221. * @internal
  222. */
  223. abstract newChild(name: string, type?: number, opts?: PathOpts): PathBase;
  224. /**
  225. * @internal
  226. */
  227. childrenCache(): ChildrenCache;
  228. /**
  229. * Get the Path object referenced by the string path, resolved from this Path
  230. */
  231. resolve(path?: string): PathBase;
  232. /**
  233. * Returns the cached children Path objects, if still available. If they
  234. * have fallen out of the cache, then returns an empty array, and resets the
  235. * READDIR_CALLED bit, so that future calls to readdir() will require an fs
  236. * lookup.
  237. *
  238. * @internal
  239. */
  240. children(): Children;
  241. /**
  242. * Resolves a path portion and returns or creates the child Path.
  243. *
  244. * Returns `this` if pathPart is `''` or `'.'`, or `parent` if pathPart is
  245. * `'..'`.
  246. *
  247. * This should not be called directly. If `pathPart` contains any path
  248. * separators, it will lead to unsafe undefined behavior.
  249. *
  250. * Use `Path.resolve()` instead.
  251. *
  252. * @internal
  253. */
  254. child(pathPart: string, opts?: PathOpts): PathBase;
  255. /**
  256. * The relative path from the cwd. If it does not share an ancestor with
  257. * the cwd, then this ends up being equivalent to the fullpath()
  258. */
  259. relative(): string;
  260. /**
  261. * The relative path from the cwd, using / as the path separator.
  262. * If it does not share an ancestor with
  263. * the cwd, then this ends up being equivalent to the fullpathPosix()
  264. * On posix systems, this is identical to relative().
  265. */
  266. relativePosix(): string;
  267. /**
  268. * The fully resolved path string for this Path entry
  269. */
  270. fullpath(): string;
  271. /**
  272. * On platforms other than windows, this is identical to fullpath.
  273. *
  274. * On windows, this is overridden to return the forward-slash form of the
  275. * full UNC path.
  276. */
  277. fullpathPosix(): string;
  278. /**
  279. * Is the Path of an unknown type?
  280. *
  281. * Note that we might know *something* about it if there has been a previous
  282. * filesystem operation, for example that it does not exist, or is not a
  283. * link, or whether it has child entries.
  284. */
  285. isUnknown(): boolean;
  286. isType(type: Type): boolean;
  287. getType(): Type;
  288. /**
  289. * Is the Path a regular file?
  290. */
  291. isFile(): boolean;
  292. /**
  293. * Is the Path a directory?
  294. */
  295. isDirectory(): boolean;
  296. /**
  297. * Is the path a character device?
  298. */
  299. isCharacterDevice(): boolean;
  300. /**
  301. * Is the path a block device?
  302. */
  303. isBlockDevice(): boolean;
  304. /**
  305. * Is the path a FIFO pipe?
  306. */
  307. isFIFO(): boolean;
  308. /**
  309. * Is the path a socket?
  310. */
  311. isSocket(): boolean;
  312. /**
  313. * Is the path a symbolic link?
  314. */
  315. isSymbolicLink(): boolean;
  316. /**
  317. * Return the entry if it has been subject of a successful lstat, or
  318. * undefined otherwise.
  319. *
  320. * Does not read the filesystem, so an undefined result *could* simply
  321. * mean that we haven't called lstat on it.
  322. */
  323. lstatCached(): PathBase | undefined;
  324. /**
  325. * Return the cached link target if the entry has been the subject of a
  326. * successful readlink, or undefined otherwise.
  327. *
  328. * Does not read the filesystem, so an undefined result *could* just mean we
  329. * don't have any cached data. Only use it if you are very sure that a
  330. * readlink() has been called at some point.
  331. */
  332. readlinkCached(): PathBase | undefined;
  333. /**
  334. * Returns the cached realpath target if the entry has been the subject
  335. * of a successful realpath, or undefined otherwise.
  336. *
  337. * Does not read the filesystem, so an undefined result *could* just mean we
  338. * don't have any cached data. Only use it if you are very sure that a
  339. * realpath() has been called at some point.
  340. */
  341. realpathCached(): PathBase | undefined;
  342. /**
  343. * Returns the cached child Path entries array if the entry has been the
  344. * subject of a successful readdir(), or [] otherwise.
  345. *
  346. * Does not read the filesystem, so an empty array *could* just mean we
  347. * don't have any cached data. Only use it if you are very sure that a
  348. * readdir() has been called recently enough to still be valid.
  349. */
  350. readdirCached(): PathBase[];
  351. /**
  352. * Return true if it's worth trying to readlink. Ie, we don't (yet) have
  353. * any indication that readlink will definitely fail.
  354. *
  355. * Returns false if the path is known to not be a symlink, if a previous
  356. * readlink failed, or if the entry does not exist.
  357. */
  358. canReadlink(): boolean;
  359. /**
  360. * Return true if readdir has previously been successfully called on this
  361. * path, indicating that cachedReaddir() is likely valid.
  362. */
  363. calledReaddir(): boolean;
  364. /**
  365. * Returns true if the path is known to not exist. That is, a previous lstat
  366. * or readdir failed to verify its existence when that would have been
  367. * expected, or a parent entry was marked either enoent or enotdir.
  368. */
  369. isENOENT(): boolean;
  370. /**
  371. * Return true if the path is a match for the given path name. This handles
  372. * case sensitivity and unicode normalization.
  373. *
  374. * Note: even on case-sensitive systems, it is **not** safe to test the
  375. * equality of the `.name` property to determine whether a given pathname
  376. * matches, due to unicode normalization mismatches.
  377. *
  378. * Always use this method instead of testing the `path.name` property
  379. * directly.
  380. */
  381. isNamed(n: string): boolean;
  382. /**
  383. * Return the Path object corresponding to the target of a symbolic link.
  384. *
  385. * If the Path is not a symbolic link, or if the readlink call fails for any
  386. * reason, `undefined` is returned.
  387. *
  388. * Result is cached, and thus may be outdated if the filesystem is mutated.
  389. */
  390. readlink(): Promise<PathBase | undefined>;
  391. /**
  392. * Synchronous {@link PathBase.readlink}
  393. */
  394. readlinkSync(): PathBase | undefined;
  395. /**
  396. * Call lstat() on this Path, and update all known information that can be
  397. * determined.
  398. *
  399. * Note that unlike `fs.lstat()`, the returned value does not contain some
  400. * information, such as `mode`, `dev`, `nlink`, and `ino`. If that
  401. * information is required, you will need to call `fs.lstat` yourself.
  402. *
  403. * If the Path refers to a nonexistent file, or if the lstat call fails for
  404. * any reason, `undefined` is returned. Otherwise the updated Path object is
  405. * returned.
  406. *
  407. * Results are cached, and thus may be out of date if the filesystem is
  408. * mutated.
  409. */
  410. lstat(): Promise<PathBase | undefined>;
  411. /**
  412. * synchronous {@link PathBase.lstat}
  413. */
  414. lstatSync(): PathBase | undefined;
  415. /**
  416. * Standard node-style callback interface to get list of directory entries.
  417. *
  418. * If the Path cannot or does not contain any children, then an empty array
  419. * is returned.
  420. *
  421. * Results are cached, and thus may be out of date if the filesystem is
  422. * mutated.
  423. *
  424. * @param cb The callback called with (er, entries). Note that the `er`
  425. * param is somewhat extraneous, as all readdir() errors are handled and
  426. * simply result in an empty set of entries being returned.
  427. * @param allowZalgo Boolean indicating that immediately known results should
  428. * *not* be deferred with `queueMicrotask`. Defaults to `false`. Release
  429. * zalgo at your peril, the dark pony lord is devious and unforgiving.
  430. */
  431. readdirCB(cb: (er: NodeJS.ErrnoException | null, entries: PathBase[]) => any, allowZalgo?: boolean): void;
  432. /**
  433. * Return an array of known child entries.
  434. *
  435. * If the Path cannot or does not contain any children, then an empty array
  436. * is returned.
  437. *
  438. * Results are cached, and thus may be out of date if the filesystem is
  439. * mutated.
  440. */
  441. readdir(): Promise<PathBase[]>;
  442. /**
  443. * synchronous {@link PathBase.readdir}
  444. */
  445. readdirSync(): PathBase[];
  446. canReaddir(): boolean;
  447. shouldWalk(dirs: Set<PathBase | undefined>, walkFilter?: (e: PathBase) => boolean): boolean;
  448. /**
  449. * Return the Path object corresponding to path as resolved
  450. * by realpath(3).
  451. *
  452. * If the realpath call fails for any reason, `undefined` is returned.
  453. *
  454. * Result is cached, and thus may be outdated if the filesystem is mutated.
  455. * On success, returns a Path object.
  456. */
  457. realpath(): Promise<PathBase | undefined>;
  458. /**
  459. * Synchronous {@link realpath}
  460. */
  461. realpathSync(): PathBase | undefined;
  462. /**
  463. * Internal method to mark this Path object as the scurry cwd,
  464. * called by {@link PathScurry#chdir}
  465. *
  466. * @internal
  467. */
  468. [setAsCwd](oldCwd: PathBase): void;
  469. }
  470. /**
  471. * Path class used on win32 systems
  472. *
  473. * Uses `'\\'` as the path separator for returned paths, either `'\\'` or `'/'`
  474. * as the path separator for parsing paths.
  475. */
  476. export declare class PathWin32 extends PathBase {
  477. /**
  478. * Separator for generating path strings.
  479. */
  480. sep: '\\';
  481. /**
  482. * Separator for parsing path strings.
  483. */
  484. splitSep: RegExp;
  485. /**
  486. * Do not create new Path objects directly. They should always be accessed
  487. * via the PathScurry class or other methods on the Path class.
  488. *
  489. * @internal
  490. */
  491. constructor(name: string, type: number | undefined, root: PathBase | undefined, roots: {
  492. [k: string]: PathBase;
  493. }, nocase: boolean, children: ChildrenCache, opts: PathOpts);
  494. /**
  495. * @internal
  496. */
  497. newChild(name: string, type?: number, opts?: PathOpts): PathWin32;
  498. /**
  499. * @internal
  500. */
  501. getRootString(path: string): string;
  502. /**
  503. * @internal
  504. */
  505. getRoot(rootPath: string): PathBase;
  506. /**
  507. * @internal
  508. */
  509. sameRoot(rootPath: string, compare?: string): boolean;
  510. }
  511. /**
  512. * Path class used on all posix systems.
  513. *
  514. * Uses `'/'` as the path separator.
  515. */
  516. export declare class PathPosix extends PathBase {
  517. /**
  518. * separator for parsing path strings
  519. */
  520. splitSep: '/';
  521. /**
  522. * separator for generating path strings
  523. */
  524. sep: '/';
  525. /**
  526. * Do not create new Path objects directly. They should always be accessed
  527. * via the PathScurry class or other methods on the Path class.
  528. *
  529. * @internal
  530. */
  531. constructor(name: string, type: number | undefined, root: PathBase | undefined, roots: {
  532. [k: string]: PathBase;
  533. }, nocase: boolean, children: ChildrenCache, opts: PathOpts);
  534. /**
  535. * @internal
  536. */
  537. getRootString(path: string): string;
  538. /**
  539. * @internal
  540. */
  541. getRoot(_rootPath: string): PathBase;
  542. /**
  543. * @internal
  544. */
  545. newChild(name: string, type?: number, opts?: PathOpts): PathPosix;
  546. }
  547. /**
  548. * Options that may be provided to the PathScurry constructor
  549. */
  550. export interface PathScurryOpts {
  551. /**
  552. * perform case-insensitive path matching. Default based on platform
  553. * subclass.
  554. */
  555. nocase?: boolean;
  556. /**
  557. * Number of Path entries to keep in the cache of Path child references.
  558. *
  559. * Setting this higher than 65536 will dramatically increase the data
  560. * consumption and construction time overhead of each PathScurry.
  561. *
  562. * Setting this value to 256 or lower will significantly reduce the data
  563. * consumption and construction time overhead, but may also reduce resolve()
  564. * and readdir() performance on large filesystems.
  565. *
  566. * Default `16384`.
  567. */
  568. childrenCacheSize?: number;
  569. /**
  570. * An object that overrides the built-in functions from the fs and
  571. * fs/promises modules.
  572. *
  573. * See {@link FSOption}
  574. */
  575. fs?: FSOption;
  576. }
  577. /**
  578. * The base class for all PathScurry classes, providing the interface for path
  579. * resolution and filesystem operations.
  580. *
  581. * Typically, you should *not* instantiate this class directly, but rather one
  582. * of the platform-specific classes, or the exported {@link PathScurry} which
  583. * defaults to the current platform.
  584. */
  585. export declare abstract class PathScurryBase {
  586. #private;
  587. /**
  588. * The root Path entry for the current working directory of this Scurry
  589. */
  590. root: PathBase;
  591. /**
  592. * The string path for the root of this Scurry's current working directory
  593. */
  594. rootPath: string;
  595. /**
  596. * A collection of all roots encountered, referenced by rootPath
  597. */
  598. roots: {
  599. [k: string]: PathBase;
  600. };
  601. /**
  602. * The Path entry corresponding to this PathScurry's current working directory.
  603. */
  604. cwd: PathBase;
  605. /**
  606. * Perform path comparisons case-insensitively.
  607. *
  608. * Defaults true on Darwin and Windows systems, false elsewhere.
  609. */
  610. nocase: boolean;
  611. /**
  612. * The path separator used for parsing paths
  613. *
  614. * `'/'` on Posix systems, either `'/'` or `'\\'` on Windows
  615. */
  616. abstract sep: string | RegExp;
  617. /**
  618. * This class should not be instantiated directly.
  619. *
  620. * Use PathScurryWin32, PathScurryDarwin, PathScurryPosix, or PathScurry
  621. *
  622. * @internal
  623. */
  624. constructor(cwd: (URL | string) | undefined, pathImpl: typeof win32 | typeof posix, sep: string | RegExp, { nocase, childrenCacheSize, fs, }?: PathScurryOpts);
  625. /**
  626. * Get the depth of a provided path, string, or the cwd
  627. */
  628. depth(path?: Path | string): number;
  629. /**
  630. * Parse the root portion of a path string
  631. *
  632. * @internal
  633. */
  634. abstract parseRootPath(dir: string): string;
  635. /**
  636. * create a new Path to use as root during construction.
  637. *
  638. * @internal
  639. */
  640. abstract newRoot(fs: FSValue): PathBase;
  641. /**
  642. * Determine whether a given path string is absolute
  643. */
  644. abstract isAbsolute(p: string): boolean;
  645. /**
  646. * Return the cache of child entries. Exposed so subclasses can create
  647. * child Path objects in a platform-specific way.
  648. *
  649. * @internal
  650. */
  651. childrenCache(): ChildrenCache;
  652. /**
  653. * Resolve one or more path strings to a resolved string
  654. *
  655. * Same interface as require('path').resolve.
  656. *
  657. * Much faster than path.resolve() when called multiple times for the same
  658. * path, because the resolved Path objects are cached. Much slower
  659. * otherwise.
  660. */
  661. resolve(...paths: string[]): string;
  662. /**
  663. * Resolve one or more path strings to a resolved string, returning
  664. * the posix path. Identical to .resolve() on posix systems, but on
  665. * windows will return a forward-slash separated UNC path.
  666. *
  667. * Same interface as require('path').resolve.
  668. *
  669. * Much faster than path.resolve() when called multiple times for the same
  670. * path, because the resolved Path objects are cached. Much slower
  671. * otherwise.
  672. */
  673. resolvePosix(...paths: string[]): string;
  674. /**
  675. * find the relative path from the cwd to the supplied path string or entry
  676. */
  677. relative(entry?: PathBase | string): string;
  678. /**
  679. * find the relative path from the cwd to the supplied path string or
  680. * entry, using / as the path delimiter, even on Windows.
  681. */
  682. relativePosix(entry?: PathBase | string): string;
  683. /**
  684. * Return the basename for the provided string or Path object
  685. */
  686. basename(entry?: PathBase | string): string;
  687. /**
  688. * Return the dirname for the provided string or Path object
  689. */
  690. dirname(entry?: PathBase | string): string;
  691. /**
  692. * Return an array of known child entries.
  693. *
  694. * First argument may be either a string, or a Path object.
  695. *
  696. * If the Path cannot or does not contain any children, then an empty array
  697. * is returned.
  698. *
  699. * Results are cached, and thus may be out of date if the filesystem is
  700. * mutated.
  701. *
  702. * Unlike `fs.readdir()`, the `withFileTypes` option defaults to `true`. Set
  703. * `{ withFileTypes: false }` to return strings.
  704. */
  705. readdir(): Promise<PathBase[]>;
  706. readdir(opts: {
  707. withFileTypes: true;
  708. }): Promise<PathBase[]>;
  709. readdir(opts: {
  710. withFileTypes: false;
  711. }): Promise<string[]>;
  712. readdir(opts: {
  713. withFileTypes: boolean;
  714. }): Promise<PathBase[] | string[]>;
  715. readdir(entry: PathBase | string): Promise<PathBase[]>;
  716. readdir(entry: PathBase | string, opts: {
  717. withFileTypes: true;
  718. }): Promise<PathBase[]>;
  719. readdir(entry: PathBase | string, opts: {
  720. withFileTypes: false;
  721. }): Promise<string[]>;
  722. readdir(entry: PathBase | string, opts: {
  723. withFileTypes: boolean;
  724. }): Promise<PathBase[] | string[]>;
  725. /**
  726. * synchronous {@link PathScurryBase.readdir}
  727. */
  728. readdirSync(): PathBase[];
  729. readdirSync(opts: {
  730. withFileTypes: true;
  731. }): PathBase[];
  732. readdirSync(opts: {
  733. withFileTypes: false;
  734. }): string[];
  735. readdirSync(opts: {
  736. withFileTypes: boolean;
  737. }): PathBase[] | string[];
  738. readdirSync(entry: PathBase | string): PathBase[];
  739. readdirSync(entry: PathBase | string, opts: {
  740. withFileTypes: true;
  741. }): PathBase[];
  742. readdirSync(entry: PathBase | string, opts: {
  743. withFileTypes: false;
  744. }): string[];
  745. readdirSync(entry: PathBase | string, opts: {
  746. withFileTypes: boolean;
  747. }): PathBase[] | string[];
  748. /**
  749. * Call lstat() on the string or Path object, and update all known
  750. * information that can be determined.
  751. *
  752. * Note that unlike `fs.lstat()`, the returned value does not contain some
  753. * information, such as `mode`, `dev`, `nlink`, and `ino`. If that
  754. * information is required, you will need to call `fs.lstat` yourself.
  755. *
  756. * If the Path refers to a nonexistent file, or if the lstat call fails for
  757. * any reason, `undefined` is returned. Otherwise the updated Path object is
  758. * returned.
  759. *
  760. * Results are cached, and thus may be out of date if the filesystem is
  761. * mutated.
  762. */
  763. lstat(entry?: string | PathBase): Promise<PathBase | undefined>;
  764. /**
  765. * synchronous {@link PathScurryBase.lstat}
  766. */
  767. lstatSync(entry?: string | PathBase): PathBase | undefined;
  768. /**
  769. * Return the Path object or string path corresponding to the target of a
  770. * symbolic link.
  771. *
  772. * If the path is not a symbolic link, or if the readlink call fails for any
  773. * reason, `undefined` is returned.
  774. *
  775. * Result is cached, and thus may be outdated if the filesystem is mutated.
  776. *
  777. * `{withFileTypes}` option defaults to `false`.
  778. *
  779. * On success, returns a Path object if `withFileTypes` option is true,
  780. * otherwise a string.
  781. */
  782. readlink(): Promise<string | undefined>;
  783. readlink(opt: {
  784. withFileTypes: false;
  785. }): Promise<string | undefined>;
  786. readlink(opt: {
  787. withFileTypes: true;
  788. }): Promise<PathBase | undefined>;
  789. readlink(opt: {
  790. withFileTypes: boolean;
  791. }): Promise<PathBase | string | undefined>;
  792. readlink(entry: string | PathBase, opt?: {
  793. withFileTypes: false;
  794. }): Promise<string | undefined>;
  795. readlink(entry: string | PathBase, opt: {
  796. withFileTypes: true;
  797. }): Promise<PathBase | undefined>;
  798. readlink(entry: string | PathBase, opt: {
  799. withFileTypes: boolean;
  800. }): Promise<string | PathBase | undefined>;
  801. /**
  802. * synchronous {@link PathScurryBase.readlink}
  803. */
  804. readlinkSync(): string | undefined;
  805. readlinkSync(opt: {
  806. withFileTypes: false;
  807. }): string | undefined;
  808. readlinkSync(opt: {
  809. withFileTypes: true;
  810. }): PathBase | undefined;
  811. readlinkSync(opt: {
  812. withFileTypes: boolean;
  813. }): PathBase | string | undefined;
  814. readlinkSync(entry: string | PathBase, opt?: {
  815. withFileTypes: false;
  816. }): string | undefined;
  817. readlinkSync(entry: string | PathBase, opt: {
  818. withFileTypes: true;
  819. }): PathBase | undefined;
  820. readlinkSync(entry: string | PathBase, opt: {
  821. withFileTypes: boolean;
  822. }): string | PathBase | undefined;
  823. /**
  824. * Return the Path object or string path corresponding to path as resolved
  825. * by realpath(3).
  826. *
  827. * If the realpath call fails for any reason, `undefined` is returned.
  828. *
  829. * Result is cached, and thus may be outdated if the filesystem is mutated.
  830. *
  831. * `{withFileTypes}` option defaults to `false`.
  832. *
  833. * On success, returns a Path object if `withFileTypes` option is true,
  834. * otherwise a string.
  835. */
  836. realpath(): Promise<string | undefined>;
  837. realpath(opt: {
  838. withFileTypes: false;
  839. }): Promise<string | undefined>;
  840. realpath(opt: {
  841. withFileTypes: true;
  842. }): Promise<PathBase | undefined>;
  843. realpath(opt: {
  844. withFileTypes: boolean;
  845. }): Promise<PathBase | string | undefined>;
  846. realpath(entry: string | PathBase, opt?: {
  847. withFileTypes: false;
  848. }): Promise<string | undefined>;
  849. realpath(entry: string | PathBase, opt: {
  850. withFileTypes: true;
  851. }): Promise<PathBase | undefined>;
  852. realpath(entry: string | PathBase, opt: {
  853. withFileTypes: boolean;
  854. }): Promise<string | PathBase | undefined>;
  855. realpathSync(): string | undefined;
  856. realpathSync(opt: {
  857. withFileTypes: false;
  858. }): string | undefined;
  859. realpathSync(opt: {
  860. withFileTypes: true;
  861. }): PathBase | undefined;
  862. realpathSync(opt: {
  863. withFileTypes: boolean;
  864. }): PathBase | string | undefined;
  865. realpathSync(entry: string | PathBase, opt?: {
  866. withFileTypes: false;
  867. }): string | undefined;
  868. realpathSync(entry: string | PathBase, opt: {
  869. withFileTypes: true;
  870. }): PathBase | undefined;
  871. realpathSync(entry: string | PathBase, opt: {
  872. withFileTypes: boolean;
  873. }): string | PathBase | undefined;
  874. /**
  875. * Asynchronously walk the directory tree, returning an array of
  876. * all path strings or Path objects found.
  877. *
  878. * Note that this will be extremely memory-hungry on large filesystems.
  879. * In such cases, it may be better to use the stream or async iterator
  880. * walk implementation.
  881. */
  882. walk(): Promise<PathBase[]>;
  883. walk(opts: WalkOptionsWithFileTypesTrue | WalkOptionsWithFileTypesUnset): Promise<PathBase[]>;
  884. walk(opts: WalkOptionsWithFileTypesFalse): Promise<string[]>;
  885. walk(opts: WalkOptions): Promise<string[] | PathBase[]>;
  886. walk(entry: string | PathBase): Promise<PathBase[]>;
  887. walk(entry: string | PathBase, opts: WalkOptionsWithFileTypesTrue | WalkOptionsWithFileTypesUnset): Promise<PathBase[]>;
  888. walk(entry: string | PathBase, opts: WalkOptionsWithFileTypesFalse): Promise<string[]>;
  889. walk(entry: string | PathBase, opts: WalkOptions): Promise<PathBase[] | string[]>;
  890. /**
  891. * Synchronously walk the directory tree, returning an array of
  892. * all path strings or Path objects found.
  893. *
  894. * Note that this will be extremely memory-hungry on large filesystems.
  895. * In such cases, it may be better to use the stream or async iterator
  896. * walk implementation.
  897. */
  898. walkSync(): PathBase[];
  899. walkSync(opts: WalkOptionsWithFileTypesTrue | WalkOptionsWithFileTypesUnset): PathBase[];
  900. walkSync(opts: WalkOptionsWithFileTypesFalse): string[];
  901. walkSync(opts: WalkOptions): string[] | PathBase[];
  902. walkSync(entry: string | PathBase): PathBase[];
  903. walkSync(entry: string | PathBase, opts: WalkOptionsWithFileTypesUnset | WalkOptionsWithFileTypesTrue): PathBase[];
  904. walkSync(entry: string | PathBase, opts: WalkOptionsWithFileTypesFalse): string[];
  905. walkSync(entry: string | PathBase, opts: WalkOptions): PathBase[] | string[];
  906. /**
  907. * Support for `for await`
  908. *
  909. * Alias for {@link PathScurryBase.iterate}
  910. *
  911. * Note: As of Node 19, this is very slow, compared to other methods of
  912. * walking. Consider using {@link PathScurryBase.stream} if memory overhead
  913. * and backpressure are concerns, or {@link PathScurryBase.walk} if not.
  914. */
  915. [Symbol.asyncIterator](): AsyncGenerator<PathBase, void, void>;
  916. /**
  917. * Async generator form of {@link PathScurryBase.walk}
  918. *
  919. * Note: As of Node 19, this is very slow, compared to other methods of
  920. * walking, especially if most/all of the directory tree has been previously
  921. * walked. Consider using {@link PathScurryBase.stream} if memory overhead
  922. * and backpressure are concerns, or {@link PathScurryBase.walk} if not.
  923. */
  924. iterate(): AsyncGenerator<PathBase, void, void>;
  925. iterate(opts: WalkOptionsWithFileTypesTrue | WalkOptionsWithFileTypesUnset): AsyncGenerator<PathBase, void, void>;
  926. iterate(opts: WalkOptionsWithFileTypesFalse): AsyncGenerator<string, void, void>;
  927. iterate(opts: WalkOptions): AsyncGenerator<string | PathBase, void, void>;
  928. iterate(entry: string | PathBase): AsyncGenerator<PathBase, void, void>;
  929. iterate(entry: string | PathBase, opts: WalkOptionsWithFileTypesTrue | WalkOptionsWithFileTypesUnset): AsyncGenerator<PathBase, void, void>;
  930. iterate(entry: string | PathBase, opts: WalkOptionsWithFileTypesFalse): AsyncGenerator<string, void, void>;
  931. iterate(entry: string | PathBase, opts: WalkOptions): AsyncGenerator<PathBase | string, void, void>;
  932. /**
  933. * Iterating over a PathScurry performs a synchronous walk.
  934. *
  935. * Alias for {@link PathScurryBase.iterateSync}
  936. */
  937. [Symbol.iterator](): Generator<PathBase, void, void>;
  938. iterateSync(): Generator<PathBase, void, void>;
  939. iterateSync(opts: WalkOptionsWithFileTypesTrue | WalkOptionsWithFileTypesUnset): Generator<PathBase, void, void>;
  940. iterateSync(opts: WalkOptionsWithFileTypesFalse): Generator<string, void, void>;
  941. iterateSync(opts: WalkOptions): Generator<string | PathBase, void, void>;
  942. iterateSync(entry: string | PathBase): Generator<PathBase, void, void>;
  943. iterateSync(entry: string | PathBase, opts: WalkOptionsWithFileTypesTrue | WalkOptionsWithFileTypesUnset): Generator<PathBase, void, void>;
  944. iterateSync(entry: string | PathBase, opts: WalkOptionsWithFileTypesFalse): Generator<string, void, void>;
  945. iterateSync(entry: string | PathBase, opts: WalkOptions): Generator<PathBase | string, void, void>;
  946. /**
  947. * Stream form of {@link PathScurryBase.walk}
  948. *
  949. * Returns a Minipass stream that emits {@link PathBase} objects by default,
  950. * or strings if `{ withFileTypes: false }` is set in the options.
  951. */
  952. stream(): Minipass<PathBase>;
  953. stream(opts: WalkOptionsWithFileTypesTrue | WalkOptionsWithFileTypesUnset): Minipass<PathBase>;
  954. stream(opts: WalkOptionsWithFileTypesFalse): Minipass<string>;
  955. stream(opts: WalkOptions): Minipass<string | PathBase>;
  956. stream(entry: string | PathBase): Minipass<PathBase>;
  957. stream(entry: string | PathBase, opts: WalkOptionsWithFileTypesUnset | WalkOptionsWithFileTypesTrue): Minipass<PathBase>;
  958. stream(entry: string | PathBase, opts: WalkOptionsWithFileTypesFalse): Minipass<string>;
  959. stream(entry: string | PathBase, opts: WalkOptions): Minipass<string> | Minipass<PathBase>;
  960. /**
  961. * Synchronous form of {@link PathScurryBase.stream}
  962. *
  963. * Returns a Minipass stream that emits {@link PathBase} objects by default,
  964. * or strings if `{ withFileTypes: false }` is set in the options.
  965. *
  966. * Will complete the walk in a single tick if the stream is consumed fully.
  967. * Otherwise, will pause as needed for stream backpressure.
  968. */
  969. streamSync(): Minipass<PathBase>;
  970. streamSync(opts: WalkOptionsWithFileTypesTrue | WalkOptionsWithFileTypesUnset): Minipass<PathBase>;
  971. streamSync(opts: WalkOptionsWithFileTypesFalse): Minipass<string>;
  972. streamSync(opts: WalkOptions): Minipass<string | PathBase>;
  973. streamSync(entry: string | PathBase): Minipass<PathBase>;
  974. streamSync(entry: string | PathBase, opts: WalkOptionsWithFileTypesUnset | WalkOptionsWithFileTypesTrue): Minipass<PathBase>;
  975. streamSync(entry: string | PathBase, opts: WalkOptionsWithFileTypesFalse): Minipass<string>;
  976. streamSync(entry: string | PathBase, opts: WalkOptions): Minipass<string> | Minipass<PathBase>;
  977. chdir(path?: string | Path): void;
  978. }
  979. /**
  980. * Options provided to all walk methods.
  981. */
  982. export interface WalkOptions {
  983. /**
  984. * Return results as {@link PathBase} objects rather than strings.
  985. * When set to false, results are fully resolved paths, as returned by
  986. * {@link PathBase.fullpath}.
  987. * @default true
  988. */
  989. withFileTypes?: boolean;
  990. /**
  991. * Attempt to read directory entries from symbolic links. Otherwise, only
  992. * actual directories are traversed. Regardless of this setting, a given
  993. * target path will only ever be walked once, meaning that a symbolic link
  994. * to a previously traversed directory will never be followed.
  995. *
  996. * Setting this imposes a slight performance penalty, because `readlink`
  997. * must be called on all symbolic links encountered, in order to avoid
  998. * infinite cycles.
  999. * @default false
  1000. */
  1001. follow?: boolean;
  1002. /**
  1003. * Only return entries where the provided function returns true.
  1004. *
  1005. * This will not prevent directories from being traversed, even if they do
  1006. * not pass the filter, though it will prevent directories themselves from
  1007. * being included in the result set. See {@link walkFilter}
  1008. *
  1009. * Asynchronous functions are not supported here.
  1010. *
  1011. * By default, if no filter is provided, all entries and traversed
  1012. * directories are included.
  1013. */
  1014. filter?: (entry: PathBase) => boolean;
  1015. /**
  1016. * Only traverse directories (and in the case of {@link follow} being set to
  1017. * true, symbolic links to directories) if the provided function returns
  1018. * true.
  1019. *
  1020. * This will not prevent directories from being included in the result set,
  1021. * even if they do not pass the supplied filter function. See {@link filter}
  1022. * to do that.
  1023. *
  1024. * Asynchronous functions are not supported here.
  1025. */
  1026. walkFilter?: (entry: PathBase) => boolean;
  1027. }
  1028. export type WalkOptionsWithFileTypesUnset = WalkOptions & {
  1029. withFileTypes?: undefined;
  1030. };
  1031. export type WalkOptionsWithFileTypesTrue = WalkOptions & {
  1032. withFileTypes: true;
  1033. };
  1034. export type WalkOptionsWithFileTypesFalse = WalkOptions & {
  1035. withFileTypes: false;
  1036. };
  1037. /**
  1038. * Windows implementation of {@link PathScurryBase}
  1039. *
  1040. * Defaults to case insensitve, uses `'\\'` to generate path strings. Uses
  1041. * {@link PathWin32} for Path objects.
  1042. */
  1043. export declare class PathScurryWin32 extends PathScurryBase {
  1044. /**
  1045. * separator for generating path strings
  1046. */
  1047. sep: '\\';
  1048. constructor(cwd?: URL | string, opts?: PathScurryOpts);
  1049. /**
  1050. * @internal
  1051. */
  1052. parseRootPath(dir: string): string;
  1053. /**
  1054. * @internal
  1055. */
  1056. newRoot(fs: FSValue): PathWin32;
  1057. /**
  1058. * Return true if the provided path string is an absolute path
  1059. */
  1060. isAbsolute(p: string): boolean;
  1061. }
  1062. /**
  1063. * {@link PathScurryBase} implementation for all posix systems other than Darwin.
  1064. *
  1065. * Defaults to case-sensitive matching, uses `'/'` to generate path strings.
  1066. *
  1067. * Uses {@link PathPosix} for Path objects.
  1068. */
  1069. export declare class PathScurryPosix extends PathScurryBase {
  1070. /**
  1071. * separator for generating path strings
  1072. */
  1073. sep: '/';
  1074. constructor(cwd?: URL | string, opts?: PathScurryOpts);
  1075. /**
  1076. * @internal
  1077. */
  1078. parseRootPath(_dir: string): string;
  1079. /**
  1080. * @internal
  1081. */
  1082. newRoot(fs: FSValue): PathPosix;
  1083. /**
  1084. * Return true if the provided path string is an absolute path
  1085. */
  1086. isAbsolute(p: string): boolean;
  1087. }
  1088. /**
  1089. * {@link PathScurryBase} implementation for Darwin (macOS) systems.
  1090. *
  1091. * Defaults to case-insensitive matching, uses `'/'` for generating path
  1092. * strings.
  1093. *
  1094. * Uses {@link PathPosix} for Path objects.
  1095. */
  1096. export declare class PathScurryDarwin extends PathScurryPosix {
  1097. constructor(cwd?: URL | string, opts?: PathScurryOpts);
  1098. }
  1099. /**
  1100. * Default {@link PathBase} implementation for the current platform.
  1101. *
  1102. * {@link PathWin32} on Windows systems, {@link PathPosix} on all others.
  1103. */
  1104. export declare const Path: typeof PathWin32 | typeof PathPosix;
  1105. export type Path = PathBase | InstanceType<typeof Path>;
  1106. /**
  1107. * Default {@link PathScurryBase} implementation for the current platform.
  1108. *
  1109. * {@link PathScurryWin32} on Windows systems, {@link PathScurryDarwin} on
  1110. * Darwin (macOS) systems, {@link PathScurryPosix} on all others.
  1111. */
  1112. export declare const PathScurry: typeof PathScurryWin32 | typeof PathScurryDarwin | typeof PathScurryPosix;
  1113. export type PathScurry = PathScurryBase | InstanceType<typeof PathScurry>;
  1114. export {};
  1115. //# sourceMappingURL=index.d.ts.map