-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
NodeModulesFS.ts
654 lines (544 loc) Β· 26.6 KB
/
NodeModulesFS.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
import {BigIntStats, DirentNoPath, ReaddirOptions, Stats} from '@yarnpkg/fslib';
import {Dirent, Filename, MkdirOptions, ExtractHintOptions, WatchFileCallback, WatchFileOptions, StatWatcher, OpendirOptions, Dir} from '@yarnpkg/fslib';
import {RmdirOptions, RmOptions} from '@yarnpkg/fslib';
import {FSPath, NativePath, PortablePath, npath, ppath, opendir} from '@yarnpkg/fslib';
import {WatchOptions, WatchCallback, Watcher} from '@yarnpkg/fslib';
import {NodeFS, FakeFS, WriteFileOptions, ProxiedFS} from '@yarnpkg/fslib';
import {CreateReadStreamOptions, CreateWriteStreamOptions} from '@yarnpkg/fslib';
import {NodeModulesTreeOptions, NodeModulesTree} from '@yarnpkg/nm';
import {buildNodeModulesTree} from '@yarnpkg/nm';
import {PnpApi} from '@yarnpkg/pnp';
import fs from 'fs';
import {WatchManager} from './WatchManager';
import {dynamicRequireNoCache} from './dynamicRequire';
import {resolveNodeModulesPath, ResolvedPath} from './resolveNodeModulesPath';
export type NodeModulesFSOptions = {
realFs?: typeof fs;
pnpifyFs?: boolean;
};
export class NodeModulesFS extends ProxiedFS<NativePath, PortablePath> {
protected readonly baseFs: FakeFS<PortablePath>;
constructor(pnp: PnpApi, {realFs = fs, pnpifyFs = true}: NodeModulesFSOptions = {}) {
super(npath);
this.baseFs = new PortableNodeModulesFS(pnp, {baseFs: new NodeFS(realFs), pnpifyFs});
}
protected mapFromBase(path: PortablePath) {
return npath.fromPortablePath(path);
}
protected mapToBase(path: NativePath) {
return npath.toPortablePath(path);
}
}
export interface PortableNodeModulesFSOptions extends NodeModulesTreeOptions {
baseFs?: FakeFS<PortablePath>;
pnpifyFs?: boolean;
}
const WRITE_FLAGS_REGEX = /[+wa]/;
export class PortableNodeModulesFS extends FakeFS<PortablePath> {
private readonly baseFs: FakeFS<PortablePath>;
private readonly options: PortableNodeModulesFSOptions;
private readonly watchManager: WatchManager;
private readonly pnpFilePath: PortablePath;
private nodeModulesTree: NodeModulesTree;
constructor(pnp: PnpApi, {baseFs = new NodeFS(), pnpifyFs = true}: PortableNodeModulesFSOptions = {}) {
super(ppath);
if (!pnp.getDependencyTreeRoots)
throw new Error(`NodeModulesFS supports PnP API versions 3+, please upgrade your PnP API provider`);
this.options = {baseFs, pnpifyFs};
this.baseFs = baseFs;
const {tree, errors} = buildNodeModulesTree(pnp, this.options);
if (!tree)
throw new Error(`Assertion failed. Have got non-persistable node_modules graph, errors:\n${JSON.stringify(errors)}`);
this.nodeModulesTree = tree;
this.watchManager = new WatchManager();
const pnpRootPath = npath.toPortablePath(pnp.getPackageInformation(pnp.topLevel)!.packageLocation);
this.pnpFilePath = ppath.join(pnpRootPath, Filename.pnpCjs);
this.watchPnpFile(pnpRootPath);
}
private watchPnpFile(pnpRootPath: PortablePath) {
this.baseFs.watch(pnpRootPath, {persistent: false}, (_, filename) => {
if (filename === Filename.pnpCjs) {
const pnp = dynamicRequireNoCache(this.pnpFilePath);
const nodeModulesTree = buildNodeModulesTree(pnp, this.options);
if (!nodeModulesTree)
throw new Error(`Assertion failed. Have got non-persistable node_modules graph`);
this.watchManager.notifyWatchers((nodePath: PortablePath) => resolveNodeModulesPath(nodePath, this.nodeModulesTree));
}
});
}
private persistPath(dir: PortablePath) {
const pathStack = [];
let curPath = dir;
while (!this.baseFs.existsSync(curPath)) {
pathStack.push(curPath);
curPath = ppath.dirname(curPath);
}
for (const fullPath of pathStack.reverse()) {
this.baseFs.mkdirSync(fullPath);
}
}
private persistVirtualParentFolder(p: FSPath<PortablePath>) {
if (typeof p !== `number`) {
const parentPath = this.resolvePath(ppath.dirname(p));
if (parentPath.dirList) {
this.persistPath(parentPath.resolvedPath);
}
}
}
getExtractHint(hints: ExtractHintOptions) {
return this.baseFs.getExtractHint(hints);
}
resolve(path: PortablePath) {
return this.baseFs.resolve(this.resolvePath(path).resolvedPath!);
}
getBaseFs() {
return this.baseFs;
}
private resolvePath(p: PortablePath): ResolvedPath & { fullOriginalPath: PortablePath } {
if (typeof p === `number`) {
return {resolvedPath: p, fullOriginalPath: p};
} else {
const fullOriginalPath = this.pathUtils.resolve(p);
return {...resolveNodeModulesPath(fullOriginalPath, this.nodeModulesTree), fullOriginalPath};
}
}
private resolveFilePath(p: PortablePath): PortablePath;
private resolveFilePath(p: FSPath<PortablePath>): FSPath<PortablePath>;
private resolveFilePath(p: FSPath<PortablePath>): FSPath<PortablePath> {
if (typeof p === `number`)
return p;
const pnpPath = this.resolvePath(p);
return pnpPath.resolvedPath;
}
private resolveDirOrFilePath(p: number): number;
private resolveDirOrFilePath(p: PortablePath): PortablePath;
private resolveDirOrFilePath(p: FSPath<PortablePath>): FSPath<PortablePath>;
private resolveDirOrFilePath(p: FSPath<PortablePath>): FSPath<PortablePath> {
if (typeof p === `number`)
return p;
const pnpPath = this.resolvePath(p);
return pnpPath.forwardedDirPath || pnpPath.resolvedPath;
}
private resolveLink(opts: {
p: PortablePath;
op: string;
onSymlink: (stats: fs.Stats | fs.BigIntStats, targetPath: PortablePath) => any;
onRealPath: (targetPath: PortablePath) => any;
statOptions?: {bigint: boolean};
}) {
const {p, onSymlink, onRealPath, statOptions} = opts;
const pnpPath = this.resolvePath(p);
if (pnpPath.isSymlink) {
let stat;
try {
stat = this.baseFs.lstatSync(pnpPath.resolvedPath, statOptions);
} catch (e) {}
if (stat) {
return onSymlink(stat, this.pathUtils.relative(this.pathUtils.dirname(pnpPath.fullOriginalPath), pnpPath.resolvedPath));
}
}
return onRealPath(pnpPath.forwardedDirPath || pnpPath.resolvedPath);
}
private static makeSymlinkStats<T extends fs.Stats | fs.BigIntStats>(stats: T): T {
return Object.assign(stats, {
isFile: () => false,
isDirectory: () => false,
isSymbolicLink: () => true,
});
}
getRealPath() {
return this.baseFs.getRealPath();
}
async openPromise(p: PortablePath, flags: string, mode?: number) {
if (WRITE_FLAGS_REGEX.test(flags))
this.persistVirtualParentFolder(p);
return await this.baseFs.openPromise(this.resolveFilePath(p), flags, mode);
}
openSync(p: PortablePath, flags: string, mode?: number) {
if (WRITE_FLAGS_REGEX.test(flags))
this.persistVirtualParentFolder(p);
return this.baseFs.openSync(this.resolveFilePath(p), flags, mode);
}
async opendirPromise(p: PortablePath, opts?: OpendirOptions): Promise<Dir<PortablePath>> {
const pnpPath = this.resolvePath(p);
if (pnpPath.dirList || this.resolvePath(ppath.join(p, `node_modules`)).dirList) {
let fsDirList: Array<Filename> = [];
try {
fsDirList = await this.baseFs.readdirPromise(pnpPath.resolvedPath);
} catch (e) {
// Ignore errors
}
const entries = Array.from(pnpPath.dirList || [`node_modules` as Filename]).concat(fsDirList).sort();
return opendir(this, p, entries);
} else {
return await this.baseFs.opendirPromise(pnpPath.resolvedPath, opts);
}
}
opendirSync(p: PortablePath, opts?: OpendirOptions): Dir<PortablePath> {
const pnpPath = this.resolvePath(p);
if (pnpPath.dirList || this.resolvePath(ppath.join(p, `node_modules`)).dirList) {
let fsDirList: Array<Filename> = [];
try {
fsDirList = this.baseFs.readdirSync(pnpPath.resolvedPath);
} catch (e) {
// Ignore errors
}
const entries = Array.from(pnpPath.dirList || [`node_modules` as Filename]).concat(fsDirList).sort();
return opendir(this, p, entries);
} else {
return this.baseFs.opendirSync(pnpPath.resolvedPath, opts);
}
}
async readPromise(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number) {
return await this.baseFs.readPromise(fd, buffer, offset, length, position);
}
readSync(fd: number, buffer: Buffer, offset?: number, length?: number, position?: number) {
return this.baseFs.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> {
if (typeof buffer === `string`) {
return await this.baseFs.writePromise(fd, buffer, offset);
} else {
return await this.baseFs.writePromise(fd, buffer, offset, length, position);
}
}
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.baseFs.writeSync(fd, buffer, offset);
} else {
return this.baseFs.writeSync(fd, buffer, offset, length, position);
}
}
async closePromise(fd: number) {
await this.baseFs.closePromise(fd);
}
closeSync(fd: number) {
this.baseFs.closeSync(fd);
}
createReadStream(p: PortablePath | null, opts?: CreateReadStreamOptions) {
return this.baseFs.createReadStream(p !== null ? this.resolveFilePath(p) : p, opts);
}
createWriteStream(p: PortablePath | null, opts?: CreateWriteStreamOptions) {
return this.baseFs.createWriteStream(p !== null ? this.resolveDirOrFilePath(p) : p, opts);
}
async realpathPromise(p: PortablePath) {
const pnpPath = this.resolvePath(p);
return pnpPath.dirList ? pnpPath.resolvedPath : this.baseFs.realpathPromise(pnpPath.resolvedPath);
}
realpathSync(p: PortablePath) {
const pnpPath = this.resolvePath(p);
return pnpPath.dirList ? pnpPath.resolvedPath : this.baseFs.realpathSync(pnpPath.resolvedPath);
}
async existsPromise(p: PortablePath) {
const pnpPath = this.resolvePath(p);
if (pnpPath.dirList) {
return true;
} else {
return await this.baseFs.existsPromise(pnpPath.resolvedPath);
}
}
existsSync(p: PortablePath) {
const pnpPath = this.resolvePath(p);
if (pnpPath.dirList) {
return true;
} else {
return this.baseFs.existsSync(pnpPath.resolvedPath);
}
}
async accessPromise(p: PortablePath, mode?: number) {
return await this.baseFs.accessPromise(this.resolveDirOrFilePath(p), mode);
}
accessSync(p: PortablePath, mode?: number) {
return this.baseFs.accessSync(this.resolveDirOrFilePath(p), mode);
}
async statPromise(p: PortablePath): Promise<Stats>;
async statPromise(p: PortablePath, opts: {bigint: true}): Promise<BigIntStats>;
async statPromise(p: PortablePath, opts?: {bigint: boolean}): Promise<BigIntStats | Stats>;
async statPromise(p: PortablePath, opts?: {bigint: boolean}) {
return await this.baseFs.statPromise(this.resolveDirOrFilePath(p), opts);
}
statSync(p: PortablePath): Stats;
statSync(p: PortablePath, opts: {bigint: true}): BigIntStats;
statSync(p: PortablePath, opts?: {bigint: boolean}): BigIntStats | Stats;
statSync(p: PortablePath, opts?: {bigint: boolean}) {
return this.baseFs.statSync(this.resolveDirOrFilePath(p), opts);
}
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 this.baseFs.fstatPromise(fd, opts);
}
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}) {
return this.baseFs.fstatSync(fd, opts);
}
async lstatPromise(p: PortablePath): Promise<Stats>;
async lstatPromise(p: PortablePath, opts: {bigint: true}): Promise<BigIntStats>;
async lstatPromise(p: PortablePath, opts?: { bigint: boolean }): Promise<BigIntStats | Stats>;
async lstatPromise(p: PortablePath, opts?: { bigint: boolean }) {
return this.resolveLink({
p,
op: `lstat`,
onSymlink: stats => PortableNodeModulesFS.makeSymlinkStats(stats),
onRealPath: async resolvedPath => await this.baseFs.lstatPromise(resolvedPath, opts),
statOptions: opts,
});
}
lstatSync(p: PortablePath): Stats;
lstatSync(p: PortablePath, opts: {bigint: true}): BigIntStats;
lstatSync(p: PortablePath, opts?: { bigint: boolean }): BigIntStats | Stats;
lstatSync(p: PortablePath, opts?: { bigint: boolean }): BigIntStats | Stats {
return this.resolveLink({
p,
op: `lstat`,
onSymlink: stats => PortableNodeModulesFS.makeSymlinkStats(stats),
onRealPath: resolvedPath => this.baseFs.lstatSync(resolvedPath, opts),
statOptions: opts,
});
}
async fchmodPromise(fd: number, mask: number): Promise<void> {
return this.baseFs.fchmodPromise(this.resolveDirOrFilePath(fd), mask);
}
fchmodSync(fd: number, mask: number): void {
return this.baseFs.fchmodSync(this.resolveDirOrFilePath(fd), mask);
}
async chmodPromise(p: PortablePath, mask: number) {
return await this.baseFs.chmodPromise(this.resolveDirOrFilePath(p), mask);
}
chmodSync(p: PortablePath, mask: number) {
return this.baseFs.chmodSync(this.resolveDirOrFilePath(p), mask);
}
async fchownPromise(fd: number, uid: number, gid: number): Promise<void> {
return this.baseFs.fchownPromise(this.resolveDirOrFilePath(fd), uid, gid);
}
fchownSync(fd: number, uid: number, gid: number): void {
return this.baseFs.fchownSync(this.resolveDirOrFilePath(fd), uid, gid);
}
async chownPromise(p: PortablePath, uid: number, gid: number) {
return await this.baseFs.chownPromise(this.resolveDirOrFilePath(p), uid, gid);
}
chownSync(p: PortablePath, uid: number, gid: number) {
return this.baseFs.chownSync(this.resolveDirOrFilePath(p), uid, gid);
}
async renamePromise(oldP: PortablePath, newP: PortablePath) {
return await this.baseFs.renamePromise(this.resolveDirOrFilePath(oldP), this.resolveDirOrFilePath(newP));
}
renameSync(oldP: PortablePath, newP: PortablePath) {
return this.baseFs.renameSync(this.resolveDirOrFilePath(oldP), this.resolveDirOrFilePath(newP));
}
async copyFilePromise(sourceP: PortablePath, destP: PortablePath, flags?: number) {
return await this.baseFs.copyFilePromise(this.resolveFilePath(sourceP), this.resolveDirOrFilePath(destP), flags);
}
copyFileSync(sourceP: PortablePath, destP: PortablePath, flags?: number) {
return this.baseFs.copyFileSync(this.resolveFilePath(sourceP), this.resolveDirOrFilePath(destP), flags);
}
async appendFilePromise(p: FSPath<PortablePath>, content: string | Uint8Array, opts?: WriteFileOptions) {
return await this.baseFs.appendFilePromise(this.resolveDirOrFilePath(p), content, opts);
}
appendFileSync(p: FSPath<PortablePath>, content: string | Uint8Array, opts?: WriteFileOptions) {
return this.baseFs.appendFileSync(this.resolveDirOrFilePath(p), content, opts);
}
async writeFilePromise(p: FSPath<PortablePath>, content: string | NodeJS.ArrayBufferView, opts?: WriteFileOptions) {
return await this.baseFs.writeFilePromise(this.resolveDirOrFilePath(p), content, opts);
}
writeFileSync(p: FSPath<PortablePath>, content: string | NodeJS.ArrayBufferView, opts?: WriteFileOptions) {
return this.baseFs.writeFileSync(this.resolveDirOrFilePath(p), content, opts);
}
async unlinkPromise(p: PortablePath) {
return await this.baseFs.unlinkPromise(this.resolveDirOrFilePath(p));
}
unlinkSync(p: PortablePath) {
return this.baseFs.unlinkSync(this.resolveDirOrFilePath(p));
}
async utimesPromise(p: PortablePath, atime: Date | string | number, mtime: Date | string | number) {
return await this.baseFs.utimesPromise(this.resolveDirOrFilePath(p), atime, mtime);
}
utimesSync(p: PortablePath, atime: Date | string | number, mtime: Date | string | number) {
return this.baseFs.utimesSync(this.resolveDirOrFilePath(p), atime, mtime);
}
async lutimesPromise(p: PortablePath, atime: Date | string | number, mtime: Date | string | number) {
return await this.baseFs.lutimesPromise(this.resolveDirOrFilePath(p), atime, mtime);
}
lutimesSync(p: PortablePath, atime: Date | string | number, mtime: Date | string | number) {
return this.baseFs.lutimesSync(this.resolveDirOrFilePath(p), atime, mtime);
}
async mkdirPromise(p: PortablePath, opts: MkdirOptions) {
const pnpPath = this.resolvePath(p);
this.persistVirtualParentFolder(p);
return this.baseFs.mkdirPromise(pnpPath.resolvedPath, opts);
}
mkdirSync(p: PortablePath, opts: MkdirOptions) {
const pnpPath = this.resolvePath(p);
this.persistVirtualParentFolder(p);
return this.baseFs.mkdirSync(pnpPath.resolvedPath, opts);
}
async rmdirPromise(p: PortablePath, opts?: RmdirOptions) {
return await this.baseFs.rmdirPromise(this.resolveDirOrFilePath(p), opts);
}
rmdirSync(p: PortablePath, opts?: RmdirOptions) {
return this.baseFs.rmdirSync(this.resolveDirOrFilePath(p), opts);
}
async rmPromise(p: PortablePath, opts?: RmOptions) {
return await this.baseFs.rmPromise(this.resolveDirOrFilePath(p), opts);
}
rmSync(p: PortablePath, opts?: RmOptions) {
return this.baseFs.rmSync(this.resolveDirOrFilePath(p), opts);
}
async linkPromise(existingP: PortablePath, newP: PortablePath) {
return await this.baseFs.linkPromise(this.resolveDirOrFilePath(existingP), this.resolveDirOrFilePath(newP));
}
linkSync(existingP: PortablePath, newP: PortablePath) {
return this.baseFs.linkSync(this.resolveDirOrFilePath(existingP), this.resolveDirOrFilePath(newP));
}
async symlinkPromise(target: PortablePath, p: PortablePath) {
return await this.baseFs.symlinkPromise(this.resolveDirOrFilePath(target), this.resolveDirOrFilePath(p));
}
symlinkSync(target: PortablePath, p: PortablePath) {
return this.baseFs.symlinkSync(this.resolveDirOrFilePath(target), this.resolveDirOrFilePath(p));
}
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 this.baseFs.readFilePromise(this.resolveFilePath(p), encoding);
}
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) {
return this.baseFs.readFileSync(this.resolveFilePath(p), encoding);
}
async readdirPromise(p: PortablePath, opts?: null): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {recursive?: false, withFileTypes: true}): Promise<Array<DirentNoPath>>;
async readdirPromise(p: PortablePath, opts: {recursive?: false, withFileTypes?: false}): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {recursive?: false, withFileTypes: boolean}): Promise<Array<DirentNoPath | 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> | DirentNoPath>>;
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> | DirentNoPath | PortablePath>>;
async readdirPromise(p: PortablePath, opts?: ReaddirOptions | null): Promise<Array<Dirent<PortablePath> | DirentNoPath | PortablePath>> {
const pnpPath = this.resolvePath(p);
if (pnpPath.dirList || this.resolvePath(ppath.join(p, `node_modules`)).dirList) {
if (opts?.recursive)
throw new Error(`Unsupported option 'recursive' for NodeModulesFS.readdirPromise`);
let fsDirList: Array<Filename> = [];
try {
fsDirList = await this.baseFs.readdirPromise(pnpPath.resolvedPath);
} catch (e) {
// Ignore errors
}
const entries = Array.from(pnpPath.dirList || [`node_modules` as Filename]).concat(fsDirList).sort();
if (!opts?.withFileTypes)
return entries;
return entries.map(name => {
return Object.assign(this.lstatSync(ppath.join(p, name)), {
name,
path: undefined,
});
});
} else {
return await this.baseFs.readdirPromise(pnpPath.resolvedPath, opts as any);
}
}
readdirSync(p: PortablePath, opts?: null): Array<Filename>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes: true}): Array<DirentNoPath>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes?: false}): Array<Filename>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes: boolean}): Array<DirentNoPath | 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> | DirentNoPath>;
readdirSync(p: PortablePath, opts: {recursive: boolean, withFileTypes?: false}): Array<PortablePath>;
readdirSync(p: PortablePath, opts: {recursive: boolean, withFileTypes: boolean}): Array<Dirent<PortablePath> | DirentNoPath | PortablePath>;
readdirSync(p: PortablePath, opts?: ReaddirOptions | null): Array<Dirent<PortablePath> | DirentNoPath | PortablePath> {
const pnpPath = this.resolvePath(p);
if (pnpPath.dirList || this.resolvePath(ppath.join(p, `node_modules`)).dirList) {
if (opts?.recursive)
throw new Error(`Unsupported option 'recursive' for NodeModulesFS.readdirSync`);
let fsDirList: Array<Filename> = [];
try {
fsDirList = this.baseFs.readdirSync(pnpPath.resolvedPath);
} catch (e) {
// Ignore errors
}
const entries = Array.from(pnpPath.dirList || [`node_modules` as Filename]).concat(fsDirList).sort();
if (!opts?.withFileTypes)
return entries;
return entries.map(name => {
return Object.assign(this.lstatSync(ppath.join(p, name)), {
name,
path: undefined,
});
});
} else {
return this.baseFs.readdirSync(pnpPath.resolvedPath, opts as any);
}
}
async readlinkPromise(p: PortablePath) {
return this.resolveLink({
p,
op: `readlink`,
onSymlink: (_stats, targetPath) => targetPath,
onRealPath: async targetPath => await this.baseFs.readlinkPromise(this.resolveDirOrFilePath(targetPath)),
});
}
readlinkSync(p: PortablePath) {
return this.resolveLink({
p,
op: `readlink`,
onSymlink: (_stats, targetPath) => targetPath,
onRealPath: targetPath => this.baseFs.readlinkSync(this.resolveDirOrFilePath(targetPath)),
});
}
async truncatePromise(p: PortablePath, len?: number) {
return await this.baseFs.truncatePromise(this.resolveDirOrFilePath(p), len);
}
truncateSync(p: PortablePath, len?: number) {
return this.baseFs.truncateSync(this.resolveDirOrFilePath(p), len);
}
async ftruncatePromise(fd: number, len?: number): Promise<void> {
return await this.baseFs.ftruncatePromise(fd, len);
}
ftruncateSync(fd: number, len?: number): void {
return this.baseFs.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): Watcher {
const pnpPath = this.resolvePath(p);
const watchPath = pnpPath.resolvedPath;
if (watchPath && pnpPath.dirList) {
const callback: WatchCallback = typeof a === `function` ? a : typeof b === `function` ? b : () => {};
return this.watchManager.registerWatcher(watchPath, pnpPath.dirList, callback);
} else {
return this.baseFs.watch(
this.resolveDirOrFilePath(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): StatWatcher {
return this.baseFs.watchFile(
this.resolveDirOrFilePath(p),
// @ts-expect-error
a,
b,
);
}
unwatchFile(p: PortablePath, cb?: WatchFileCallback) {
return this.baseFs.unwatchFile(this.resolveDirOrFilePath(p), cb);
}
}