index.d.ts 39 KB

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