Cli Todo命令行todo工具使用演示
前言
bald3r-node-todo是一個(gè)用node.js開(kāi)發(fā)的,主要用于命令行的todo工具,主要使用了fs模塊,目前已經(jīng)發(fā)布至npm
本工具主要使用了面向接口的編程思想,并用jest進(jìn)行單元測(cè)試
鏈接
bald3r-node-todo - npm (npmjs.com)
使用演示
- 首先使用
yarn或npm安裝bald3r-node-todo
npm install bald3r-todo yarn global add bald3r-todo
安裝完成后就可以使用全局命令t來(lái)使用了

使用命令行添加一個(gè)待辦t add [taskName]

查看當(dāng)前待辦

二級(jí)菜單


清空所有待辦t clear

實(shí)現(xiàn)過(guò)程
實(shí)現(xiàn)命令行參數(shù)
這里我使用了commander庫(kù)來(lái)實(shí)現(xiàn)參數(shù)功能
program
.command('add')
.description('add a task')
.action((...args) => {
const words = args.slice(0, -1).join(' ')
api.add(words).then(() => {
console.log('The task has been successfully added')
}, () => {
console.log('Failed to add the task')
})
})
program
.command('clear')
.description('clear all tasks')
.action(() => {
api.clear().then(() => {
console.log('All tasks have been successfully removed')
}, () => {
console.log('Failed to remove all the tasks')
})
})
commander默認(rèn)會(huì)有兩個(gè)參數(shù),一個(gè)是node的路徑,一個(gè)是當(dāng)前文件的路徑,因此我們判斷參數(shù)的數(shù)量是否為2就可以判斷用戶(hù)是否傳參
如果用戶(hù)沒(méi)有傳參,則顯示所有的待辦項(xiàng)
if (process.argv.length === 2) {
api.showAll()
}
實(shí)現(xiàn)可以操作的命令行
這里我使用了inquirer庫(kù)來(lái)給命令行做了美化,實(shí)現(xiàn)可以用方向鍵和回車(chē)控制的UI界面
inquirer的使用非常簡(jiǎn)單,這里我展示二級(jí)菜單作為參考
function askForAction(list, index) {
const actions = {markAsUndone, markAsDone, changeTitle, removeTask}
inquirer.prompt({
type: 'list',
name: 'action',
message: 'What to do with the task?',
choices: [
{name: 'Exit', value: 'quit'},
{name: 'Mark as Done', value: 'markAsDone'},
{name: 'Mark as Undone', value: 'markAsUndone'},
{name: 'Edit Title', value: 'changeTitle'},
{name: 'Delete', value: 'removeTask'},
]
}).then(answer2 => {
const action = actions[answer2.action]
action && action(list, index)
})
}
這樣便實(shí)現(xiàn)了下圖的二級(jí)菜單

待辦項(xiàng)保存在本地
使用node.js的fs模塊來(lái)實(shí)現(xiàn)對(duì)文件的讀寫(xiě),這里涉及一個(gè)保存路徑的問(wèn)題,在本項(xiàng)目中,為了方便使用了~目錄,所有數(shù)據(jù)保存在~/.todo中
獲取~目錄:
const homedir = require('os').homedir()
const home = process.env.HOME || homedir
考慮到跨平臺(tái)使用路徑的表示方式不同,這里使用了node.js中的path模塊:
const p = require('path')
const dbPath = p.join(home, '.todo')
然后使用fs模塊中的fs.readFile()和fs.writeFile()即可完成對(duì)數(shù)據(jù)的讀寫(xiě)。這里需要注意這兩個(gè)操作都是異步的,因此用到了Promise,這里的{flag: 'a+'}是表示讀取文件,若不存在則創(chuàng)建一個(gè):
read(path = dbPath) {
return new Promise((resolve, reject) => {
fs.readFile(path, {flag: 'a+'}, (error, data) => {
if (error) return reject(error)
let list
try {
list = JSON.parse(data.toString())
} catch (error2) {
list = []
}
resolve(list)
})
})
}以上就是Cli Todo命令行todo工具使用演示的詳細(xì)內(nèi)容,更多關(guān)于Cli Todo命令行todo工具的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Koa中更方便簡(jiǎn)單發(fā)送響應(yīng)的方式
這篇文章主要介紹了詳解Koa中更方便簡(jiǎn)單發(fā)送響應(yīng)的方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
一個(gè)簡(jiǎn)單的node.js界面實(shí)現(xiàn)方法
今天小編就為大家分享一篇一個(gè)簡(jiǎn)單的node.js界面實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
nodejs實(shí)現(xiàn)用戶(hù)登錄路由功能
這篇文章主要介紹了nodejs中實(shí)現(xiàn)用戶(hù)登錄路由功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
手把手教你實(shí)現(xiàn) Promise的使用方法
這篇文章主要介紹了手把手教你實(shí)現(xiàn) Promise的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
node簡(jiǎn)單實(shí)現(xiàn)一個(gè)更改頭像功能的示例
本篇文章主要介紹了node簡(jiǎn)單實(shí)現(xiàn)一個(gè)更改頭像功能的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12

