-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddtodo.js
55 lines (44 loc) · 1.71 KB
/
addtodo.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
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const randomId = Math.floor(Math.random() * 1000)
function addtodo() {
// sure if file exists or not exists
if (fs.existsSync('./data.json')) {
//reading file data by using non-blocking method
fs.readFile('./data.json', 'utf8', (err, data) => {
//after reading file data sure if we have an error or real data
if (err) {
console.log('Error reading', err);
} else {
//take from the user taskName
if (!data) {
console.log('Opps 404 🤪 ');
} else {
rl.question('Enter taskName ? : ', (taskName) => {
//convert json data into readable format data
const todos = JSON.parse(data);
//adding data from the user to the json file
todos.push({ id: randomId, task: taskName })
//written data into the json file
fs.writeFile('./data.json', JSON.stringify(todos, null, 2), (err) => {
if (err) {
console.log('Error From Reading File ', err);
} else {
console.log('Successfully added the task ');
}
})
})
}
}
})
} else {
console.log(' Opps 404 ');
}
}
module.exports = {
addtodo
}