Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SEI 24 Aurelia #160

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"todoItems":[{"num":". [X] - walk the doggie date added: Sat Aug 22 2020 18:57:01 GMT+0800 (Singapore Standard Time) date updated: Sat Aug 22 2020 19:02:39 GMT+0800 (Singapore Standard Time)"},{"num":". [ ] - do the laundry date added: Sat Aug 22 2020 18:58:46 GMT+0800 (Singapore Standard Time)"},{"num":". [ ] - finish your homework date added: Sat Aug 22 2020 21:22:59 GMT+0800 (Singapore Standard Time)"},{"num":". [ ] - go for a run date added: Sat Aug 22 2020 21:23:32 GMT+0800 (Singapore Standard Time)"},{"num":". [ ] - make your bed date added: Sat Aug 22 2020 21:31:20 GMT+0800 (Singapore Standard Time)"}]}
138 changes: 126 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,133 @@
console.log("works!!", process.argv[2]);
const jsonfile = require('jsonfile');
const formPhrase = require("font-ascii").default;
const file = 'data.json'

var commandType = process.argv[2];
const ENV = process.argv[0];
const PATH = process.argv[1];
const operation = process.argv[2];
const chore = process.argv[3];

console.log("Your command was: "+commandType);

const jsonfile = require('jsonfile');
//status is either [x] or [ ]
let status;
let num;
let value;
let number;
let newValue;

const file = 'data.json'

jsonfile.readFile(file, (err, obj) => {

console.log(obj);
obj["helloworld"] = "monkey";

jsonfile.writeFile(file, obj, (err) => {
console.log(err)
});
});

//helper functions

//function to add things to do list

// everytime a user adds something, push it into file.toDoItems array so you can use .length property to track number of items in the list

function addList () {
jsonfile.readFile(file, (err, obj) => {
//check for error, if not will return a promis
if(err) {
console.log("Error at jsonreadfile!")
}

number = obj["todoItems"].length+ 1
num = number.toString();
status = ". [ ] - ";
let dateAdded = new Date();
value = status + chore + " date added: " + dateAdded
console.log(num,value);
obj["todoItems"].push({
num : value,

});
formPhrase("Added to the list", { typeface: "Small", color: "magenta" })
//write changes into the file
jsonfile.writeFile(file, obj, (err) =>{
if(err){
console.log(err, "error at write file!")
}
})
})
}

// show to do list function
function showList () {
jsonfile.readFile( file, (err, obj) => {
if(err) {
console.log("Error at json read file")
}
formPhrase("To do List", { typeface: "FlowerPower", color: "cyan" });
// for loop to console everything in the array
for(i=0; i< obj["todoItems"].length; i++){
let showNum = i + 1
let showvalue = obj["todoItems"][i].num
console.log(showNum, showvalue)
}
})
}

// mark as done function

function markDone(numberToParse) {
jsonfile.readFile( file, (err,obj) => {
if(err){
console.log("error at jsonReadfile")
}
let arrayNum = parseInt(numberToParse) - 1;
for(i=0; i< obj["todoItems"].length; i++){
if(i !==arrayNum){
num = i + 1 ;
value = obj["todoItems"][i].num;
console.log(num, value)
} else {
num = i + 1;
const dateUpdated = new Date();
newValue = obj["todoItems"][i].num.replace(". [ ] - ",". [X] - ") + " date updated: " + dateUpdated;
console.log(num, newValue);
obj["todoItems"].splice(arrayNum, 1 );
obj["todoItems"].splice(arrayNum,0, {num: newValue});
}
}



jsonfile.writeFile(file, obj, (err) =>{
if(err){
console.log(err, "error at write file!")
}
})
})
}

//remove function

function removeItem (removeNum) {
jsonfile.readFile(file, (err, obj) => {
if(err){
console.log("error at jsonRead")
}
let removeIndex = removeNum -1;
obj["todoItems"].splice(removeIndex, 1);
jsonfile.writeFile(file, obj, (err) =>{
if(err){
console.log(err, "error at write file!")
}
showList();
})
})
}


//COMMAND LINE STUFF
if(operation=== "add"){
addList()
} else if(operation === "show") {
showList()
} else if (operation=== "done"){
markDone(chore)
} else if (operation === "remove") {
removeItem(chore)
}

Loading