-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.js
More file actions
28 lines (27 loc) · 1011 Bytes
/
menu.js
File metadata and controls
28 lines (27 loc) · 1011 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
import fs from 'node:fs';
import { addToJson } from './addToJson.js';
import jsonFile from './list.json' with { type: 'json' };
export function menu (rl, path) {
rl.question('[a]dd, [d]el or cancel [any key]? \n', (answer) => {
if (answer === "a") {
rl.question('what do you want to add? \n', (content) => {
let data = fs.readFileSync(path);
let myJsonList = JSON.parse(data);
myJsonList.list.push(content);
let updatedJson = JSON.stringify(myJsonList);
addToJson(path, updatedJson);
});
} else if (answer === 'd') {
rl.question('what item(number) do you want to delete? \n', (number) => {
let data = fs.readFileSync(path);
let myJsonList = JSON.parse(data);
myJsonList.list.splice(number,1);
let updatedJson = JSON.stringify(myJsonList);
addToJson(path, updatedJson);
});
} else {
console.log('nothing added or removed try running program again.');
process.exit();
}
});
}