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 Claire Assignment complete #162

Open
wants to merge 1 commit 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":[{"done":"","item":"","createdAt":"created_at","updatedAt":"updated_at"},{"done":"[ ]","item":"grocery shopping","createdAt":"2020-8-21 19:47:12","updatedAt":""},{"done":"[X]","item":"homework","createdAt":"2020-8-21 19:47:17","updatedAt":"2020-8-21 19:54:0"},{"done":"[ ]","item":"revise study","createdAt":"2020-8-21 19:53:1","updatedAt":""},{"done":"[ ]","item":"revise study","createdAt":"2020-8-21 19:53:20","updatedAt":""},{"done":"[ ]","item":"revise study","createdAt":"2020-8-21 19:53:43","updatedAt":""}]}
63 changes: 51 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
console.log("works!!", process.argv[2]);
var jsonfile = require('jsonfile')

var commandType = process.argv[2];
var inputs = process.argv
var filename = 'data.json'

console.log("Your command was: "+commandType);
var command = inputs[2]

const jsonfile = require('jsonfile');
if(command=="add"){
jsonfile.readFile(filename, (err, obj)=>{
let newToDoItem = {
"done": "[ ]",
"item": inputs[3],
"createdAt": generateDate(),
"updatedAt": ""
}
obj.todoItems.push(newToDoItem)
jsonfile.writeFile(filename, obj, (err)=>{console.log(err)})
})
} else if(command=="show"){
jsonfile.readFile(filename, (err, obj)=>{
//find length of longest string
let longestString = 0
for(let i=1;i<obj.todoItems.length;i++){
longestString = Math.max(longestString, obj.todoItems[i].item.length)
}

const file = 'data.json'
var columnheaders = obj.todoItems[0]
console.log(" ".repeat(12+longestString), columnheaders.createdAt, " ".repeat(12), columnheaders.updatedAt)

jsonfile.readFile(file, (err, obj) => {
for(i=1;i<obj.todoItems.length;i++){
let thingToDo=obj.todoItems[i]
let columnSpace = " ".repeat(longestString - obj.todoItems[i].item.length) //make sure dates line up
console.log(i,".", thingToDo.done, "-", thingToDo.item, columnSpace, thingToDo.createdAt, " ", thingToDo.updatedAt)
}
})
} else if(command=="done"){
let index = parseInt(inputs[3])

console.log(obj);
obj["helloworld"] = "monkey";
jsonfile.readFile(filename,(err,obj)=>{
obj.todoItems[index].done = "[X]"
obj.todoItems[index].updatedAt = generateDate()
jsonfile.writeFile(filename, obj, (err)=>console.log(err))
})
} else if(command=="delete"){
let index = parseInt(inputs[3])
jsonfile.readFile(filename,(err,obj)=>{
obj.todoItems.splice(index, 1)
jsonfile.writeFile(filename, obj, (err)=>console.log(err))
})
}

jsonfile.writeFile(file, obj, (err) => {
console.log(err)
});
});
function generateDate(){
let today = new Date();
let date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
let time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
return date+' '+time;

}
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "cli-todo",
"version": "1.0.0",
"description": "![https://i.giphy.com/media/26ufnwz3wDUli7GU0/giphy.webp](https://i.giphy.com/media/26ufnwz3wDUli7GU0/giphy.webp)",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/clairetay96/cli-todo.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/clairetay96/cli-todo/issues"
},
"homepage": "https://github.com/clairetay96/cli-todo#readme",
"dependencies": {
"jsonfile": "^6.0.1"
}
}
58 changes: 58 additions & 0 deletions todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var jsonfile = require('jsonfile')

var inputs = process.argv
var filename = 'data.json'

var command = inputs[2]

if(command=="add"){
jsonfile.readFile(filename, (err, obj)=>{
let newToDoItem = {
"done": "[ ]",
"item": inputs[3],
"createdAt": generateDate(),
"updatedAt": ""
}
obj.todoItems.push(newToDoItem)
jsonfile.writeFile(filename, obj, (err)=>{console.log(err)})
})
} else if(command=="show"){
jsonfile.readFile(filename, (err, obj)=>{
//find length of longest string
let longestString = 0
for(let i=1;i<obj.todoItems.length;i++){
longestString = Math.max(longestString, obj.todoItems[i].item.length)
}

var columnheaders = obj.todoItems[0]
console.log(" ".repeat(12+longestString), columnheaders.createdAt, " ".repeat(10), columnheaders.updatedAt)

for(i=1;i<obj.todoItems.length;i++){
let thingToDo=obj.todoItems[i]
let columnSpace = " ".repeat(longestString - obj.todoItems[i].item.length)
console.log(i,".", thingToDo.done, "-", thingToDo.item, columnSpace, thingToDo.createdAt, " ", thingToDo.updatedAt)
}
})
} else if(command=="done"){
let index = parseInt(inputs[3])

jsonfile.readFile(filename,(err,obj)=>{
obj.todoItems[index].done = "[X]"
obj.todoItems[index].updatedAt = generateDate()
jsonfile.writeFile(filename, obj, (err)=>console.log(err))
})
} else if(command=="delete"){
let index = parseInt(inputs[3])
jsonfile.readFile(filename,(err,obj)=>{
obj.todoItems.splice(index, 1)
jsonfile.writeFile(filename, obj, (err)=>console.log(err))
})
}

function generateDate(){
let today = new Date();
let date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
let time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
return date+' '+time;

}