File size: 1,582 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
const DIRECTORY_MODE = 16877;
import fs = require('fs');
export interface BaseEntry {
relativePath: string;
isDirectory() : boolean;
}
export interface DefaultEntry extends BaseEntry {
relativePath: string;
mode?: number;
size?: number;
mtime?: number | Date; // All algorithms coerce to number
isDirectory() : boolean;
}
export default class Entry implements DefaultEntry {
relativePath: string;
mode?: number;
size?: number;
mtime?: number | Date; // All algorithms coerce to number
constructor(relativePath:string, size?:number, mtime?: number | Date, mode?: number) {
if (mode === undefined) {
const isDirectory = relativePath.charAt(relativePath.length - 1) === '/';
this.mode = isDirectory ? DIRECTORY_MODE : 0;
} else {
const modeType = typeof mode;
if (modeType !== 'number') {
throw new TypeError(`Expected 'mode' to be of type 'number' but was of type '${modeType}' instead.`);
}
this.mode = mode;
}
if (mtime !== undefined) {
this.mtime = mtime;
}
this.relativePath = relativePath;
this.size = size;
}
static isDirectory(entry: Entry) {
if (entry.mode === undefined) {
return false
} else {
return (entry.mode & 61440) === 16384
}
}
static isFile(entry: Entry) {
return !this.isDirectory(entry);
}
static fromStat(relativePath: string, stat: fs.Stats) {
return new this(relativePath, stat.size, stat.mtime, stat.mode);
}
isDirectory() {
return (this.constructor as typeof Entry).isDirectory(this);
}
};
|