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

feat(fslib): add throwIfNoEntry support #4475

Merged
merged 1 commit into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 37 additions & 16 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions .yarn/versions/80053b5a.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": patch
"@yarnpkg/fslib": minor
"@yarnpkg/pnp": patch

declined:
- "@yarnpkg/esbuild-plugin-pnp"
- "@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-http"
- "@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-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/json-proxy"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The following changes only affect people writing Yarn plugins:

- The patched filesystem now supports `ftruncate`.
- The patched filesystem now supports `fchmod`.
- The patched filesystem now supports `throwIfNoEntry`.
- Updates the PnP compatibility layer for TypeScript 4.8 Beta

### Bugfixes
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/sources/worker-zip/index.js

Large diffs are not rendered by default.

40 changes: 32 additions & 8 deletions packages/yarnpkg-fslib/sources/FakeFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ export type ExtractHintOptions = {

export type SymlinkType = 'file' | 'dir' | 'junction';

export interface StatOptions {
bigint?: boolean | undefined;
}

export interface StatSyncOptions extends StatOptions {
throwIfNoEntry?: boolean | undefined;
}

export abstract class FakeFS<P extends Path> {
public readonly pathUtils: PathUtils<P>;

Expand Down Expand Up @@ -166,12 +174,20 @@ export abstract class FakeFS<P extends Path> {
abstract accessPromise(p: P, mode?: number): Promise<void>;
abstract accessSync(p: P, mode?: number): void;

// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/51d793492d4c2e372b01257668dcd3afc58d7352/types/node/v16/fs.d.ts#L1042-L1059
abstract statPromise(p: P): Promise<Stats>;
abstract statPromise(p: P, opts: {bigint: true}): Promise<BigIntStats>;
abstract statPromise(p: P, opts?: {bigint: boolean}): Promise<BigIntStats | Stats>;
abstract statPromise(p: P, opts: (StatOptions & { bigint?: false | undefined }) | undefined): Promise<Stats>;
abstract statPromise(p: P, opts: StatOptions & { bigint: true }): Promise<BigIntStats>;
abstract statPromise(p: P, opts?: StatOptions): Promise<Stats | BigIntStats>;

// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/51d793492d4c2e372b01257668dcd3afc58d7352/types/node/v16/fs.d.ts#L931-L967
abstract statSync(p: P): Stats;
abstract statSync(p: P, opts: {bigint: true}): BigIntStats;
abstract statSync(p: P, opts?: {bigint: boolean}): BigIntStats | Stats;
abstract statSync(p: P, opts?: StatSyncOptions & {bigint?: false | undefined, throwIfNoEntry: false}): Stats | undefined;
abstract statSync(p: P, opts: StatSyncOptions & {bigint: true, throwIfNoEntry: false}): BigIntStats | undefined;
abstract statSync(p: P, opts?: StatSyncOptions & {bigint?: false | undefined}): Stats;
abstract statSync(p: P, opts: StatSyncOptions & {bigint: true}): BigIntStats;
abstract statSync(p: P, opts: StatSyncOptions & {bigint: boolean, throwIfNoEntry?: false | undefined}): Stats | BigIntStats;
abstract statSync(p: P, opts?: StatSyncOptions): Stats | BigIntStats | undefined;

abstract fstatPromise(fd: number): Promise<Stats>;
abstract fstatPromise(fd: number, opts: {bigint: true}): Promise<BigIntStats>;
Expand All @@ -180,12 +196,20 @@ export abstract class FakeFS<P extends Path> {
abstract fstatSync(fd: number, opts: {bigint: true}): BigIntStats;
abstract fstatSync(fd: number, opts?: {bigint: boolean}): BigIntStats | Stats;

// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/51d793492d4c2e372b01257668dcd3afc58d7352/types/node/v16/fs.d.ts#L1042-L1059
abstract lstatPromise(p: P): Promise<Stats>;
abstract lstatPromise(p: P, opts: {bigint: true}): Promise<BigIntStats>;
abstract lstatPromise(p: P, opts?: {bigint: boolean}): Promise<BigIntStats | Stats>;
abstract lstatPromise(p: P, opts: (StatOptions & { bigint?: false | undefined }) | undefined): Promise<Stats>;
abstract lstatPromise(p: P, opts: StatOptions & { bigint: true }): Promise<BigIntStats>;
abstract lstatPromise(p: P, opts?: StatOptions): Promise<Stats | BigIntStats>;

// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/51d793492d4c2e372b01257668dcd3afc58d7352/types/node/v16/fs.d.ts#L931-L967
abstract lstatSync(p: P): Stats;
abstract lstatSync(p: P, opts: {bigint: true}): BigIntStats;
abstract lstatSync(p: P, opts?: {bigint: boolean}): BigIntStats | Stats;
abstract lstatSync(p: P, opts?: StatSyncOptions & {bigint?: false | undefined, throwIfNoEntry: false}): Stats | undefined;
abstract lstatSync(p: P, opts: StatSyncOptions & {bigint: true, throwIfNoEntry: false}): BigIntStats | undefined;
abstract lstatSync(p: P, opts?: StatSyncOptions & {bigint?: false | undefined}): Stats;
abstract lstatSync(p: P, opts: StatSyncOptions & {bigint: true}): BigIntStats;
abstract lstatSync(p: P, opts: StatSyncOptions & { bigint: boolean, throwIfNoEntry?: false | undefined }): Stats | BigIntStats;
abstract lstatSync(p: P, opts?: StatSyncOptions): Stats | BigIntStats | undefined;

abstract chmodPromise(p: P, mask: number): Promise<void>;
abstract chmodSync(p: P, mask: number): void;
Expand Down
44 changes: 28 additions & 16 deletions packages/yarnpkg-fslib/sources/NodeFS.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs, {BigIntStats, Stats} from 'fs';

import {CreateReadStreamOptions, CreateWriteStreamOptions, Dir, StatWatcher, WatchFileCallback, WatchFileOptions, OpendirOptions} from './FakeFS';
import {Dirent, SymlinkType} from './FakeFS';
import {Dirent, SymlinkType, StatSyncOptions, StatOptions} from './FakeFS';
import {BasePortableFakeFS, WriteFileOptions} from './FakeFS';
import {MkdirOptions, RmdirOptions, WatchOptions, WatchCallback, Watcher} from './FakeFS';
import {ENOSYS} from './errors';
Expand Down Expand Up @@ -154,24 +154,31 @@ export class NodeFS extends BasePortableFakeFS {
return this.realFs.existsSync(npath.fromPortablePath(p));
}

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}) {
// 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<Stats>((resolve, reject) => {
if (opts) {
// @ts-expect-error The node types are out of date
this.realFs.stat(npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
} else {
this.realFs.stat(npath.fromPortablePath(p), this.makeCallback(resolve, reject));
}
});
}

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}) {
// 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) {
// @ts-expect-error The node types are out of date
return this.realFs.statSync(npath.fromPortablePath(p), opts);
} else {
return this.realFs.statSync(npath.fromPortablePath(p));
Expand Down Expand Up @@ -204,10 +211,11 @@ export class NodeFS extends BasePortableFakeFS {
}
}

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 }) {
// 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<Stats>((resolve, reject) => {
if (opts) {
// @ts-expect-error - TS does not know this takes options
Expand All @@ -218,10 +226,14 @@ export class NodeFS extends BasePortableFakeFS {
});
}

// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/51d793492d4c2e372b01257668dcd3afc58d7352/types/node/v16/fs.d.ts#L931-L967
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 {
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) {
// @ts-expect-error - TS does not know this takes options
return this.realFs.lstatSync(npath.fromPortablePath(p), opts);
Expand Down
Loading