FakeFS.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. /// <reference types="node" />
  4. /// <reference types="node" />
  5. /// <reference types="node" />
  6. import { EventEmitter } from 'events';
  7. import { Dirent as NodeDirent, ReadStream } from 'fs';
  8. import { Stats as NodeStats, WriteStream } from 'fs';
  9. import { NoParamCallback, BigIntStats as NodeBigIntStats } from 'fs';
  10. import { LinkStrategy } from './algorithms/copyPromise';
  11. import { FSPath, Path, PortablePath, PathUtils, Filename } from './path';
  12. export type Stats = NodeStats & {
  13. crc?: number;
  14. };
  15. export type BigIntStats = NodeBigIntStats & {
  16. crc?: number;
  17. };
  18. export type Dirent = Exclude<NodeDirent, 'name'> & {
  19. name: Filename;
  20. };
  21. export type Dir<P extends Path> = {
  22. readonly path: P;
  23. [Symbol.asyncIterator](): AsyncIterableIterator<Dirent>;
  24. close(): Promise<void>;
  25. close(cb: NoParamCallback): void;
  26. closeSync(): void;
  27. read(): Promise<Dirent | null>;
  28. read(cb: (err: NodeJS.ErrnoException | null, dirent: Dirent | null) => void): void;
  29. readSync(): Dirent | null;
  30. };
  31. export type OpendirOptions = Partial<{
  32. bufferSize: number;
  33. }>;
  34. export type CreateReadStreamOptions = Partial<{
  35. encoding: string;
  36. fd: number;
  37. }>;
  38. export type CreateWriteStreamOptions = Partial<{
  39. encoding: string;
  40. fd: number;
  41. flags: 'a';
  42. }>;
  43. export type MkdirOptions = Partial<{
  44. recursive: boolean;
  45. mode: number;
  46. }>;
  47. export type RmdirOptions = Partial<{
  48. maxRetries: number;
  49. recursive: boolean;
  50. retryDelay: number;
  51. }>;
  52. export type WriteFileOptions = Partial<{
  53. encoding: string;
  54. mode: number;
  55. flag: string;
  56. }> | string;
  57. export type WatchOptions = Partial<{
  58. persistent: boolean;
  59. recursive: boolean;
  60. encoding: string;
  61. }> | string;
  62. export type WatchFileOptions = Partial<{
  63. bigint: boolean;
  64. persistent: boolean;
  65. interval: number;
  66. }>;
  67. export type ChangeFileOptions = Partial<{
  68. automaticNewlines: boolean;
  69. mode: number;
  70. }>;
  71. export type WatchCallback = (eventType: string, filename: string) => void;
  72. export type Watcher = {
  73. on: any;
  74. close: () => void;
  75. };
  76. export type WatchFileCallback = (current: Stats, previous: Stats) => void;
  77. export type StatWatcher = EventEmitter & {
  78. ref?: () => StatWatcher;
  79. unref?: () => StatWatcher;
  80. };
  81. export type ExtractHintOptions = {
  82. relevantExtensions: Set<string>;
  83. };
  84. export type SymlinkType = 'file' | 'dir' | 'junction';
  85. export interface StatOptions {
  86. bigint?: boolean | undefined;
  87. }
  88. export interface StatSyncOptions extends StatOptions {
  89. throwIfNoEntry?: boolean | undefined;
  90. }
  91. export declare abstract class FakeFS<P extends Path> {
  92. readonly pathUtils: PathUtils<P>;
  93. protected constructor(pathUtils: PathUtils<P>);
  94. /**
  95. * @deprecated: Moved to jsInstallUtils
  96. */
  97. abstract getExtractHint(hints: ExtractHintOptions): boolean;
  98. abstract getRealPath(): P;
  99. abstract resolve(p: P): P;
  100. abstract opendirPromise(p: P, opts?: OpendirOptions): Promise<Dir<P>>;
  101. abstract opendirSync(p: P, opts?: OpendirOptions): Dir<P>;
  102. abstract openPromise(p: P, flags: string, mode?: number): Promise<number>;
  103. abstract openSync(p: P, flags: string, mode?: number): number;
  104. abstract readPromise(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number | null): Promise<number>;
  105. abstract readSync(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number | null): number;
  106. abstract writePromise(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number): Promise<number>;
  107. abstract writePromise(fd: number, buffer: string, position?: number): Promise<number>;
  108. abstract writeSync(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number): number;
  109. abstract writeSync(fd: number, buffer: string, position?: number): number;
  110. abstract closePromise(fd: number): Promise<void>;
  111. abstract closeSync(fd: number): void;
  112. abstract createWriteStream(p: P | null, opts?: CreateWriteStreamOptions): WriteStream;
  113. abstract createReadStream(p: P | null, opts?: CreateReadStreamOptions): ReadStream;
  114. abstract realpathPromise(p: P): Promise<P>;
  115. abstract realpathSync(p: P): P;
  116. abstract readdirPromise(p: P): Promise<Array<Filename>>;
  117. abstract readdirPromise(p: P, opts: {
  118. withFileTypes: false;
  119. } | null): Promise<Array<Filename>>;
  120. abstract readdirPromise(p: P, opts: {
  121. withFileTypes: true;
  122. }): Promise<Array<Dirent>>;
  123. abstract readdirPromise(p: P, opts: {
  124. withFileTypes: boolean;
  125. }): Promise<Array<Filename> | Array<Dirent>>;
  126. abstract readdirSync(p: P): Array<Filename>;
  127. abstract readdirSync(p: P, opts: {
  128. withFileTypes: false;
  129. } | null): Array<Filename>;
  130. abstract readdirSync(p: P, opts: {
  131. withFileTypes: true;
  132. }): Array<Dirent>;
  133. abstract readdirSync(p: P, opts: {
  134. withFileTypes: boolean;
  135. }): Array<Filename> | Array<Dirent>;
  136. abstract existsPromise(p: P): Promise<boolean>;
  137. abstract existsSync(p: P): boolean;
  138. abstract accessPromise(p: P, mode?: number): Promise<void>;
  139. abstract accessSync(p: P, mode?: number): void;
  140. abstract statPromise(p: P): Promise<Stats>;
  141. abstract statPromise(p: P, opts: (StatOptions & {
  142. bigint?: false | undefined;
  143. }) | undefined): Promise<Stats>;
  144. abstract statPromise(p: P, opts: StatOptions & {
  145. bigint: true;
  146. }): Promise<BigIntStats>;
  147. abstract statPromise(p: P, opts?: StatOptions): Promise<Stats | BigIntStats>;
  148. abstract statSync(p: P): Stats;
  149. abstract statSync(p: P, opts?: StatSyncOptions & {
  150. bigint?: false | undefined;
  151. throwIfNoEntry: false;
  152. }): Stats | undefined;
  153. abstract statSync(p: P, opts: StatSyncOptions & {
  154. bigint: true;
  155. throwIfNoEntry: false;
  156. }): BigIntStats | undefined;
  157. abstract statSync(p: P, opts?: StatSyncOptions & {
  158. bigint?: false | undefined;
  159. }): Stats;
  160. abstract statSync(p: P, opts: StatSyncOptions & {
  161. bigint: true;
  162. }): BigIntStats;
  163. abstract statSync(p: P, opts: StatSyncOptions & {
  164. bigint: boolean;
  165. throwIfNoEntry?: false | undefined;
  166. }): Stats | BigIntStats;
  167. abstract statSync(p: P, opts?: StatSyncOptions): Stats | BigIntStats | undefined;
  168. abstract fstatPromise(fd: number): Promise<Stats>;
  169. abstract fstatPromise(fd: number, opts: {
  170. bigint: true;
  171. }): Promise<BigIntStats>;
  172. abstract fstatPromise(fd: number, opts?: {
  173. bigint: boolean;
  174. }): Promise<BigIntStats | Stats>;
  175. abstract fstatSync(fd: number): Stats;
  176. abstract fstatSync(fd: number, opts: {
  177. bigint: true;
  178. }): BigIntStats;
  179. abstract fstatSync(fd: number, opts?: {
  180. bigint: boolean;
  181. }): BigIntStats | Stats;
  182. abstract lstatPromise(p: P): Promise<Stats>;
  183. abstract lstatPromise(p: P, opts: (StatOptions & {
  184. bigint?: false | undefined;
  185. }) | undefined): Promise<Stats>;
  186. abstract lstatPromise(p: P, opts: StatOptions & {
  187. bigint: true;
  188. }): Promise<BigIntStats>;
  189. abstract lstatPromise(p: P, opts?: StatOptions): Promise<Stats | BigIntStats>;
  190. abstract lstatSync(p: P): Stats;
  191. abstract lstatSync(p: P, opts?: StatSyncOptions & {
  192. bigint?: false | undefined;
  193. throwIfNoEntry: false;
  194. }): Stats | undefined;
  195. abstract lstatSync(p: P, opts: StatSyncOptions & {
  196. bigint: true;
  197. throwIfNoEntry: false;
  198. }): BigIntStats | undefined;
  199. abstract lstatSync(p: P, opts?: StatSyncOptions & {
  200. bigint?: false | undefined;
  201. }): Stats;
  202. abstract lstatSync(p: P, opts: StatSyncOptions & {
  203. bigint: true;
  204. }): BigIntStats;
  205. abstract lstatSync(p: P, opts: StatSyncOptions & {
  206. bigint: boolean;
  207. throwIfNoEntry?: false | undefined;
  208. }): Stats | BigIntStats;
  209. abstract lstatSync(p: P, opts?: StatSyncOptions): Stats | BigIntStats | undefined;
  210. abstract chmodPromise(p: P, mask: number): Promise<void>;
  211. abstract chmodSync(p: P, mask: number): void;
  212. abstract fchmodPromise(fd: number, mask: number): Promise<void>;
  213. abstract fchmodSync(fd: number, mask: number): void;
  214. abstract fchownPromise(fd: number, uid: number, gid: number): Promise<void>;
  215. abstract fchownSync(fd: number, uid: number, gid: number): void;
  216. abstract chownPromise(p: P, uid: number, gid: number): Promise<void>;
  217. abstract chownSync(p: P, uid: number, gid: number): void;
  218. abstract mkdirPromise(p: P, opts?: MkdirOptions): Promise<string | undefined>;
  219. abstract mkdirSync(p: P, opts?: MkdirOptions): string | undefined;
  220. abstract rmdirPromise(p: P, opts?: RmdirOptions): Promise<void>;
  221. abstract rmdirSync(p: P, opts?: RmdirOptions): void;
  222. abstract linkPromise(existingP: P, newP: P): Promise<void>;
  223. abstract linkSync(existingP: P, newP: P): void;
  224. abstract symlinkPromise(target: P, p: P, type?: SymlinkType): Promise<void>;
  225. abstract symlinkSync(target: P, p: P, type?: SymlinkType): void;
  226. abstract renamePromise(oldP: P, newP: P): Promise<void>;
  227. abstract renameSync(oldP: P, newP: P): void;
  228. abstract copyFilePromise(sourceP: P, destP: P, flags?: number): Promise<void>;
  229. abstract copyFileSync(sourceP: P, destP: P, flags?: number): void;
  230. abstract appendFilePromise(p: FSPath<P>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): Promise<void>;
  231. abstract appendFileSync(p: FSPath<P>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): void;
  232. abstract writeFilePromise(p: FSPath<P>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): Promise<void>;
  233. abstract writeFileSync(p: FSPath<P>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): void;
  234. abstract unlinkPromise(p: P): Promise<void>;
  235. abstract unlinkSync(p: P): void;
  236. abstract utimesPromise(p: P, atime: Date | string | number, mtime: Date | string | number): Promise<void>;
  237. abstract utimesSync(p: P, atime: Date | string | number, mtime: Date | string | number): void;
  238. lutimesPromise?(p: P, atime: Date | string | number, mtime: Date | string | number): Promise<void>;
  239. lutimesSync?(p: P, atime: Date | string | number, mtime: Date | string | number): void;
  240. abstract readFilePromise(p: FSPath<P>, encoding: 'utf8'): Promise<string>;
  241. abstract readFilePromise(p: FSPath<P>, encoding?: string): Promise<Buffer>;
  242. abstract readFileSync(p: FSPath<P>, encoding: 'utf8'): string;
  243. abstract readFileSync(p: FSPath<P>, encoding?: string): Buffer;
  244. abstract readlinkPromise(p: P): Promise<P>;
  245. abstract readlinkSync(p: P): P;
  246. abstract ftruncatePromise(fd: number, len?: number): Promise<void>;
  247. abstract ftruncateSync(fd: number, len?: number): void;
  248. abstract truncatePromise(p: P, len?: number): Promise<void>;
  249. abstract truncateSync(p: P, len?: number): void;
  250. abstract watch(p: P, cb?: WatchCallback): Watcher;
  251. abstract watch(p: P, opts: WatchOptions, cb?: WatchCallback): Watcher;
  252. abstract watchFile(p: P, cb: WatchFileCallback): StatWatcher;
  253. abstract watchFile(p: P, opts: WatchFileOptions, cb: WatchFileCallback): StatWatcher;
  254. abstract unwatchFile(p: P, cb?: WatchFileCallback): void;
  255. genTraversePromise(init: P, { stableSort }?: {
  256. stableSort?: boolean;
  257. }): AsyncGenerator<P, void, unknown>;
  258. removePromise(p: P, { recursive, maxRetries }?: {
  259. recursive?: boolean;
  260. maxRetries?: number;
  261. }): Promise<void>;
  262. removeSync(p: P, { recursive }?: {
  263. recursive?: boolean;
  264. }): void;
  265. mkdirpPromise(p: P, { chmod, utimes }?: {
  266. chmod?: number;
  267. utimes?: [Date | string | number, Date | string | number];
  268. }): Promise<string | undefined>;
  269. mkdirpSync(p: P, { chmod, utimes }?: {
  270. chmod?: number;
  271. utimes?: [Date | string | number, Date | string | number];
  272. }): string | undefined;
  273. copyPromise(destination: P, source: P, options?: {
  274. baseFs?: undefined;
  275. overwrite?: boolean;
  276. stableSort?: boolean;
  277. stableTime?: boolean;
  278. linkStrategy?: LinkStrategy;
  279. }): Promise<void>;
  280. copyPromise<P2 extends Path>(destination: P, source: P2, options: {
  281. baseFs: FakeFS<P2>;
  282. overwrite?: boolean;
  283. stableSort?: boolean;
  284. stableTime?: boolean;
  285. linkStrategy?: LinkStrategy;
  286. }): Promise<void>;
  287. /** @deprecated Prefer using `copyPromise` instead */
  288. copySync(destination: P, source: P, options?: {
  289. baseFs?: undefined;
  290. overwrite?: boolean;
  291. }): void;
  292. copySync<P2 extends Path>(destination: P, source: P2, options: {
  293. baseFs: FakeFS<P2>;
  294. overwrite?: boolean;
  295. }): void;
  296. changeFilePromise(p: P, content: Buffer): Promise<void>;
  297. changeFilePromise(p: P, content: string, opts?: ChangeFileOptions): Promise<void>;
  298. private changeFileBufferPromise;
  299. private changeFileTextPromise;
  300. changeFileSync(p: P, content: Buffer): void;
  301. changeFileSync(p: P, content: string, opts?: ChangeFileOptions): void;
  302. private changeFileBufferSync;
  303. private changeFileTextSync;
  304. movePromise(fromP: P, toP: P): Promise<void>;
  305. moveSync(fromP: P, toP: P): void;
  306. lockPromise<T>(affectedPath: P, callback: () => Promise<T>): Promise<T>;
  307. readJsonPromise(p: P): Promise<any>;
  308. readJsonSync(p: P): any;
  309. writeJsonPromise(p: P, data: any): Promise<void>;
  310. writeJsonSync(p: P, data: any): void;
  311. preserveTimePromise(p: P, cb: () => Promise<P | void>): Promise<void>;
  312. preserveTimeSync(p: P, cb: () => P | void): Promise<void>;
  313. }
  314. export declare abstract class BasePortableFakeFS extends FakeFS<PortablePath> {
  315. protected constructor();
  316. }
  317. export declare function normalizeLineEndings(originalContent: string, newContent: string): string;