-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
NodeFS.ts
590 lines (507 loc) Β· 25.5 KB
/
NodeFS.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import fs, {BigIntStats, Stats} from 'fs';
import {CreateReadStreamOptions, CreateWriteStreamOptions, Dir, StatWatcher, WatchFileCallback, WatchFileOptions, OpendirOptions, ReaddirOptions} from './FakeFS';
import {Dirent, SymlinkType, StatSyncOptions, StatOptions} from './FakeFS';
import {BasePortableFakeFS, WriteFileOptions} from './FakeFS';
import {MkdirOptions, RmdirOptions, WatchOptions, WatchCallback, Watcher} from './FakeFS';
import {FSPath, PortablePath, Filename, ppath, npath, NativePath} from './path';
function direntToPortable(dirent: Dirent<NativePath>): Dirent<PortablePath> {
// We don't need to return a copy, we can just reuse the object the real fs returned
const portableDirent = dirent as Dirent<PortablePath>;
if (typeof dirent.path === `string`)
portableDirent.path = npath.toPortablePath(dirent.path);
return portableDirent;
}
export class NodeFS extends BasePortableFakeFS {
private readonly realFs: typeof fs;
constructor(realFs: typeof fs = fs) {
super();
this.realFs = realFs;
}
getExtractHint() {
return false;
}
getRealPath() {
return PortablePath.root;
}
resolve(p: PortablePath) {
return ppath.resolve(p);
}
async openPromise(p: PortablePath, flags: string, mode?: number) {
return await new Promise<number>((resolve, reject) => {
this.realFs.open(npath.fromPortablePath(p), flags, mode, this.makeCallback(resolve, reject));
});
}
openSync(p: PortablePath, flags: string, mode?: number) {
return this.realFs.openSync(npath.fromPortablePath(p), flags, mode);
}
async opendirPromise(p: PortablePath, opts?: OpendirOptions): Promise<Dir<PortablePath>> {
return await new Promise<fs.Stats>((resolve, reject) => {
if (typeof opts !== `undefined`) {
this.realFs.opendir(npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject) as any);
} else {
this.realFs.opendir(npath.fromPortablePath(p), this.makeCallback(resolve, reject) as any);
}
}).then(dir => {
// @ts-expect-error
//
// We need a way to tell TS that the values returned by the `read`
// methods are compatible with `Dir`, especially the `name` field.
//
// We also can't use `Object.assign` to set the because the `path`
// field to a Filename, because the property isn't writable, so
// we need to use defineProperty instead.
//
const dirWithFixedPath: Dir<PortablePath> = dir;
Object.defineProperty(dirWithFixedPath, `path`, {
value: p,
configurable: true,
writable: true,
});
return dirWithFixedPath;
});
}
opendirSync(p: PortablePath, opts?: OpendirOptions) {
const dir: Omit<fs.Dir, `path`> = typeof opts !== `undefined`
? this.realFs.opendirSync(npath.fromPortablePath(p), opts)
: this.realFs.opendirSync(npath.fromPortablePath(p));
// @ts-expect-error
//
// We need a way to tell TS that the values returned by the `read`
// methods are compatible with `Dir`, especially the `name` field.
//
// We also can't use `Object.assign` to set the because the `path`
// field to a Filename, because the property isn't writable, so
// we need to use defineProperty instead.
//
const dirWithFixedPath: Dir<PortablePath> = dir;
Object.defineProperty(dirWithFixedPath, `path`, {
value: p,
configurable: true,
writable: true,
});
return dirWithFixedPath;
}
async readPromise(fd: number, buffer: Buffer, offset: number = 0, length: number = 0, position: number | null = -1) {
return await new Promise<number>((resolve, reject) => {
this.realFs.read(fd, buffer, offset, length, position, (error, bytesRead) => {
if (error) {
reject(error);
} else {
resolve(bytesRead);
}
});
});
}
readSync(fd: number, buffer: Buffer, offset: number, length: number, position: number) {
return this.realFs.readSync(fd, buffer, offset, length, position);
}
writePromise(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number): Promise<number>;
writePromise(fd: number, buffer: string, position?: number): Promise<number>;
async writePromise(fd: number, buffer: Buffer | string, offset?: number, length?: number, position?: number): Promise<number> {
return await new Promise<number>((resolve, reject) => {
if (typeof buffer === `string`) {
return this.realFs.write(fd, buffer, offset, this.makeCallback(resolve, reject));
} else {
return this.realFs.write(fd, buffer, offset, length, position, this.makeCallback(resolve, reject));
}
});
}
writeSync(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number): number;
writeSync(fd: number, buffer: string, position?: number): number;
writeSync(fd: number, buffer: Buffer | string, offset?: number, length?: number, position?: number) {
if (typeof buffer === `string`) {
return this.realFs.writeSync(fd, buffer, offset);
} else {
return this.realFs.writeSync(fd, buffer, offset, length, position);
}
}
async closePromise(fd: number) {
await new Promise<void>((resolve, reject) => {
this.realFs.close(fd, this.makeCallback(resolve, reject));
});
}
closeSync(fd: number) {
this.realFs.closeSync(fd);
}
createReadStream(p: PortablePath | null, opts?: CreateReadStreamOptions) {
const realPath = (p !== null ? npath.fromPortablePath(p) : p) as fs.PathLike;
return this.realFs.createReadStream(realPath, opts);
}
createWriteStream(p: PortablePath | null, opts?: CreateWriteStreamOptions) {
const realPath = (p !== null ? npath.fromPortablePath(p) : p) as fs.PathLike;
return this.realFs.createWriteStream(realPath, opts);
}
async realpathPromise(p: PortablePath) {
return await new Promise<string>((resolve, reject) => {
this.realFs.realpath(npath.fromPortablePath(p), {}, this.makeCallback(resolve, reject));
}).then(path => {
return npath.toPortablePath(path);
});
}
realpathSync(p: PortablePath) {
return npath.toPortablePath(this.realFs.realpathSync(npath.fromPortablePath(p), {}));
}
async existsPromise(p: PortablePath) {
return await new Promise<boolean>(resolve => {
this.realFs.exists(npath.fromPortablePath(p), resolve);
});
}
accessSync(p: PortablePath, mode?: number) {
return this.realFs.accessSync(npath.fromPortablePath(p), mode);
}
async accessPromise(p: PortablePath, mode?: number) {
return await new Promise<void>((resolve, reject) => {
this.realFs.access(npath.fromPortablePath(p), mode, this.makeCallback(resolve, reject));
});
}
existsSync(p: PortablePath) {
return this.realFs.existsSync(npath.fromPortablePath(p));
}
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/51d793492d4c2e372b01257668dcd3afc58d7352/types/node/v16/fs.d.ts#L1042-L1059
async statPromise(p: PortablePath): Promise<Stats>;
async statPromise(p: PortablePath, opts: (StatOptions & { bigint?: false | undefined }) | undefined): Promise<Stats>;
async statPromise(p: PortablePath, opts: StatOptions & { bigint: true }): Promise<BigIntStats>;
async statPromise(p: PortablePath, opts?: StatOptions): Promise<Stats | BigIntStats> {
return await new Promise<BigIntStats | Stats>((resolve, reject) => {
if (opts) {
this.realFs.stat(npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
} else {
this.realFs.stat(npath.fromPortablePath(p), this.makeCallback(resolve, reject));
}
});
}
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/51d793492d4c2e372b01257668dcd3afc58d7352/types/node/v16/fs.d.ts#L931-L967
statSync(p: PortablePath): Stats;
statSync(p: PortablePath, opts?: StatSyncOptions & {bigint?: false | undefined, throwIfNoEntry: false}): Stats | undefined;
statSync(p: PortablePath, opts: StatSyncOptions & {bigint: true, throwIfNoEntry: false}): BigIntStats | undefined;
statSync(p: PortablePath, opts?: StatSyncOptions & {bigint?: false | undefined}): Stats;
statSync(p: PortablePath, opts: StatSyncOptions & {bigint: true}): BigIntStats;
statSync(p: PortablePath, opts: StatSyncOptions & {bigint: boolean, throwIfNoEntry?: false | undefined}): Stats | BigIntStats;
statSync(p: PortablePath, opts?: StatSyncOptions): Stats | BigIntStats | undefined {
if (opts) {
return this.realFs.statSync(npath.fromPortablePath(p), opts);
} else {
return this.realFs.statSync(npath.fromPortablePath(p));
}
}
async fstatPromise(fd: number): Promise<Stats>;
async fstatPromise(fd: number, opts: {bigint: true}): Promise<BigIntStats>;
async fstatPromise(fd: number, opts?: {bigint: boolean}): Promise<BigIntStats | Stats>;
async fstatPromise(fd: number, opts?: {bigint: boolean}) {
return await new Promise<BigIntStats | Stats>((resolve, reject) => {
if (opts) {
this.realFs.fstat(fd, opts, this.makeCallback(resolve, reject));
} else {
this.realFs.fstat(fd, this.makeCallback(resolve, reject));
}
});
}
fstatSync(fd: number): Stats;
fstatSync(fd: number, opts: {bigint: true}): BigIntStats;
fstatSync(fd: number, opts?: {bigint: boolean}): BigIntStats | Stats;
fstatSync(fd: number, opts?: {bigint: boolean}) {
if (opts) {
return this.realFs.fstatSync(fd, opts);
} else {
return this.realFs.fstatSync(fd);
}
}
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/51d793492d4c2e372b01257668dcd3afc58d7352/types/node/v16/fs.d.ts#L1042-L1059
async lstatPromise(p: PortablePath): Promise<Stats>;
async lstatPromise(p: PortablePath, opts: (StatOptions & { bigint?: false | undefined }) | undefined): Promise<Stats>;
async lstatPromise(p: PortablePath, opts: StatOptions & { bigint: true }): Promise<BigIntStats>;
async lstatPromise(p: PortablePath, opts?: StatOptions): Promise<Stats | BigIntStats> {
return await new Promise<BigIntStats | Stats>((resolve, reject) => {
if (opts) {
this.realFs.lstat(npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
} else {
this.realFs.lstat(npath.fromPortablePath(p), this.makeCallback(resolve, reject));
}
});
}
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/51d793492d4c2e372b01257668dcd3afc58d7352/types/node/v16/fs.d.ts#L931-L967
lstatSync(p: PortablePath): Stats;
lstatSync(p: PortablePath, opts?: StatSyncOptions & {bigint?: false | undefined, throwIfNoEntry: false}): Stats | undefined;
lstatSync(p: PortablePath, opts: StatSyncOptions & {bigint: true, throwIfNoEntry: false}): BigIntStats | undefined;
lstatSync(p: PortablePath, opts?: StatSyncOptions & {bigint?: false | undefined}): Stats;
lstatSync(p: PortablePath, opts: StatSyncOptions & {bigint: true}): BigIntStats;
lstatSync(p: PortablePath, opts: StatSyncOptions & { bigint: boolean, throwIfNoEntry?: false | undefined }): Stats | BigIntStats;
lstatSync(p: PortablePath, opts?: StatSyncOptions): Stats | BigIntStats | undefined {
if (opts) {
return this.realFs.lstatSync(npath.fromPortablePath(p), opts);
} else {
return this.realFs.lstatSync(npath.fromPortablePath(p));
}
}
async fchmodPromise(fd: number, mask: number): Promise<void> {
return await new Promise<void>((resolve, reject) => {
this.realFs.fchmod(fd, mask, this.makeCallback(resolve, reject));
});
}
fchmodSync(fd: number, mask: number): void {
return this.realFs.fchmodSync(fd, mask);
}
async chmodPromise(p: PortablePath, mask: number) {
return await new Promise<void>((resolve, reject) => {
this.realFs.chmod(npath.fromPortablePath(p), mask, this.makeCallback(resolve, reject));
});
}
chmodSync(p: PortablePath, mask: number) {
return this.realFs.chmodSync(npath.fromPortablePath(p), mask);
}
async fchownPromise(fd: number, uid: number, gid: number): Promise<void> {
return await new Promise<void>((resolve, reject) => {
this.realFs.fchown(fd, uid, gid, this.makeCallback(resolve, reject));
});
}
fchownSync(fd: number, uid: number, gid: number): void {
return this.realFs.fchownSync(fd, uid, gid);
}
async chownPromise(p: PortablePath, uid: number, gid: number) {
return await new Promise<void>((resolve, reject) => {
this.realFs.chown(npath.fromPortablePath(p), uid, gid, this.makeCallback(resolve, reject));
});
}
chownSync(p: PortablePath, uid: number, gid: number) {
return this.realFs.chownSync(npath.fromPortablePath(p), uid, gid);
}
async renamePromise(oldP: PortablePath, newP: PortablePath) {
return await new Promise<void>((resolve, reject) => {
this.realFs.rename(npath.fromPortablePath(oldP), npath.fromPortablePath(newP), this.makeCallback(resolve, reject));
});
}
renameSync(oldP: PortablePath, newP: PortablePath) {
return this.realFs.renameSync(npath.fromPortablePath(oldP), npath.fromPortablePath(newP));
}
async copyFilePromise(sourceP: PortablePath, destP: PortablePath, flags: number = 0) {
return await new Promise<void>((resolve, reject) => {
this.realFs.copyFile(npath.fromPortablePath(sourceP), npath.fromPortablePath(destP), flags, this.makeCallback(resolve, reject));
});
}
copyFileSync(sourceP: PortablePath, destP: PortablePath, flags: number = 0) {
return this.realFs.copyFileSync(npath.fromPortablePath(sourceP), npath.fromPortablePath(destP), flags);
}
async appendFilePromise(p: FSPath<PortablePath>, content: string | Uint8Array, opts?: WriteFileOptions) {
return await new Promise<void>((resolve, reject) => {
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p;
if (opts) {
this.realFs.appendFile(fsNativePath, content, opts, this.makeCallback(resolve, reject));
} else {
this.realFs.appendFile(fsNativePath, content, this.makeCallback(resolve, reject));
}
});
}
appendFileSync(p: PortablePath, content: string | Uint8Array, opts?: WriteFileOptions) {
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p;
if (opts) {
this.realFs.appendFileSync(fsNativePath, content, opts);
} else {
this.realFs.appendFileSync(fsNativePath, content);
}
}
async writeFilePromise(p: FSPath<PortablePath>, content: string | NodeJS.ArrayBufferView, opts?: WriteFileOptions) {
return await new Promise<void>((resolve, reject) => {
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p;
if (opts) {
this.realFs.writeFile(fsNativePath, content, opts, this.makeCallback(resolve, reject));
} else {
this.realFs.writeFile(fsNativePath, content, this.makeCallback(resolve, reject));
}
});
}
writeFileSync(p: PortablePath, content: string | NodeJS.ArrayBufferView, opts?: WriteFileOptions) {
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p;
if (opts) {
this.realFs.writeFileSync(fsNativePath, content, opts);
} else {
this.realFs.writeFileSync(fsNativePath, content);
}
}
async unlinkPromise(p: PortablePath) {
return await new Promise<void>((resolve, reject) => {
this.realFs.unlink(npath.fromPortablePath(p), this.makeCallback(resolve, reject));
});
}
unlinkSync(p: PortablePath) {
return this.realFs.unlinkSync(npath.fromPortablePath(p));
}
async utimesPromise(p: PortablePath, atime: Date | string | number, mtime: Date | string | number) {
return await new Promise<void>((resolve, reject) => {
this.realFs.utimes(npath.fromPortablePath(p), atime, mtime, this.makeCallback(resolve, reject));
});
}
utimesSync(p: PortablePath, atime: Date | string | number, mtime: Date | string | number) {
this.realFs.utimesSync(npath.fromPortablePath(p), atime, mtime);
}
async lutimesPromise(p: PortablePath, atime: Date | string | number, mtime: Date | string | number) {
return await new Promise<void>((resolve, reject) => {
this.realFs.lutimes(npath.fromPortablePath(p), atime, mtime, this.makeCallback(resolve, reject));
});
}
lutimesSync(p: PortablePath, atime: Date | string | number, mtime: Date | string | number) {
this.realFs.lutimesSync(npath.fromPortablePath(p), atime, mtime);
}
async mkdirPromise(p: PortablePath, opts?: MkdirOptions) {
return await new Promise<string | undefined>((resolve, reject) => {
this.realFs.mkdir(npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
});
}
mkdirSync(p: PortablePath, opts?: MkdirOptions): string | undefined {
return this.realFs.mkdirSync(npath.fromPortablePath(p), opts);
}
async rmdirPromise(p: PortablePath, opts?: RmdirOptions) {
return await new Promise<void>((resolve, reject) => {
// TODO: always pass opts when min node version is 12.10+
if (opts) {
this.realFs.rmdir(npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
} else {
this.realFs.rmdir(npath.fromPortablePath(p), this.makeCallback(resolve, reject));
}
});
}
rmdirSync(p: PortablePath, opts?: RmdirOptions) {
return this.realFs.rmdirSync(npath.fromPortablePath(p), opts);
}
async linkPromise(existingP: PortablePath, newP: PortablePath) {
return await new Promise<void>((resolve, reject) => {
this.realFs.link(npath.fromPortablePath(existingP), npath.fromPortablePath(newP), this.makeCallback(resolve, reject));
});
}
linkSync(existingP: PortablePath, newP: PortablePath) {
return this.realFs.linkSync(npath.fromPortablePath(existingP), npath.fromPortablePath(newP));
}
async symlinkPromise(target: PortablePath, p: PortablePath, type?: SymlinkType) {
return await new Promise<void>((resolve, reject) => {
this.realFs.symlink(npath.fromPortablePath(target.replace(/\/+$/, ``) as PortablePath), npath.fromPortablePath(p), type, this.makeCallback(resolve, reject));
});
}
symlinkSync(target: PortablePath, p: PortablePath, type?: SymlinkType) {
return this.realFs.symlinkSync(npath.fromPortablePath(target.replace(/\/+$/, ``) as PortablePath), npath.fromPortablePath(p), type);
}
readFilePromise(p: FSPath<PortablePath>, encoding?: null): Promise<Buffer>;
readFilePromise(p: FSPath<PortablePath>, encoding: BufferEncoding): Promise<string>;
readFilePromise(p: FSPath<PortablePath>, encoding?: BufferEncoding | null): Promise<Buffer | string>;
async readFilePromise(p: FSPath<PortablePath>, encoding?: BufferEncoding | null) {
return await new Promise<any>((resolve, reject) => {
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p;
this.realFs.readFile(fsNativePath, encoding, this.makeCallback(resolve, reject));
});
}
readFileSync(p: FSPath<PortablePath>, encoding?: null): Buffer;
readFileSync(p: FSPath<PortablePath>, encoding: BufferEncoding): string;
readFileSync(p: FSPath<PortablePath>, encoding?: BufferEncoding | null): Buffer | string;
readFileSync(p: FSPath<PortablePath>, encoding?: BufferEncoding | null) {
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p;
return this.realFs.readFileSync(fsNativePath, encoding);
}
async readdirPromise(p: PortablePath, opts?: null): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {recursive?: false, withFileTypes: true}): Promise<Array<Dirent<PortablePath>>>;
async readdirPromise(p: PortablePath, opts: {recursive?: false, withFileTypes?: false}): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {recursive?: false, withFileTypes: boolean}): Promise<Array<Dirent<PortablePath> | Filename>>;
async readdirPromise(p: PortablePath, opts: {recursive: true, withFileTypes: true}): Promise<Array<Dirent<PortablePath>>>;
async readdirPromise(p: PortablePath, opts: {recursive: true, withFileTypes?: false}): Promise<Array<PortablePath>>;
async readdirPromise(p: PortablePath, opts: {recursive: true, withFileTypes: boolean}): Promise<Array<Dirent<PortablePath> | PortablePath>>;
async readdirPromise(p: PortablePath, opts: {recursive: boolean, withFileTypes: true}): Promise<Array<Dirent<PortablePath>>>;
async readdirPromise(p: PortablePath, opts: {recursive: boolean, withFileTypes?: false}): Promise<Array<PortablePath>>;
async readdirPromise(p: PortablePath, opts: {recursive: boolean, withFileTypes: boolean}): Promise<Array<Dirent<PortablePath> | PortablePath>>;
async readdirPromise(p: PortablePath, opts?: ReaddirOptions | null): Promise<Array<Dirent<PortablePath> | PortablePath>> {
return await new Promise<any>((resolve, reject) => {
if (opts) {
if (opts.recursive && process.platform === `win32`) {
if (opts.withFileTypes) {
this.realFs.readdir(npath.fromPortablePath(p), opts as any, this.makeCallback<Array<Dirent<NativePath>>>(results => resolve(results.map(direntToPortable)), reject) as any);
} else {
this.realFs.readdir(npath.fromPortablePath(p), opts as any, this.makeCallback<Array<NativePath>>(results => resolve(results.map(npath.toPortablePath)), reject) as any);
}
} else {
this.realFs.readdir(npath.fromPortablePath(p), opts as any, this.makeCallback(resolve, reject) as any);
}
} else {
this.realFs.readdir(npath.fromPortablePath(p), this.makeCallback(resolve, reject));
}
});
}
readdirSync(p: PortablePath, opts?: null): Array<Filename>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes: true}): Array<Dirent<PortablePath>>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes?: false}): Array<Filename>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes: boolean}): Array<Dirent<PortablePath> | Filename>;
readdirSync(p: PortablePath, opts: {recursive: true, withFileTypes: true}): Array<Dirent<PortablePath>>;
readdirSync(p: PortablePath, opts: {recursive: true, withFileTypes?: false}): Array<PortablePath>;
readdirSync(p: PortablePath, opts: {recursive: true, withFileTypes: boolean}): Array<Dirent<PortablePath> | PortablePath>;
readdirSync(p: PortablePath, opts: {recursive: boolean, withFileTypes: true}): Array<Dirent<PortablePath>>;
readdirSync(p: PortablePath, opts: {recursive: boolean, withFileTypes?: false}): Array<PortablePath>;
readdirSync(p: PortablePath, opts: {recursive: boolean, withFileTypes: boolean}): Array<Dirent<PortablePath> | Dirent<PortablePath> | PortablePath>;
readdirSync(p: PortablePath, opts?: ReaddirOptions | null): Array<Dirent<PortablePath> | PortablePath> {
if (opts) {
if (opts.recursive && process.platform === `win32`) {
if (opts.withFileTypes) {
return (this.realFs.readdirSync(npath.fromPortablePath(p), opts as any) as any as Array<Dirent<NativePath>>).map(direntToPortable);
} else {
return (this.realFs.readdirSync(npath.fromPortablePath(p), opts as any) as any as Array<NativePath>).map(npath.toPortablePath);
}
} else {
return this.realFs.readdirSync(npath.fromPortablePath(p), opts as any) as Array<PortablePath>;
}
} else {
return this.realFs.readdirSync(npath.fromPortablePath(p)) as Array<PortablePath>;
}
}
async readlinkPromise(p: PortablePath) {
return await new Promise<string>((resolve, reject) => {
this.realFs.readlink(npath.fromPortablePath(p), this.makeCallback(resolve, reject));
}).then(path => {
return npath.toPortablePath(path);
});
}
readlinkSync(p: PortablePath) {
return npath.toPortablePath(this.realFs.readlinkSync(npath.fromPortablePath(p)));
}
async truncatePromise(p: PortablePath, len?: number) {
return await new Promise<void>((resolve, reject) => {
this.realFs.truncate(npath.fromPortablePath(p), len, this.makeCallback(resolve, reject));
});
}
truncateSync(p: PortablePath, len?: number) {
return this.realFs.truncateSync(npath.fromPortablePath(p), len);
}
async ftruncatePromise(fd: number, len?: number): Promise<void> {
return await new Promise<void>((resolve, reject) => {
this.realFs.ftruncate(fd, len, this.makeCallback(resolve, reject));
});
}
ftruncateSync(fd: number, len?: number): void {
return this.realFs.ftruncateSync(fd, len);
}
watch(p: PortablePath, cb?: WatchCallback): Watcher;
watch(p: PortablePath, opts: WatchOptions, cb?: WatchCallback): Watcher;
watch(p: PortablePath, a?: WatchOptions | WatchCallback, b?: WatchCallback) {
return this.realFs.watch(
npath.fromPortablePath(p),
// @ts-expect-error
a,
b,
);
}
watchFile(p: PortablePath, cb: WatchFileCallback): StatWatcher;
watchFile(p: PortablePath, opts: WatchFileOptions, cb: WatchFileCallback): StatWatcher;
watchFile(p: PortablePath, a: WatchFileOptions | WatchFileCallback, b?: WatchFileCallback) {
return this.realFs.watchFile(
npath.fromPortablePath(p),
// @ts-expect-error
a,
b,
) as unknown as StatWatcher;
}
unwatchFile(p: PortablePath, cb?: WatchFileCallback) {
return this.realFs.unwatchFile(npath.fromPortablePath(p), cb);
}
private makeCallback<T>(resolve: (value: T) => void, reject: (reject: NodeJS.ErrnoException) => void) {
return (err: NodeJS.ErrnoException | null, result: T) => {
if (err) {
reject(err);
} else {
resolve(result);
}
};
}
}