-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotesManager.js
51 lines (41 loc) · 1.25 KB
/
notesManager.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
/**
* all notes stored in json file
*/
const fs = require('fs');
var prompt = require('prompt-sync')();
const filename = "notes.json";
const notesFilePath = require('os').homedir() + "/" + filename;
var NOTES = {};
try {
NOTES = JSON.parse( fs.readFileSync(notesFilePath, 'utf8') );
}catch (err){ console.log("Error", err.message) }
module.exports.show = function(note){
if( NOTES[note] ){
console.log("--------------------------------------");
console.log( NOTES[note] );
console.log("--------------------------------------");
}else{
console.log("[ error ]", note + " not found.");
}
};
module.exports.add = function(note){
console.log("--------------------------------------");
if ( NOTES[note] ) console.log( NOTES[note] );
else NOTES[note] = "";
let line;
while(line = prompt('>') ) NOTES[note] += line + "\n";
saveJsonFile(NOTES, notesFilePath);
};
module.exports.delete = function(note){
NOTES[note] = undefined;
saveJsonFile(NOTES, notesFilePath);
};
module.exports.list = function(){
for (var note in NOTES) console.log(note);
};
function saveJsonFile(jsonObject, filePath){
var jsonString = JSON.stringify(jsonObject, null, space=2);
fs.writeFile(filePath, jsonString, 'utf8', (err) => {
if(err) console.log(err.message);
});
}