Skip to content

Commit

Permalink
refactor: update node types (#4764)
Browse files Browse the repository at this point in the history
* refactor: update node types

* chore: dedupe

* refactor: remove unnecessary switch

Co-authored-by: merceyz <merceyz@users.noreply.github.com>
(cherry picked from commit 6302ecb)
  • Loading branch information
paul-soporan authored and merceyz committed Dec 25, 2023
1 parent 1c93cf6 commit 38189ee
Show file tree
Hide file tree
Showing 27 changed files with 240 additions and 243 deletions.
142 changes: 63 additions & 79 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 @@ -13,7 +13,7 @@
"@babel/preset-typescript": "^7.23.3",
"@babel/register": "^7.22.15",
"@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 @@ -39,7 +39,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 @@ -338,7 +338,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
1 change: 0 additions & 1 deletion packages/yarnpkg-core/sources/scriptUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,6 @@ export function isNodeScript(p: PortablePath) {
xfs.closeSync(fd);
}

// @ts-expect-error - Types are incorrect
const magic = buf.readUint32BE();
if (
magic === 0xcafebabe || // OSX Universal Binary
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 @@ -8,6 +8,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 @@ -40,12 +42,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 @@ -62,16 +64,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 @@ -191,10 +193,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 @@ -240,11 +242,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 @@ -255,11 +257,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 @@ -302,7 +295,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 @@ -313,7 +306,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 @@ -322,7 +315,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 @@ -333,7 +326,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 @@ -363,7 +356,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 @@ -374,7 +366,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 @@ -384,13 +375,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 @@ -429,18 +418,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

0 comments on commit 38189ee

Please sign in to comment.