From 22a0f0a4e216e53853310fa4506b562530b765c3 Mon Sep 17 00:00:00 2001 From: clairetay96 Date: Fri, 21 Aug 2020 19:55:55 +0800 Subject: [PATCH] Assignment complete --- data.json | 1 + index.js | 63 ++++++++++++++++++++++++++++++++++++++--------- package-lock.json | 28 +++++++++++++++++++++ package.json | 22 +++++++++++++++++ todo.js | 58 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 160 insertions(+), 12 deletions(-) create mode 100644 data.json create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 todo.js diff --git a/data.json b/data.json new file mode 100644 index 0000000..3c898e5 --- /dev/null +++ b/data.json @@ -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":""}]} diff --git a/index.js b/index.js index 9a12786..911c4af 100644 --- a/index.js +++ b/index.js @@ -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 { + for(i=1;i{ + 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; + +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..2bf6db9 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,28 @@ +{ + "name": "cli-todo", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "graceful-fs": { + "version": "4.2.4", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.4.tgz", + "integrity": "sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==", + "optional": true + }, + "jsonfile": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.0.1.tgz", + "integrity": "sha512-jR2b5v7d2vIOust+w3wtFKZIfpC2pnRmFAhAC/BuweZFQR8qZzxH1OyrQ10HmdVYiXWkYUqPVsz91cG7EL2FBg==", + "requires": { + "graceful-fs": "^4.1.6", + "universalify": "^1.0.0" + } + }, + "universalify": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz", + "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..608737c --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/todo.js b/todo.js new file mode 100644 index 0000000..b50d33a --- /dev/null +++ b/todo.js @@ -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[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; + +} \ No newline at end of file