index.d.ts 39 KB

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