-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
node-fs.js
32 lines (30 loc) · 851 Bytes
/
node-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
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const url = require('url')
const http = require('./http/node-http.js')
module.exports = {
add: (image) => {
mkdirp.sync(image.relative)
const filePath = path.format({ dir: image.relative, base: image.basename })
fs.writeFileSync(filePath, image.contents, 'binary')
},
exists: (path) => {
return fs.existsSync(path)
},
read: (path, encoding = 'utf8') => {
if (path.startsWith('http:https://') || path.startsWith('https://')) {
return http.get(path, encoding)
}
if (path.startsWith('file:https://')) {
return fs.readFileSync(url.fileURLToPath(path), encoding)
}
return fs.readFileSync(path, encoding)
},
parse: (resourceId) => {
return {
dir: path.dirname(resourceId),
path: resourceId
}
}
}