index.d.cts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /// <reference types="node" />
  2. import picomatch from "picomatch";
  3. //#region src/api/queue.d.ts
  4. type OnQueueEmptyCallback = (error: Error | null, output: WalkerState) => void;
  5. /**
  6. * This is a custom stateless queue to track concurrent async fs calls.
  7. * It increments a counter whenever a call is queued and decrements it
  8. * as soon as it completes. When the counter hits 0, it calls onQueueEmpty.
  9. */
  10. declare class Queue {
  11. private onQueueEmpty?;
  12. count: number;
  13. constructor(onQueueEmpty?: OnQueueEmptyCallback | undefined);
  14. enqueue(): number;
  15. dequeue(error: Error | null, output: WalkerState): void;
  16. }
  17. //#endregion
  18. //#region src/types.d.ts
  19. type Counts = {
  20. files: number;
  21. directories: number;
  22. /**
  23. * @deprecated use `directories` instead. Will be removed in v7.0.
  24. */
  25. dirs: number;
  26. };
  27. type Group = {
  28. directory: string;
  29. files: string[];
  30. /**
  31. * @deprecated use `directory` instead. Will be removed in v7.0.
  32. */
  33. dir: string;
  34. };
  35. type GroupOutput = Group[];
  36. type OnlyCountsOutput = Counts;
  37. type PathsOutput = string[];
  38. type Output = OnlyCountsOutput | PathsOutput | GroupOutput;
  39. type WalkerState = {
  40. root: string;
  41. paths: string[];
  42. groups: Group[];
  43. counts: Counts;
  44. options: Options;
  45. queue: Queue;
  46. controller: AbortController;
  47. symlinks: Map<string, string>;
  48. visited: string[];
  49. };
  50. type ResultCallback<TOutput extends Output> = (error: Error | null, output: TOutput) => void;
  51. type FilterPredicate = (path: string, isDirectory: boolean) => boolean;
  52. type ExcludePredicate = (dirName: string, dirPath: string) => boolean;
  53. type PathSeparator = "/" | "\\";
  54. type Options<TGlobFunction = unknown> = {
  55. includeBasePath?: boolean;
  56. includeDirs?: boolean;
  57. normalizePath?: boolean;
  58. maxDepth: number;
  59. maxFiles?: number;
  60. resolvePaths?: boolean;
  61. suppressErrors: boolean;
  62. group?: boolean;
  63. onlyCounts?: boolean;
  64. filters: FilterPredicate[];
  65. resolveSymlinks?: boolean;
  66. useRealPaths?: boolean;
  67. excludeFiles?: boolean;
  68. excludeSymlinks?: boolean;
  69. exclude?: ExcludePredicate;
  70. relativePaths?: boolean;
  71. pathSeparator: PathSeparator;
  72. signal?: AbortSignal;
  73. globFunction?: TGlobFunction;
  74. };
  75. type GlobMatcher = (test: string) => boolean;
  76. type GlobFunction = (glob: string | string[], ...params: unknown[]) => GlobMatcher;
  77. type GlobParams<T> = T extends ((globs: string | string[], ...params: infer TParams extends unknown[]) => GlobMatcher) ? TParams : [];
  78. //#endregion
  79. //#region src/builder/api-builder.d.ts
  80. declare class APIBuilder<TReturnType extends Output> {
  81. private readonly root;
  82. private readonly options;
  83. constructor(root: string, options: Options);
  84. withPromise(): Promise<TReturnType>;
  85. withCallback(cb: ResultCallback<TReturnType>): void;
  86. sync(): TReturnType;
  87. }
  88. //#endregion
  89. //#region src/builder/index.d.ts
  90. declare class Builder<TReturnType extends Output = PathsOutput, TGlobFunction = typeof picomatch> {
  91. private readonly globCache;
  92. private options;
  93. private globFunction?;
  94. constructor(options?: Partial<Options<TGlobFunction>>);
  95. group(): Builder<GroupOutput, TGlobFunction>;
  96. withPathSeparator(separator: "/" | "\\"): this;
  97. withBasePath(): this;
  98. withRelativePaths(): this;
  99. withDirs(): this;
  100. withMaxDepth(depth: number): this;
  101. withMaxFiles(limit: number): this;
  102. withFullPaths(): this;
  103. withErrors(): this;
  104. withSymlinks({
  105. resolvePaths
  106. }?: {
  107. resolvePaths?: boolean | undefined;
  108. }): this;
  109. withAbortSignal(signal: AbortSignal): this;
  110. normalize(): this;
  111. filter(predicate: FilterPredicate): this;
  112. onlyDirs(): this;
  113. exclude(predicate: ExcludePredicate): this;
  114. onlyCounts(): Builder<OnlyCountsOutput, TGlobFunction>;
  115. crawl(root?: string): APIBuilder<TReturnType>;
  116. withGlobFunction<TFunc>(fn: TFunc): Builder<TReturnType, TFunc>;
  117. /**
  118. * @deprecated Pass options using the constructor instead:
  119. * ```ts
  120. * new fdir(options).crawl("/path/to/root");
  121. * ```
  122. * This method will be removed in v7.0
  123. */
  124. crawlWithOptions(root: string, options: Partial<Options<TGlobFunction>>): APIBuilder<TReturnType>;
  125. glob(...patterns: string[]): Builder<TReturnType, TGlobFunction>;
  126. globWithOptions(patterns: string[]): Builder<TReturnType, TGlobFunction>;
  127. globWithOptions(patterns: string[], ...options: GlobParams<TGlobFunction>): Builder<TReturnType, TGlobFunction>;
  128. }
  129. //#endregion
  130. //#region src/index.d.ts
  131. type Fdir = typeof Builder;
  132. //#endregion
  133. export { Counts, ExcludePredicate, Fdir, FilterPredicate, GlobFunction, GlobMatcher, GlobParams, Group, GroupOutput, OnlyCountsOutput, Options, Output, PathSeparator, PathsOutput, ResultCallback, WalkerState, Builder as fdir };