-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataFile.js
More file actions
39 lines (34 loc) · 786 Bytes
/
dataFile.js
File metadata and controls
39 lines (34 loc) · 786 Bytes
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
const path = require('path')
const fs = require('fs')
const DataFilePath = path.resolve(process.env.HOME, '.task.json')
const readDataFile = callback => {
fs.readFile(DataFilePath, (error, json) => {
if (error){
if (error.message.includes('ENOENT')){
callback([])
}else{
bail(error)
}
}else{
callback(JSON.parse(json))
}
})
}
const writeDataFile = (data, callback) => {
if (!Array.isArray(data)) bail(new Error('data must be an array'))
fs.writeFile(DataFilePath, JSON.stringify(data, null, 2), (error) => {
if (error){
bail(error)
}else{
callback(data)
}
})
}
const bail = error => {
console.error(error)
process.exit(1)
}
module.exports = {
read: readDataFile,
write: writeDataFile,
}