Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements the readdir recursive flag #5514

Merged
merged 5 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .yarn/versions/cc34eb37.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
releases:
"@yarnpkg/cli": minor
"@yarnpkg/core": minor
"@yarnpkg/fslib": minor
"@yarnpkg/libzip": minor
"@yarnpkg/plugin-pnpm": minor
"@yarnpkg/pnpify": minor

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/nm"
- "@yarnpkg/pnp"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
2 changes: 1 addition & 1 deletion packages/plugin-pnpm/sources/PnpmLinker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class PnpmInstaller implements Installer {
private readonly indexFolderPromise: Promise<PortablePath>;

constructor(private opts: LinkOptions) {
this.indexFolderPromise = setupCopyIndex(xfs, {
this.indexFolderPromise = setupCopyIndex<PortablePath>(xfs, {
arcanis marked this conversation as resolved.
Show resolved Hide resolved
indexPath: ppath.join(opts.project.configuration.get(`globalFolder`), `index`),
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/sources/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export class Cache {

// We use an AliasFS to speed up getRealPath calls (e.g. VirtualFetcher.ensureVirtualLink)
// (there's no need to create the lazy baseFs instance to gather the already-known cachePath)
const aliasFs = new AliasFS(cachePath, {baseFs: lazyFs, pathUtils: ppath});
const aliasFs = new AliasFS<PortablePath>(cachePath, {baseFs: lazyFs, pathUtils: ppath});

const releaseFs = () => {
zipFs?.discardAndClose();
Expand Down
41 changes: 31 additions & 10 deletions packages/yarnpkg-fslib/sources/FakeFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export type BigIntStats = NodeBigIntStats & {
crc?: number;
};

export type Dirent = Exclude<NodeDirent, 'name'> & {
export type Dirent<T = undefined> = Exclude<NodeDirent, 'name'> & {
name: Filename;
path: T;
};

export type Dir<P extends Path> = {
Expand All @@ -40,6 +41,12 @@ export type Dir<P extends Path> = {

export type OpendirOptions = Partial<{
bufferSize: number;
recursive: boolean;
}>;

export type ReaddirOptions = Partial<{
recursive: boolean;
withFileTypes: boolean;
}>;

export type CreateReadStreamOptions = Partial<{
Expand Down Expand Up @@ -161,15 +168,29 @@ export abstract class FakeFS<P extends Path> {
abstract realpathPromise(p: P): Promise<P>;
abstract realpathSync(p: P): P;

abstract readdirPromise(p: P): Promise<Array<Filename>>;
abstract readdirPromise(p: P, opts: {withFileTypes: false} | null): Promise<Array<Filename>>;
abstract readdirPromise(p: P, opts: {withFileTypes: true}): Promise<Array<Dirent>>;
abstract readdirPromise(p: P, opts: {withFileTypes: boolean}): Promise<Array<Filename> | Array<Dirent>>;

abstract readdirSync(p: P): Array<Filename>;
abstract readdirSync(p: P, opts: {withFileTypes: false} | null): Array<Filename>;
abstract readdirSync(p: P, opts: {withFileTypes: true}): Array<Dirent>;
abstract readdirSync(p: P, opts: {withFileTypes: boolean}): Array<Filename> | Array<Dirent>;
abstract readdirPromise(p: P, opts?: null): Promise<Array<Filename>>;
abstract readdirPromise(p: P, opts: {recursive?: false, withFileTypes: true}): Promise<Array<Dirent>>;
abstract readdirPromise(p: P, opts: {recursive?: false, withFileTypes?: false}): Promise<Array<Filename>>;
abstract readdirPromise(p: P, opts: {recursive?: false, withFileTypes: boolean}): Promise<Array<Dirent | Filename>>;
abstract readdirPromise(p: P, opts: {recursive: true, withFileTypes: true}): Promise<Array<Dirent<P>>>;
abstract readdirPromise(p: P, opts: {recursive: true, withFileTypes?: false}): Promise<Array<P>>;
abstract readdirPromise(p: P, opts: {recursive: true, withFileTypes: boolean}): Promise<Array<Dirent | P>>;
abstract readdirPromise(p: P, opts: {recursive: boolean, withFileTypes: true}): Promise<Array<Dirent<P>>>;
abstract readdirPromise(p: P, opts: {recursive: boolean, withFileTypes?: false}): Promise<Array<P>>;
abstract readdirPromise(p: P, opts: {recursive: boolean, withFileTypes: boolean}): Promise<Array<Dirent<P> | P>>;
abstract readdirPromise(p: P, opts?: ReaddirOptions | null): Promise<Array<Dirent<P> | Dirent | P | Filename>>;

abstract readdirSync(p: P, opts?: null): Array<Filename>;
abstract readdirSync(p: P, opts: {recursive?: false, withFileTypes: true}): Array<Dirent>;
abstract readdirSync(p: P, opts: {recursive?: false, withFileTypes?: false}): Array<Filename>;
abstract readdirSync(p: P, opts: {recursive?: false, withFileTypes: boolean}): Array<Dirent | Filename>;
abstract readdirSync(p: P, opts: {recursive: true, withFileTypes: true}): Array<Dirent<P>>;
abstract readdirSync(p: P, opts: {recursive: true, withFileTypes?: false}): Array<P>;
abstract readdirSync(p: P, opts: {recursive: true, withFileTypes: boolean}): Array<Dirent | P>;
abstract readdirSync(p: P, opts: {recursive: boolean, withFileTypes: true}): Array<Dirent<P>>;
abstract readdirSync(p: P, opts: {recursive: boolean, withFileTypes?: false}): Array<P>;
abstract readdirSync(p: P, opts: {recursive: boolean, withFileTypes: boolean}): Array<Dirent<P> | P>;
abstract readdirSync(p: P, opts?: ReaddirOptions | null): Array<Dirent<P> | Dirent | P | Filename>;

abstract existsPromise(p: P): Promise<boolean>;
abstract existsSync(p: P): boolean;
Expand Down
34 changes: 23 additions & 11 deletions packages/yarnpkg-fslib/sources/MountFS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {BigIntStats, constants, Stats} from 'fs';

import {WatchOptions, WatchCallback, Watcher, StatOptions, StatSyncOptions} from './FakeFS';
import {WatchOptions, WatchCallback, Watcher, StatOptions, StatSyncOptions, ReaddirOptions} from './FakeFS';
import {FakeFS, MkdirOptions, RmdirOptions, WriteFileOptions, OpendirOptions} from './FakeFS';
import {Dirent, SymlinkType} from './FakeFS';
import {CreateReadStreamOptions, CreateWriteStreamOptions, BasePortableFakeFS, ExtractHintOptions, WatchFileOptions, WatchFileCallback, StatWatcher} from './FakeFS';
Expand Down Expand Up @@ -806,11 +806,17 @@ export class MountFS<MountedFS extends MountableFS> extends BasePortableFakeFS {
});
}

async readdirPromise(p: PortablePath): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: false} | null): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: true}): Promise<Array<Dirent>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: boolean}): Promise<Array<Filename> | Array<Dirent>>;
async readdirPromise(p: PortablePath, opts?: {withFileTypes?: boolean} | null): Promise<Array<string> | Array<Dirent>> {
async readdirPromise(p: PortablePath, opts?: null): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {recursive?: false, withFileTypes: true}): Promise<Array<Dirent>>;
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>>;
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> | Dirent | PortablePath>> {
return await this.makeCallPromise(p, async () => {
return await this.baseFs.readdirPromise(p, opts as any);
}, async (mountFs, {subPath}) => {
Expand All @@ -820,11 +826,17 @@ export class MountFS<MountedFS extends MountableFS> extends BasePortableFakeFS {
});
}

readdirSync(p: PortablePath): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: false} | null): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: true}): Array<Dirent>;
readdirSync(p: PortablePath, opts: {withFileTypes: boolean}): Array<Filename> | Array<Dirent>;
readdirSync(p: PortablePath, opts?: {withFileTypes?: boolean} | null): Array<string> | Array<Dirent> {
readdirSync(p: PortablePath, opts?: null): Array<Filename>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes: true}): Array<Dirent>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes?: false}): Array<Filename>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes: boolean}): Array<Dirent | 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>;
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> | PortablePath>;
readdirSync(p: PortablePath, opts?: ReaddirOptions | null): Array<Dirent<PortablePath> | Dirent | PortablePath> {
return this.makeCallSync(p, () => {
return this.baseFs.readdirSync(p, opts as any);
}, (mountFs, {subPath}) => {
Expand Down
48 changes: 30 additions & 18 deletions packages/yarnpkg-fslib/sources/NodeFS.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fs, {BigIntStats, Stats} from 'fs';
import fs, {BigIntStats, Stats} from 'fs';

import {CreateReadStreamOptions, CreateWriteStreamOptions, Dir, StatWatcher, WatchFileCallback, WatchFileOptions, OpendirOptions} 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} from './path';
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} from './path';

export class NodeFS extends BasePortableFakeFS {
private readonly realFs: typeof fs;
Expand Down Expand Up @@ -422,11 +422,17 @@ export class NodeFS extends BasePortableFakeFS {
return this.realFs.readFileSync(fsNativePath, encoding);
}

async readdirPromise(p: PortablePath): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: false} | null): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: true}): Promise<Array<Dirent>>;
async readdirPromise(p: PortablePath, opts: {withFileTypes: boolean}): Promise<Array<Filename> | Array<Dirent>>;
async readdirPromise(p: PortablePath, opts?: {withFileTypes?: boolean} | null): Promise<Array<string> | Array<Dirent>> {
async readdirPromise(p: PortablePath, opts?: null): Promise<Array<Filename>>;
async readdirPromise(p: PortablePath, opts: {recursive?: false, withFileTypes: true}): Promise<Array<Dirent>>;
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>>;
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> | Dirent | PortablePath>> {
return await new Promise<Array<Filename> | Array<Dirent>>((resolve, reject) => {
if (opts?.withFileTypes) {
this.realFs.readdir(npath.fromPortablePath(p), {withFileTypes: true}, this.makeCallback(resolve, reject) as any);
Expand All @@ -436,15 +442,21 @@ export class NodeFS extends BasePortableFakeFS {
});
}

readdirSync(p: PortablePath): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: false} | null): Array<Filename>;
readdirSync(p: PortablePath, opts: {withFileTypes: true}): Array<Dirent>;
readdirSync(p: PortablePath, opts: {withFileTypes: boolean}): Array<Filename> | Array<Dirent>;
readdirSync(p: PortablePath, opts?: {withFileTypes?: boolean} | null): Array<string> | Array<Dirent> {
readdirSync(p: PortablePath, opts?: null): Array<Filename>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes: true}): Array<Dirent>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes?: false}): Array<Filename>;
readdirSync(p: PortablePath, opts: {recursive?: false, withFileTypes: boolean}): Array<Dirent | 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>;
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> | PortablePath>;
readdirSync(p: PortablePath, opts?: ReaddirOptions | null): Array<Dirent<PortablePath> | Dirent | PortablePath> {
if (opts?.withFileTypes) {
return this.realFs.readdirSync(npath.fromPortablePath(p), {withFileTypes: true} as any);
return this.realFs.readdirSync(npath.fromPortablePath(p), {withFileTypes: true} as any) as Array<any>;
} else {
return this.realFs.readdirSync(npath.fromPortablePath(p)) as Array<Filename>;
return this.realFs.readdirSync(npath.fromPortablePath(p)) as Array<any>;
}
}

Expand Down
Loading