Skip to content

Commit

Permalink
Change FileInfo to interface (denoland#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
ztplz authored and ry committed Sep 17, 2018
1 parent 850fca8 commit 0260aaf
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions js/stat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import { assert } from "./util";
* A FileInfo describes a file and is returned by `stat`, `lstat`,
* `statSync`, `lstatSync`.
*/
// TODO FileInfo should be an interface not a class.
export class FileInfo {
private readonly _isFile: boolean;
private readonly _isSymlink: boolean;
export interface FileInfo {
readonly _isFile: boolean;
readonly _isSymlink: boolean;
/** The size of the file, in bytes. */
len: number;
/**
Expand All @@ -38,13 +37,41 @@ export class FileInfo {
*/
mode: number | null;

/**
* Returns whether this is info for a regular file. This result is mutually
* exclusive to `FileInfo.isDirectory` and `FileInfo.isSymlink`.
*/
isFile(): boolean;

/**
* Returns whether this is info for a regular directory. This result is
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isSymlink`.
*/
isDirectory(): boolean;

/**
* Returns whether this is info for a symlink. This result is
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isDirectory`.
*/
isSymlink(): boolean;
}

class FileInfoImpl implements FileInfo {
readonly _isFile: boolean;
readonly _isSymlink: boolean;
len: number;
modified: number | null;
accessed: number | null;
created: number | null;
mode: number | null;

/* @internal */
constructor(private _msg: fbs.StatRes) {
const modified = this._msg.modified().toFloat64();
const accessed = this._msg.accessed().toFloat64();
const created = this._msg.created().toFloat64();
const mode = this._msg.mode(); // negative for invalid mode (Windows)

this._isFile = this._msg.isFile();
this._isSymlink = this._msg.isSymlink();
this.len = this._msg.len().toFloat64();
Expand All @@ -55,26 +82,14 @@ export class FileInfo {
this.mode = mode >= 0 ? mode & 0o7777 : null;
}

/**
* Returns whether this is info for a regular file. This result is mutually
* exclusive to `FileInfo.isDirectory` and `FileInfo.isSymlink`.
*/
isFile() {
return this._isFile;
}

/**
* Returns whether this is info for a regular directory. This result is
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isSymlink`.
*/
isDirectory() {
return !this._isFile && !this._isSymlink;
}

/**
* Returns whether this is info for a symlink. This result is
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isDirectory`.
*/
isSymlink() {
return this._isSymlink;
}
Expand Down Expand Up @@ -148,5 +163,5 @@ function res(baseRes: null | fbs.Base): FileInfo {
assert(fbs.Any.StatRes === baseRes!.msgType());
const res = new fbs.StatRes();
assert(baseRes!.msg(res) != null);
return new FileInfo(res);
return new FileInfoImpl(res);
}

0 comments on commit 0260aaf

Please sign in to comment.