這是我參與8月更文挑戰的第7天,活動詳情查看:8月更文挑戰」git
上一篇文章主要闡述了生成一段時間內的報告指令:github
timec -or --range <startTime>_<endTime> <filename1> <filename1> .....
有時候指望直接導出的某一天,某一月,甚至某一年的數據,爲此將會拓展幾個日期相關的指令json
timec -or --day [date]
timec -or --month [month]
timec -or --year [year]
timec -or -Y [year] -M [month]
--month
與 --year
組合使用-M
,-Y
分別是上述兩個指令的縮寫除了這部份內容,本節也將會進入新的篇章,開始開發使用指令管理任務與事務數組
本文將會涉及任務相關的指令開發:timec task [name]
markdown
本部分將會省略五官代碼(前幾篇文章已出現過)app
首先是指定到某一天:ide
option
註冊可選參數cmdObj
的day屬性拿到用戶傳入的值.option('-D, --day [date]', 'One day')
// 省略actions code
const { day } = cmdObj
複製代碼
對輸出報告的函數進行改造封裝:函數
開始時間
與結束時間
const output = (s, e) => {
const outPutPath = getFilePath(cwd, `report-${outFileName}.md`)
const json = getJSONByRange(content, s, e)
if (json.length === 0) {
console.log('沒有符合條件的數據');
return
}
const data = outPutReport(json)
createFile(outPutPath, data, false)
console.log(`導出成功`);
}
複製代碼
判斷日期是否存在,存在則直接導出工具
if (day) {
return output(day, day)
}
複製代碼
老規矩先註冊相關指令oop
.option('-M, --month [month]', 'One month')
複製代碼
若是隻有月,那麼默認年就是今年,起止時間分別是
nowYear-month-01
nowYear-month-days
插播一條技巧:如何快速獲取某年某月的天數:
Date
構造函數支持傳入年,月,日三個參數的函數重載從0開始計算
:"1-12"分別對應"0-11"getDate
方法獲取日期,則獲取到目標月份的天數例如:2021-08月的天數:
new Date(2021,8,0)
2021年9月
開始的前一天日期,即2021年8月31日
getDate
返回結果即爲31
const days = new Date(year,month,0).getDate()
複製代碼
導出邏輯以下:
new Date().getFullYear()
獲取if (month) {
const year = new Date().getFullYear()
return output(`${year}-${month}-01`, `${year}-${month}-${new Date(year, month, 0).getDate()}`)
}
複製代碼
若是是年,那麼起止時間分別就是:
year-01-01
year-12-31
這個沒得太多說法,輕車熟路寫好
.option('-Y, --year [year]', 'One year')
// ...more code
if (year) {
return output(`${year}-01-01`, `${year}-12-31`)
}
複製代碼
這個就是-M與-Y參數組合使用時的場景
只須要將上述兩種導出方式的邏輯作一個合併便可,邏輯簡單
if (year && month) {
return output(`${year}-${month}-01`, `${year}-${month}-${new Date(year, month, 0).getDate()}`)
}
複製代碼
幾個日期相關的指令搞完,接着就開始整任務相關的指令
指令格式以下
timec task [name]
複製代碼
name
參數是可選的,有以下幾種邏輯:
理清邏輯後,進入開發
在項目工程的根目錄建立一個.config/record.json
文件
/Users/sugar/Documents/fe-project/time-control
├── bin
├── src
├── test
├── .config
├────└──record.json
└── test2.md
複製代碼
配置文件結構以下:
{
"recordFilepath": "",
"tasks": [],
"defaultTaskIdx": -1,
"thing": {
"name": "",
"startTime": "2021-01-01",
"endTime": "2021-12-31",
"pauseTime": "2021-12-26"
}
}
複製代碼
這裏將主要用到tasks
與defaultTaskIdx
兩個屬性,前者記錄全部的任務,後者記錄當前正在進行的任務
使用commander.command
註冊指令:
[]
包裹的參數標識可選參數/** * 建立任務、切換任務、查看任務列表 */
commander.command("task [name]")
.alias('t')
.description('check tasks/add task/checkout task')
.action((name) => {
// ...code 後文介紹
})
複製代碼
配置文件的路徑
__dirname
與配置文件的相對路徑定位配置文件const configPath = path.join(__dirname, '../.config/record.json')
複製代碼
經過require
方法引入json配置文件
JSON.parse
進行轉換const config = require(configPath)
複製代碼
下面就到具體的業務邏輯代碼
name
tasks
defaultTaskIdx
的值const { tasks, defaultTaskIdx } = config
const idx = tasks.findIndex(v => v === name)
if(!name){
if(tasks.length===0){
console.log('no tasks, you can use command add task');
console.log('timec task [name]');
return
}
tasks.forEach((v,i)=>{
let mark = '[ ]'
if(i===+defaultTaskIdx){
mark = '[*]'
}
console.log(mark,v);
})
return
}
if (idx === -1) {
tasks.push(name)
if(tasks.length===1){
config.defaultTaskIdx = 0
}
console.log('add task success');
}else{
config.defaultTaskIdx = idx
console.log('now use task:',tasks[idx]);
}
writeFileSync(configPath,JSON.stringify(config))
複製代碼
這個指令就開發完了,時間倉促,代碼質量可能不會過高
TODO:後續優化
到目前爲止已經支持以下指令:
這些指令都還不是最終版本,因爲時間太緊湊,設計時間也較短,後期會不斷完善
因爲天天空閒時間有限,本文就先到這
若是讀者還感受意猶未盡,敬請期待後續更新,或持續關注一下倉庫的狀態
歡迎評論區提需求,交流探討
本系列會不斷的更新迭代,直至產品初代完成