這是我參與8月更文挑戰的第11天,活動詳情查看:8月更文挑戰」git
上一篇文章主要詳細介紹了工具的使用,到目前爲止已經支持了很多指令了github
本期主要作一些優化工做,新增一個指令json
timec report [option] [filenames...]
:導出報告
註冊一個新的command
,將原有的option移動下來數組
其中當[param]
後面帶有...
=> [param...]
時,標識這個參數類型是一個數組,便可以獲取輸入的多個值markdown
commander.command("report [filenames...]")
.description('Automatic generation of time management reports')
.option('-D, --day [date]', 'One day')
.option('-M, --month [month]', 'One month')
.option('-Y, --year [year]', 'One year')
.option('-R, --range [startDate_endDate]', 'A time period')
.action((filenames, cmdObj) => {
// ...code
}
複製代碼
從配置文件中獲取到記錄文件的位置app
若是沒有輸入文件名且沒有指定默認記錄文件,那麼拋出提示信息ide
const config = require(configPath)
const { recordFilepath } = config
if (filenames.length === 0 && !existsSync(recordFilepath)) {
console.log(`${recordFilepath} is not exist`);
console.log('you can use "timec upPath <recordFilepath>" set it');
return
}
複製代碼
若是沒有輸入文件,就讀取配置的文件中的內容工具
const content = getFilesContent(filenames.length === 0 ? [recordFilepath] : filenames.map(filename => {
return getFilePath(cwd, filename)
}))
複製代碼
後續的邏輯不變,跟原來的一致,這裏額外添加了一個兜底邏輯oop
當沒有指定輸出時間範圍的時候,輸出1970-2970
(手動滑稽)的數據post
// ...more code
if (month) {
const year = new Date().getFullYear()
return output(`${year}-${month}-01`, `${year}-${month}-${new Date(year, month, 0).getDate()}`)
}
// 兜底(上下1000年,但願代碼還在)
output('1970-01-01','2970-01-01')
複製代碼
直接導出默認所有的數據
timec report
複製代碼
導出某天
timec report -D 2021-08-11
複製代碼
導出今年某月
timec report -M 8
複製代碼
導出某年
timec report -Y 2021
複製代碼
導出一段時間
timec report -R 2021-08-01_2021-08-11
複製代碼
這部分邏輯原來和上述部分邏輯耦合在一塊兒,這裏也將其拆分出來
預期的指令timec output [option] [filenames...]
邏輯跟上述相同,默認會以配置中的defaultFilepath
做爲輸入文件
commander.Command('output [filenames...]')
.option('-j, --json', 'Export result as json description file')
.option('-m, --markdown', 'Export the result as a markdown file')
.option('-t, --time', 'Export the result with time')
.action((filenames, cmdObj) => {
// ...code 添加跟上述同樣的邏輯
})
複製代碼
後續邏輯基本一致
// 1.
if (filenames.length === 0 && !existsSync(recordFilepath)){
// code
}
// 2.
// 獲取全部文件的內容
// 3.
// 判斷輸入的option
const { json, markdown, time } = cmdObj
if(json){
}
// ...
複製代碼
優化後的指令以下
導出json
timec output -j
複製代碼
導出md
timec output -m
複製代碼
option能夠組合使用
使用自定義的輸入文件
timec output -mj ./file1 ./file2
複製代碼
目前的指令以下timec --help
如今的代碼就像shi⛰,下一期和你們一塊兒優化一下
整潔代碼就要來了
而後後續再作一個可視化的功能,將報告經過一個網頁展現出來
因爲天天空閒時間有限,本文就先到這
若是讀者還感受意猶未盡,敬請期待後續更新,或持續關注一下倉庫的狀態
歡迎評論區提需求,交流探討
本系列會不斷的更新迭代,直至產品初代完成