这是我参与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⛰,下一期和你们一块儿优化一下
整洁代码就要来了
而后后续再作一个可视化的功能,将报告经过一个网页展现出来
因为天天空闲时间有限,本文就先到这
若是读者还感受意犹未尽,敬请期待后续更新,或持续关注一下仓库的状态
欢迎评论区提需求,交流探讨
本系列会不断的更新迭代,直至产品初代完成