/
composite.ts
39 lines (32 loc) · 946 Bytes
/
composite.ts
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
import * as Path from 'path';
import * as FS from 'fs';
abstract class FileSystemObject {
constructor(
public path: string,
public parent?: FileSystemObject
) { }
get basename(): string {
return Path.basename(this.path);
}
}
class FolderObject extends FileSystemObject {
items: FileSystemObject[];
constructor(path: string, parent?: FileSystemObject) {
super(path, parent);
this.items = FS
.readdirSync(this.path)
.map(path => {
let stats = FS.statSync(path);
if (stats.isFile()) {
return new FileObject(path, this);
} else {
return new FolderObject(path, this);
}
});
}
}
class FileObject extends FileSystemObject {
readAll(): Buffer {
return FS.readFileSync(this.path);
}
}