Skip to content

Commit

Permalink
feat: 🎸 make promises methods bound
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 22, 2023
1 parent f6727f3 commit 3010141
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 132 deletions.
5 changes: 3 additions & 2 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as optHelpers from '../node/options';
import * as util from '../node/util';
import { createPromisesApi } from '../node/promises';
import { FsPromises } from '../node/FsPromises';
import { pathToLocation, testDirectoryIsWritable } from './util';
import { ERRSTR, MODE } from '../node/constants';
import { strToEncoding } from '../encoding';
Expand All @@ -14,6 +14,7 @@ import { FsSynchronousApi } from '../node/types/FsSynchronousApi';
import { FsaNodeWriteStream } from './FsaNodeWriteStream';
import { FsaNodeReadStream } from './FsaNodeReadStream';
import { FsaNodeCore } from './FsaNodeCore';
import { FileHandle } from '../node/FileHandle';
import type { FsCallbackApi, FsPromisesApi } from '../node/types';
import type * as misc from '../node/types/misc';
import type * as opts from '../node/types/options';
Expand All @@ -38,7 +39,7 @@ const noop: (...args: any[]) => any = () => {};
export class FsaNodeFs extends FsaNodeCore implements FsCallbackApi, FsSynchronousApi, FsCommonObjects {
// ------------------------------------------------------------ FsPromisesApi

public readonly promises: FsPromisesApi = createPromisesApi(this);
public readonly promises: FsPromisesApi = new FsPromises(this, FileHandle);

// ------------------------------------------------------------ FsCallbackApi

Expand Down
79 changes: 79 additions & 0 deletions src/node/FsPromises.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { promisify } from './util';
import { constants } from '../constants';
import type * as opts from './types/options';
import type * as misc from './types/misc';
import type { FsCallbackApi, FsPromisesApi } from './types';

export class FsPromises implements FsPromisesApi {
public readonly constants = constants;

public constructor(
protected readonly fs: FsCallbackApi,
public readonly FileHandle: new (...args: unknown[]) => misc.IFileHandle,
) {}

public readonly cp = promisify(this.fs, 'cp');
public readonly opendir = promisify(this.fs, 'opendir');
public readonly statfs = promisify(this.fs, 'statfs');
public readonly lutimes = promisify(this.fs, 'lutimes');
public readonly access = promisify(this.fs, 'access');
public readonly chmod = promisify(this.fs, 'chmod');
public readonly chown = promisify(this.fs, 'chown');
public readonly copyFile = promisify(this.fs, 'copyFile');
public readonly lchmod = promisify(this.fs, 'lchmod');
public readonly lchown = promisify(this.fs, 'lchown');
public readonly link = promisify(this.fs, 'link');
public readonly lstat = promisify(this.fs, 'lstat');
public readonly mkdir = promisify(this.fs, 'mkdir');
public readonly mkdtemp = promisify(this.fs, 'mkdtemp');
public readonly readdir = promisify(this.fs, 'readdir');
public readonly readlink = promisify(this.fs, 'readlink');
public readonly realpath = promisify(this.fs, 'realpath');
public readonly rename = promisify(this.fs, 'rename');
public readonly rmdir = promisify(this.fs, 'rmdir');
public readonly rm = promisify(this.fs, 'rm');
public readonly stat = promisify(this.fs, 'stat');
public readonly symlink = promisify(this.fs, 'symlink');
public readonly truncate = promisify(this.fs, 'truncate');
public readonly unlink = promisify(this.fs, 'unlink');
public readonly utimes = promisify(this.fs, 'utimes');

public readonly readFile = (
id: misc.TFileHandle,
options?: opts.IReadFileOptions | string,
): Promise<misc.TDataOut> => {
return promisify(this.fs, 'readFile')(id instanceof this.FileHandle ? id.fd : (id as misc.PathLike), options);
};

public readonly appendFile = (
path: misc.TFileHandle,
data: misc.TData,
options?: opts.IAppendFileOptions | string,
): Promise<void> => {
return promisify(this.fs, 'appendFile')(
path instanceof this.FileHandle ? path.fd : (path as misc.PathLike),
data,
options,
);
};

public readonly open = (path: misc.PathLike, flags: misc.TFlags = 'r', mode?: misc.TMode) => {
return promisify(this.fs, 'open', fd => new this.FileHandle(this.fs, fd))(path, flags, mode);
};

public readonly writeFile = (
id: misc.TFileHandle,
data: misc.TData,
options?: opts.IWriteFileOptions,
): Promise<void> => {
return promisify(this.fs, 'writeFile')(
id instanceof this.FileHandle ? id.fd : (id as misc.PathLike),
data,
options,
);
};

public readonly watch = () => {
throw new Error('Not implemented');
};
}
125 changes: 0 additions & 125 deletions src/node/promises.ts

This file was deleted.

11 changes: 6 additions & 5 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { Readable, Writable } from 'stream';
import { constants } from './constants';
import { EventEmitter } from 'events';
import { TEncodingExtended, TDataOut, strToEncoding, ENCODING_UTF8 } from './encoding';
import { FileHandle } from './node/FileHandle';
import * as util from 'util';
import * as misc from './node/types/misc';
import * as opts from './node/types/options';
import { FsCallbackApi, WritevCallback } from './node/types/FsCallbackApi';
import { createPromisesApi } from './node/promises';
import { FsCallbackApi } from './node/types/FsCallbackApi';
import { FsPromises } from './node/FsPromises';
import { ERRSTR, FLAGS, MODE } from './node/constants';
import {
getDefaultOpts,
Expand Down Expand Up @@ -54,7 +55,7 @@ import {
unixify,
} from './node/util';
import type { PathLike, symlink } from 'fs';
import type { FsSynchronousApi } from './node/types';
import type { FsPromisesApi, FsSynchronousApi } from './node/types';

const resolveCrossPlatform = pathModule.resolve;
const {
Expand Down Expand Up @@ -288,9 +289,9 @@ export class Volume implements FsCallbackApi {
File: new (...args) => File;
};

private promisesApi = createPromisesApi(this);
private promisesApi = new FsPromises(this, FileHandle);

get promises() {
get promises(): FsPromisesApi {
if (this.promisesApi === null) throw new Error('Promise is not supported in this environment.');
return this.promisesApi;
}
Expand Down

0 comments on commit 3010141

Please sign in to comment.