这是我参与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
逻辑稍微有些复杂,今日时间实在有限,明日再详细分析
因为天天空闲时间有限,本文就先到这
若是读者还感受意犹未尽,敬请期待后续更新,或持续关注一下仓库的状态
欢迎评论区提需求,交流探讨
本系列会不断的更新迭代,直至产品初代完成