-
Notifications
You must be signed in to change notification settings - Fork 51
/
WebpackFileSystem.ts
53 lines (42 loc) · 1.17 KB
/
WebpackFileSystem.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import * as path from 'path';
import { FileSystem } from './FileSystem';
class WebpackFileSystem implements FileSystem {
pathSeparator: string = path.sep;
constructor(private fs: any) {}
isFileInDirectory(filename: string, directory: string): boolean {
const normalizedFile = this.resolvePath(filename);
const normalizedDirectory = this.resolvePath(directory);
return (
!this.isDirectory(normalizedFile) &&
normalizedFile.indexOf(normalizedDirectory) === 0
);
}
pathExists(filename: string): boolean {
try {
this.fs.statSync(filename);
return true;
} catch (e) {
return false;
}
}
readFileAsUtf8(filename: string): string {
return this.fs.readFileSync(filename).toString('utf8');
}
join(...paths: string[]): string {
return path.join(...paths);
}
resolvePath(pathInput: string): string {
return path.resolve(pathInput);
}
listPaths(dir: string): string[] {
return this.fs.readdirSync(dir);
}
isDirectory(dir: string): boolean {
let isDir = false;
try {
isDir = this.fs.statSync(dir).isDirectory();
} catch (e) {}
return isDir;
}
}
export { WebpackFileSystem };