這是我參與8月更文挑戰的第8天,活動詳情查看:8月更文挑戰」git
上一篇文章主要闡述了多種日期的數據導出與任務管理相關的指令開發github
本文將會涉及指令:json
timec task -d [name]
option
參數-d
,標識要移除這個任務timec upPath <recordFilepath>
timec thing -s [name]
-s
標識,是否暫存任務狀態的可選參數TODO: 補一張圖數組
這個功能只需在昨天開發的任務管理指令的基礎上添加一個可選指令便可markdown
option
方法註冊--del
可選參數cmdObj
上拿到del
的值commander.command("task [name]")
.option('-d, --del', 'Delete task or thing')
.action((name, cmdObj) => {
const { del } = cmdObj
// ...code
}
複製代碼
若是del的值爲true,代表設置了del這個optionapp
defaultTaskIdx
置爲-1const idx = tasks.findIndex(v => v === name)
if (del) {
tasks.splice(idx, 1)
console.log(`del ${name} success`);
config.defaultTaskIdx = tasks.length ? 0 : -1
if (config.defaultTaskIdx === 0) {
console.log('now use task:', tasks[config.defaultTaskIdx]);
}
}
writeFileSync(configPath, JSON.stringify(config))
複製代碼
刪除任務的功能,就簡簡單單的搞定了ide
在未引入經過指令記錄所作任務的耗時時工具
須要手動將這些記錄添加到記錄文件中oop
指望後續能經過指令,就自動計算事務耗時,而後將記錄自動寫到目標文件中post
若是目標文件的地址是拼在參數中,不免在每次使用的時候稍顯麻煩
爲了不這個小麻煩,就將目標文件的路徑存到配置文件中
其中存儲位置對應配置文件中的recordFilepath
屬性
{
"recordFilepath":"/Users/sugar/Documents/fe-project/time-control/test.md"
}
複製代碼
經過commander.command
註冊指令,設置爲upPath <recordFilepath>
/** * 更改默認記錄文件的位置 */
commander.command("upPath <recordFilepath>")
// .alias('urp')
.description('update config recordFilepath')
.action((recordFilePath) => {
// ...code
})
複製代碼
經過指令執行目錄cwd
與傳入的文件相對路徑recordFilePath
獲得輸出文件所在位置的絕對路徑
而後將這個絕對路徑賦值給配置文件的recordFilePath
屬性
經過fs.existsSync
方法,判斷這個文件是否存在,若是不存在就自動建立
最後經過fs.writeFileSync
更新配置文件
const config = require(configPath)
const fullPath = path.resolve(cwd, recordFilePath)
config.recordFilepath = fullPath
if (!existsSync(fullPath)) {
// 自動建立空文件
createFile(fullPath, '', false)
}
writeFileSync(configPath, JSON.stringify(config))
console.log('set recordFilePath success:', fullPath);
複製代碼
設置輸出文件路徑的指令開發到這就完畢了
指望經過簡單的timec thing -s [name]
,便可完成事務的添加,結束,自動寫入到文件
其中--stop
option是可選的,標識結束這個工做,將其寫入到文件之中去
首先註冊指令,而後從配置文件中取出相關的配置項目
其中thing
屬性的結構以下
{
"name":"abc",
"startTime":"2021-08-08 22:18:19"
}
複製代碼
分別存放當前進行中的事務名和事務開始時間
commander.command("thing [name]")
.option('-s, --stop', 'stop a thing ')
.description('update config recordFilepath')
.action((name, cmdObj) => {
const config = require(configPath)
const { thing, recordFilepath, tasks, defaultTaskIdx } = config
const task = tasks[defaultTaskIdx]
})
複製代碼
首先作一些判斷,避免引起錯誤
若是沒有設置,拋出響應提示信息
const s = new Date(thing.startTime)
if (!existsSync(recordFilepath)) {
console.log(`${recordFilepath} is not exist`);
console.log('you can use "timec upPath <recordFilepath>" set it');
return
}
if (!task) {
console.log('not set task');
console.log('you can use "timec task [name]" set it');
return
}
複製代碼
若是沒有傳入事件名稱name
,表示使用查詢功能,打印當前事務的基本信息
若是設置告終束的標誌stop
,則將這個事務的記錄寫入到文件之中去,更新配置文件
if (!name) {
if (!thing.name) {
console.log('Events not in progress');
return
}
const { stop } = cmdObj
if (stop) {
writeRecord(recordFilepath, task, thing.name, thing.startTime)
thing.name = ''
thing.startTime = ''
writeFileSync(configPath, JSON.stringify(config))
return
}
console.log('------');
console.log(`-name: ${thing.name}`);
console.log(`-start: ${s.format('yyyy-MM-dd hh:mm:ss')}`);
// TODO:時分秒
console.log(`-duration: ${Date.now() - s} mss`);
console.log('------');
return
}
複製代碼
具體的輸出到文件的方法writeRecord
邏輯稍微有些複雜,今日時間實在有限,明日再詳細分析
因爲天天空閒時間有限,本文就先到這
若是讀者還感受意猶未盡,敬請期待後續更新,或持續關注一下倉庫的狀態
歡迎評論區提需求,交流探討
本系列會不斷的更新迭代,直至產品初代完成