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