-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
manager
executable file
·42 lines (36 loc) · 1.14 KB
/
manager
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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const PATH_DB = 'db.json';
let rawdata = fs.readFileSync(PATH_DB);
let db = JSON.parse(rawdata);
switch (process.argv[2]) {
case 'get':
const commentGet = db.filter((comment) => {
return comment.thread === process.argv[3];
});
console.log(commentGet);
break;
case 'last':
const lastComments = db.sort((a, b) => b.createdAt - a.createdAt).slice(0, process.argv[3]).reverse();
console.log(lastComments);
break;
case 'update':
const commentsUpdate = db.map((comment) => {
const temp = comment;
if (temp.id === Number(process.argv[3])) {
temp.message = process.argv[4];
}
return temp;
});
fs.writeFileSync(PATH_DB, JSON.stringify(commentsUpdate));
console.log('Update!');
break;
case 'delete':
const commentDelete = db.filter((comment) => {
return comment.id !== Number(process.argv[3]);
});
fs.writeFileSync(PATH_DB, JSON.stringify(commentDelete));
console.log('Delete!');
break;
}