提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.0 KiB

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