Last active
January 4, 2019 18:40
-
-
Save katelynsills/0c639e4bb20f791a4c997ba3e0df37c4 to your computer and use it in GitHub Desktop.
A simple command line todo app running on Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run from command line | |
// saves todo to file, appending it | |
// also displays data, highlighing high, medium, and low priority tasks | |
// Examples: | |
// node todo.js --add --todo="brush teeth" | |
// node todo.js --display | |
const fs = require('fs'); | |
const parseArgs = require('minimist'); | |
const chalk = require('chalk'); | |
const todoFile = 'todo.txt' | |
const addTodoToFile = (todo, priority='Medium') => { | |
fs.appendFile(todoFile, `${priority}: ${todo} \n`, (err) => { | |
if (err) throw err; | |
console.log('Todo was added'); | |
}); | |
} | |
const addTodo = (args) => { | |
const {todo, priority} = args | |
addTodoToFile(todo, priority) | |
} | |
const highlightTodo = (todoLine) => { | |
const [priority, todo] = todoLine.split(": ") | |
switch (priority) { | |
case 'High': | |
return chalk.red(todo) | |
case 'Medium': | |
return chalk.yellow(todo) | |
case 'Low': | |
return chalk.green(todo) | |
} | |
} | |
const displayTodos = () => { | |
try { | |
console.log(chalk.greenBright('****** TODAY\'S TODOS ********')) | |
const stream = fs.createReadStream(todoFile) | |
const lineReader = require('readline').createInterface({ | |
input: stream | |
}); | |
lineReader.on('line', (line) => { | |
console.log(highlightTodo(line)); | |
}); | |
//lineReader.on('close', () => console.log('That\'s it!')) | |
} catch (err) { | |
if (err.includes('ENOENT')) { | |
console.log('Nothing to display. Please add a todo first') | |
} | |
} | |
} | |
/* we expect { | |
_: [], | |
'add'|'display': true, | |
todo: todoText, | |
priority:'Low'|'Medium'|'High' | |
} | |
todo is required if run is 'add', and priority defaults to Medium | |
*/ | |
const args = parseArgs(process.argv.slice(2)); | |
if (args.add) { | |
addTodo(args) | |
} else if (args.display) { | |
displayTodos(); | |
} else { | |
console.log('ERROR: not a valid command;') | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment