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

refactor: update node types #4764

Merged
merged 3 commits into from
Aug 18, 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
128 changes: 61 additions & 67 deletions .pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
39 changes: 39 additions & 0 deletions .yarn/versions/392e431d.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": patch
"@yarnpkg/fslib": patch
"@yarnpkg/plugin-pnp": patch
"@yarnpkg/pnp": patch
"@yarnpkg/pnpify": patch
"@yarnpkg/sdks": 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-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/shell"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@babel/preset-typescript": "^7.18.6",
"@babel/register": "^7.18.9",
"@types/jest": "^28.1.6",
"@types/node": "^13.7.0",
"@types/node": "^18.7.6",
"@yarnpkg/cli": "workspace:^",
"@yarnpkg/core": "workspace:^",
"@yarnpkg/eslint-config": "workspace:^",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export const writeFile = async (target: PortablePath, body: string | Buffer): Pr
await xfs.writeFilePromise(target, body);
};

export const readFile = (source: PortablePath, encoding?: string): Promise<any> => {
export const readFile = (source: PortablePath, encoding?: BufferEncoding): Promise<any> => {
return xfs.readFilePromise(source, encoding);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@types/diff": "^5.0.0",
"@types/lodash": "^4.14.136",
"@types/micromatch": "^4.0.1",
"@types/node": "^13.7.0",
"@types/node": "^18.7.6",
"@types/tar": "^4.0.4",
"@types/tunnel": "^0.0.0",
"@yarnpkg/cli": "workspace:^",
Expand Down
22 changes: 11 additions & 11 deletions packages/yarnpkg-core/sources/execUtils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {PortablePath, npath, ppath} from '@yarnpkg/fslib';
import {ChildProcess} from 'child_process';
import crossSpawn from 'cross-spawn';
import {Readable, Writable} from 'stream';
import {PortablePath, npath, ppath, BufferEncodingOrBuffer} from '@yarnpkg/fslib';
import {ChildProcess} from 'child_process';
import crossSpawn from 'cross-spawn';
import {Readable, Writable} from 'stream';

import {Configuration} from './Configuration';
import {MessageName} from './MessageName';
import {Report, ReportError} from './Report';
import * as formatUtils from './formatUtils';
import {Configuration} from './Configuration';
import {MessageName} from './MessageName';
import {Report, ReportError} from './Report';
import * as formatUtils from './formatUtils';

export enum EndStrategy {
Never,
Expand All @@ -26,7 +26,7 @@ export type PipevpOptions = {

export type PipeErrorOptions = {
fileName: string;
code: number;
code: number | null;
signal: NodeJS.Signals | null;
};

Expand Down Expand Up @@ -151,7 +151,7 @@ export async function pipevp(fileName: string, args: Array<string>, {cwd, env =
process.off(`SIGTERM`, sigtermHandler);
}

if (end === EndStrategy.Always || (end === EndStrategy.ErrorCode && code > 0))
if (end === EndStrategy.Always || (end === EndStrategy.ErrorCode && code !== 0))
closeStreams();

if (code === 0 || !strict) {
Expand All @@ -166,7 +166,7 @@ export async function pipevp(fileName: string, args: Array<string>, {cwd, env =
export type ExecvpOptions = {
cwd: PortablePath;
env?: {[key: string]: string | undefined};
encoding?: string;
encoding?: BufferEncodingOrBuffer;
strict?: boolean;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/sources/miscUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ function dynamicRequireNoCache(path: string) {
try {
result = dynamicRequireNode(physicalPath);

const freshCacheEntry = realRequire.cache[physicalPath];
const freshCacheEntry = realRequire.cache[physicalPath]!;

const dynamicModule = eval(`module`) as NodeModule;
const freshCacheIndex = dynamicModule.children.indexOf(freshCacheEntry);
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/sources/worker-zip/index.js

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions packages/yarnpkg-fslib/sources/FakeFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {copyPromise, LinkStrategy} from './algorithms/copy
import {FSPath, Path, PortablePath, PathUtils, Filename} from './path';
import {convertPath, ppath} from './path';

export type BufferEncodingOrBuffer = BufferEncoding | 'buffer';

export type Stats = NodeStats & {
crc?: number;
};
Expand Down Expand Up @@ -41,12 +43,12 @@ export type OpendirOptions = Partial<{
}>;

export type CreateReadStreamOptions = Partial<{
encoding: string;
encoding: BufferEncoding;
fd: number;
}>;

export type CreateWriteStreamOptions = Partial<{
encoding: string;
encoding: BufferEncoding;
fd: number;
flags: 'a';
}>;
Expand All @@ -63,16 +65,16 @@ export type RmdirOptions = Partial<{
}>;

export type WriteFileOptions = Partial<{
encoding: string;
encoding: BufferEncoding;
mode: number;
flag: string;
}> | string;
}> | BufferEncoding;

export type WatchOptions = Partial<{
persistent: boolean;
recursive: boolean;
encoding: string;
}> | string;
encoding: BufferEncodingOrBuffer;
}> | BufferEncodingOrBuffer;

export type WatchFileOptions = Partial<{
bigint: boolean;
Expand Down Expand Up @@ -192,10 +194,10 @@ export abstract class FakeFS<P extends Path> {

abstract fstatPromise(fd: number): Promise<Stats>;
abstract fstatPromise(fd: number, opts: {bigint: true}): Promise<BigIntStats>;
abstract fstatPromise(fd: number, opts?: {bigint: boolean}): Promise<BigIntStats | Stats>;
abstract fstatPromise(fd: number, opts?: {bigint?: boolean}): Promise<BigIntStats | Stats>;
abstract fstatSync(fd: number): Stats;
abstract fstatSync(fd: number, opts: {bigint: true}): BigIntStats;
abstract fstatSync(fd: number, opts?: {bigint: boolean}): BigIntStats | Stats;
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>;
Expand Down Expand Up @@ -238,11 +240,11 @@ export abstract class FakeFS<P extends Path> {
abstract copyFilePromise(sourceP: P, destP: P, flags?: number): Promise<void>;
abstract copyFileSync(sourceP: P, destP: P, flags?: number): void;

abstract appendFilePromise(p: FSPath<P>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): Promise<void>;
abstract appendFileSync(p: FSPath<P>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): void;
abstract appendFilePromise(p: FSPath<P>, content: string | Uint8Array, opts?: WriteFileOptions): Promise<void>;
abstract appendFileSync(p: FSPath<P>, content: string | Uint8Array, opts?: WriteFileOptions): void;

abstract writeFilePromise(p: FSPath<P>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): Promise<void>;
abstract writeFileSync(p: FSPath<P>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions): void;
abstract writeFilePromise(p: FSPath<P>, content: string | NodeJS.ArrayBufferView, opts?: WriteFileOptions): Promise<void>;
abstract writeFileSync(p: FSPath<P>, content: string | NodeJS.ArrayBufferView, opts?: WriteFileOptions): void;

abstract unlinkPromise(p: P): Promise<void>;
abstract unlinkSync(p: P): void;
Expand All @@ -253,11 +255,13 @@ export abstract class FakeFS<P extends Path> {
lutimesPromise?(p: P, atime: Date | string | number, mtime: Date | string | number): Promise<void>;
lutimesSync?(p: P, atime: Date | string | number, mtime: Date | string | number): void;

abstract readFilePromise(p: FSPath<P>, encoding: 'utf8'): Promise<string>;
abstract readFilePromise(p: FSPath<P>, encoding?: string): Promise<Buffer>;
abstract readFilePromise(p: FSPath<P>, encoding?: null): Promise<Buffer>;
abstract readFilePromise(p: FSPath<P>, encoding: BufferEncoding): Promise<string>;
abstract readFilePromise(p: FSPath<P>, encoding?: BufferEncoding | null): Promise<Buffer | string>;

abstract readFileSync(p: FSPath<P>, encoding: 'utf8'): string;
abstract readFileSync(p: FSPath<P>, encoding?: string): Buffer;
abstract readFileSync(p: FSPath<P>, encoding?: null): Buffer;
abstract readFileSync(p: FSPath<P>, encoding: BufferEncoding): string;
abstract readFileSync(p: FSPath<P>, encoding?: BufferEncoding | null): Buffer | string;

abstract readlinkPromise(p: P): Promise<P>;
abstract readlinkSync(p: P): P;
Expand Down
39 changes: 15 additions & 24 deletions packages/yarnpkg-fslib/sources/NodeFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export class NodeFS extends BasePortableFakeFS {

this.realFs = realFs;

// @ts-expect-error
if (typeof this.realFs.lutimes !== `undefined`) {
this.lutimesPromise = this.lutimesPromiseImpl;
this.lutimesSync = this.lutimesSyncImpl;
Expand Down Expand Up @@ -159,9 +158,8 @@ export class NodeFS extends BasePortableFakeFS {
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) => {
return await new Promise<BigIntStats | 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));
Expand All @@ -178,7 +176,6 @@ export class NodeFS extends BasePortableFakeFS {
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 All @@ -189,9 +186,8 @@ export class NodeFS extends BasePortableFakeFS {
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<Stats>((resolve, reject) => {
return await new Promise<BigIntStats | Stats>((resolve, reject) => {
if (opts) {
// @ts-expect-error - The node typings doesn't know about the options
this.realFs.fstat(fd, opts, this.makeCallback(resolve, reject));
} else {
this.realFs.fstat(fd, this.makeCallback(resolve, reject));
Expand All @@ -204,7 +200,6 @@ export class NodeFS extends BasePortableFakeFS {
fstatSync(fd: number, opts?: {bigint: boolean}): BigIntStats | Stats
fstatSync(fd: number, opts?: {bigint: boolean}) {
if (opts) {
// @ts-expect-error - The node typings doesn't know about the options
return this.realFs.fstatSync(fd, opts);
} else {
return this.realFs.fstatSync(fd);
Expand All @@ -216,9 +211,8 @@ export class NodeFS extends BasePortableFakeFS {
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) => {
return await new Promise<BigIntStats | Stats>((resolve, reject) => {
if (opts) {
// @ts-expect-error - TS does not know this takes options
this.realFs.lstat(npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
} else {
this.realFs.lstat(npath.fromPortablePath(p), this.makeCallback(resolve, reject));
Expand All @@ -235,7 +229,6 @@ export class NodeFS extends BasePortableFakeFS {
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);
} else {
return this.realFs.lstatSync(npath.fromPortablePath(p));
Expand Down Expand Up @@ -292,7 +285,7 @@ export class NodeFS extends BasePortableFakeFS {
return this.realFs.copyFileSync(npath.fromPortablePath(sourceP), npath.fromPortablePath(destP), flags);
}

async appendFilePromise(p: FSPath<PortablePath>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions) {
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) {
Expand All @@ -303,7 +296,7 @@ export class NodeFS extends BasePortableFakeFS {
});
}

appendFileSync(p: PortablePath, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions) {
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);
Expand All @@ -312,7 +305,7 @@ export class NodeFS extends BasePortableFakeFS {
}
}

async writeFilePromise(p: FSPath<PortablePath>, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions) {
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) {
Expand All @@ -323,7 +316,7 @@ export class NodeFS extends BasePortableFakeFS {
});
}

writeFileSync(p: PortablePath, content: string | Buffer | ArrayBuffer | DataView, opts?: WriteFileOptions) {
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);
Expand Down Expand Up @@ -353,7 +346,6 @@ export class NodeFS extends BasePortableFakeFS {
}

private async lutimesPromiseImpl(this: NodeFS, p: PortablePath, atime: Date | string | number, mtime: Date | string | number) {
// @ts-expect-error: Not yet in DefinitelyTyped
const lutimes = this.realFs.lutimes;
if (typeof lutimes === `undefined`)
throw ENOSYS(`unavailable Node binding`, `lutimes '${p}'`);
Expand All @@ -364,7 +356,6 @@ export class NodeFS extends BasePortableFakeFS {
}

private lutimesSyncImpl(this: NodeFS, p: PortablePath, atime: Date | string | number, mtime: Date | string | number) {
// @ts-expect-error: Not yet in DefinitelyTyped
const lutimesSync = this.realFs.lutimesSync;
if (typeof lutimesSync === `undefined`)
throw ENOSYS(`unavailable Node binding`, `lutimes '${p}'`);
Expand All @@ -374,13 +365,11 @@ export class NodeFS extends BasePortableFakeFS {

async mkdirPromise(p: PortablePath, opts?: MkdirOptions) {
return await new Promise<string | undefined>((resolve, reject) => {
// @ts-expect-error - Types are outdated, the second argument in the callback is either a string or undefined
this.realFs.mkdir(npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
});
}

mkdirSync(p: PortablePath, opts?: MkdirOptions): string | undefined {
// @ts-expect-error - Types are outdated, returns either a string or undefined
return this.realFs.mkdirSync(npath.fromPortablePath(p), opts);
}

Expand Down Expand Up @@ -419,18 +408,20 @@ export class NodeFS extends BasePortableFakeFS {
return this.realFs.symlinkSync(npath.fromPortablePath(target.replace(/\/+$/, ``) as PortablePath), npath.fromPortablePath(p), type);
}

readFilePromise(p: FSPath<PortablePath>, encoding: 'utf8'): Promise<string>;
readFilePromise(p: FSPath<PortablePath>, encoding?: string): Promise<Buffer>;
async readFilePromise(p: FSPath<PortablePath>, encoding?: string) {
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: 'utf8'): string;
readFileSync(p: FSPath<PortablePath>, encoding?: string): Buffer;
readFileSync(p: FSPath<PortablePath>, encoding?: string) {
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);
}
Expand Down
Loading