-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-fs.js
61 lines (51 loc) · 1.08 KB
/
basic-fs.js
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
const fs = require('fs/promises')
const fsReg = require('fs')
const path = require('path')
const FILE_DIR = 'ic'
class BasicFS {
constructor() {
;(async () => {
this.createDir(FILE_DIR)
})()
}
_path (pth) {
if (pth && !pth.startsWith('/')) {
pth = '/' + pth
}
return FILE_DIR + pth
}
async createDir (dir) {
if (!fsReg.existsSync(dir)){
await fs.mkdir(dir, { recursive: true })
}
}
async exists (pth) {
try {
return await fs.stat(this._path(pth))
} catch (e) {
return false
}
}
createReadStream (pth) {
return fsReg.createReadStream(this._path(pth))
}
async readFile (pth) {
try{
const ret = await fs.readFile(this._path(pth), 'utf8')
return ret
} catch (e) {
return null
}
}
async readDir (pth) {
return fs.readdir(this._path(pth))
}
async unlink (pth) {
return fs.unlink(this._path(pth))
}
async writeFile (pth, str) {
await this.createDir(FILE_DIR + path.dirname(pth))
return fs.writeFile(this._path(pth), str)
}
}
module.exports = BasicFS