-
Notifications
You must be signed in to change notification settings - Fork 2
/
mock_fs_helper.ts
75 lines (66 loc) · 2.5 KB
/
mock_fs_helper.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import * as mockfs from 'mock-fs'
import * as fs from 'fs'
import * as path from 'path'
import * as _ from 'lodash'
import { AbsPath } from './path_helper';
import { isString } from 'util';
export class MockFSHelper {
public constructor(public fs_structure : {[key:string]:any} = {}) {}
public addSourceDirContents() : MockFSHelper {
this.addDirContents(this.src_dir)
return this
}
public addFile(file: AbsPath|string) : MockFSHelper {
if ( isString(file) ) {
file = new AbsPath(file)
}
if ( file._abspath == null ) {
throw "file path is null"
}
this.fs_structure[file._abspath] = file.contentsBuffer.toString()
return this
}
public addDirContents(dir: AbsPath, max_levels : number = 5) : MockFSHelper {
for ( let entry of dir.dirContents || []) {
if ( entry.isFile ) {
if ( entry._abspath == null ) {
throw "entry path is null"
}
this.fs_structure[entry._abspath] = entry.contentsBuffer.toString()
} else if ( entry.isDir && max_levels > 0) {
this.addDirContents(entry, max_levels-1)
}
}
return this
}
public get src_dir() : AbsPath {
return new AbsPath(__dirname).findUpwards("src", true)
}
public addDirs(dirs: Array<string|AbsPath>) : MockFSHelper {
for ( let dir of dirs ) {
this.addDirContents(new AbsPath(dir))
}
return this
}
public static ls(dir: AbsPath|string, max_levels : number = 5, with_contents_of : Array<string> | null = null) : {[key:string]:any} {
let result : {[key:string]:any} = {}
if ( typeof(dir) == "string") dir = new AbsPath(dir)
for ( let entry of dir.dirContents || []) {
// console.log(entry.abspath)
if ( entry.isFile ) {
if ( with_contents_of && (with_contents_of[0] == '*' || _.includes(with_contents_of, entry._abspath))) {
result[entry.basename] = entry.contentsBuffer.toString()
} else {
result[entry.basename] = "<file>"
}
} else if ( entry.isDir ) {
if ( max_levels > 0) {
result[entry.basename] = this.ls(entry, max_levels - 1, with_contents_of)
} else {
result[entry.basename] = "<dir>"
}
}
}
return result
}
}