UNPKG

tuancontact8

Version:

Contact Manager

114 lines (104 loc) 3.95 kB
#!/usr/bin/env node //console.log('Hello, world!'); //var co = require('co'); //var prompt = require('co-prompt'); //var prompt = require('prompt'); const yargs = require("yargs"); const fs = require("fs"); //const readline = require('readline'); const readlineSync = require('readline-sync'); var argv = yargs.argv; var command = argv._[0]; //read data from "note-data.json" var fetchNotes = () => { try{ var notes = fs.readFileSync("note-data.json"); return JSON.parse(notes); } catch(e){ return []; } } //save file var saveNotes = (arr) => { fs.writeFileSync("note-data.json", JSON.stringify(arr)); } //read data file conten profle var profiles; // add, list, read , remove, update if(command == "add"){ console.log("Tuan"); // profiles = fetchNotes(); // var firstName = readlineSync.question('? Enter first name... '); // var lastName = readlineSync.question('? Enter last name... '); // var phoneNumber = readlineSync.question('? Enter phone number... '); // var email = readlineSync.question('? Enter email address... '); // /*Creat id */ // var crypto = require("crypto"); // var id = crypto.randomBytes(12).toString('hex'); // /*Creat opject save a profile*/ // var profile = {id, firstName, lastName, phoneNumber, email}; // profiles.push(profile); // saveNotes(profiles); // console.log("New contact added"); }else if(command == "list"){ profiles = fetchNotes(); console.log(profiles); }else if(command == "read"){ profiles = fetchNotes(); var flag = false; const findName = readlineSync.question('? Enter name need to find... '); for( i = 0; i< profiles.length; i++ ){ if(profiles[i].firstName == findName ){ flag = true; console.log("\n **********\n\n ",profiles[i], "\n"); } } if(flag == false){console.log("Name does not exist!");} }else if(command == "remove"){ profiles = fetchNotes(); var flag1 = false; const findID = readlineSync.question('? Enter ID need to delete... '); for( i = 0; i< profiles.length; i++ ){ if(profiles[i].id == findID ){ flag1 = true; profiles.splice(i, 1); saveNotes(profiles); console.log("Delete successfully."); break; } } if(flag1 == false){ console.log("ID does not exist!")} }else if(command == "update"){ profiles = fetchNotes(); var flag2 = false; var id; const findID1 = readlineSync.question('? Enter ID need to update... '); for( i = 0; i< profiles.length; i++ ){ if(profiles[i].id == findID1 ){ flag2 = true; console.log("Object need to update:\n ",profiles[i]); profiles.splice(i, 1); console.log("\n ==============>"); break; } } if(flag2 == false){ console.log("ID does not exist!"); }else{ id = findID1; var firstName = readlineSync.question('? Enter first name update... '); var lastName = readlineSync.question('? Enter last name update... '); var phoneNumber = readlineSync.question('? Enter phone number update... '); var email = readlineSync.question('? Enter email address update... '); var profile = {id, firstName, lastName, phoneNumber,email}; profiles.push(profile); saveNotes(profiles); console.log("Update successful."); } }else { console.log("Invalid command, please try again!"); } //save file // var saveNotes = (arr) => { // fs.writeFileSync("note-data.json", JSON.stringify(notes)); // }