index.d.ts 38 KB

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