Skip to content

Instantly share code, notes, and snippets.

@sroehrl
Created March 11, 2020 14:16
Show Gist options
  • Save sroehrl/e9fe001d662960e492d9179b576286d0 to your computer and use it in GitHub Desktop.
Save sroehrl/e9fe001d662960e492d9179b576286d0 to your computer and use it in GitHub Desktop.
Little cli tasks tool as outlined in https://youtu.be/d8P7NIqQelE
const inquirer = require('inquirer');
const fs = require('fs');
const jsonPath = __dirname + '/tasks.json';
let tasks = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
function run() {
inquirer.prompt([
{
name: 'action',
type: 'list',
message: 'What do you want to do?',
choices: ['show tasks', 'add task', 'remove task' , 'mark as completed']
},
{
name: 'new',
type: 'input',
message: 'What is your new task?',
when: (answers) => {
return answers.action === 'add task'
}
},
{
name: 'dedicated',
type: 'list',
message: 'Which task?',
when: (answers) => {
return answers.action === 'remove task' || answers.action === 'mark as completed'
},
choices: function () {
let i, ids = [];
for (i = 0; i < tasks.length; i++) {
ids.push(i);
}
return ids;
}
}
]).then(answers => {
switch (answers.action) {
case 'show tasks':
console.table(tasks);
break;
case 'remove task':
tasks.splice(answers.dedicated,1);
break;
case 'add task':
tasks.push({
title: answers.new,
done: false
});
break;
case 'mark as completed':
tasks[answers.dedicated].done = true;
break;
}
writeToFile();
run();
});
}
function writeToFile(){
fs.writeFileSync(jsonPath, JSON.stringify(tasks));
}
run();
{
"name": "cli-tasks",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"inquirer": "^7.1.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index"
},
"author": "neoan",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment