这是我参与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:后续优化
到目前为止已经支持以下指令:
这些指令都还不是最终版本,因为时间太紧凑,设计时间也较短,后期会不断完善
因为天天空闲时间有限,本文就先到这
若是读者还感受意犹未尽,敬请期待后续更新,或持续关注一下仓库的状态
欢迎评论区提需求,交流探讨
本系列会不断的更新迭代,直至产品初代完成