-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
34 lines (34 loc) · 1.21 KB
/
storage.js
File metadata and controls
34 lines (34 loc) · 1.21 KB
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
const path = require('path')
const fs = require('fs')
const FILE_STORAGE = process.env.LOCAL_CONFIGURATION_STORAGE || '.storage'
if (!fs.existsSync(path.resolve(__dirname, FILE_STORAGE))) {
fs.mkdirSync(path.resolve(__dirname, FILE_STORAGE))
}
module.exports = {
// the promise returned from sync function
// set item with the key
setItem: (key, value) => {
fs.writeFileSync(path.resolve(__dirname, FILE_STORAGE, `${key}`), value)
},
// get item with the key
getItem: (key) => {
if (fs.existsSync(path.resolve(__dirname, FILE_STORAGE, `${key}`))) {
return `${fs.readFileSync(path.resolve(__dirname, FILE_STORAGE, `${key}`))}`
} else {
return null
}
},
// remove item with the key
removeItem: (key) => {
if (fs.existsSync(path.resolve(__dirname, FILE_STORAGE, `${key}`))) {
fs.unlinkSync(path.resolve(__dirname, FILE_STORAGE, `${key}`))
}
},
// clear out the storage
clear: () => {
fs.rmdirSync(path.resolve(__dirname, FILE_STORAGE))
},
// If the storage operations are async(i.e AsyncStorage)
// Then you need to sync those items into the memory in this method
sync: () => { }
}