作一個CLI版的時間管理工具(七)

作一個CLI版的時間管理工具(七)

這是我參與8月更文挑戰的第8天,活動詳情查看:8月更文挑戰git

前言

上一篇文章主要闡述了多種日期的數據導出與任務管理相關的指令開發github

本文將會涉及指令:json

  1. 刪除指定任務:timec task -d [name]
    1. 添加了一個option參數-d,標識要移除這個任務
  2. 設置記錄默認輸出位置:timec upPath <recordFilepath>
    1. 用戶後續經過指令添加/完成事務均會在此記錄
  3. 開始/結束具體的事務:timec thing -s [name]
    1. 其中-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

  • 使用splice方法,從任務數組中刪除這一項
  • 刪除後更新配置文件
    • 若是任務被清空了,那麼將defaultTaskIdx置爲-1
    • 不然以當前任務列表的第一項,做爲正在進行的任務
const 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>

  • emm這個命名感受有點奇怪,暫時沒想到更好的命名方式,後續在優化階段統一更新
/** * 更改默認記錄文件的位置 */
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],便可完成事務的添加,結束,自動寫入到文件

其中--stopoption是可選的,標識結束這個工做,將其寫入到文件之中去

首先註冊指令,而後從配置文件中取出相關的配置項目

其中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]
        
    })
複製代碼

首先作一些判斷,避免引起錯誤

  1. 判斷是否設置了寫出文件路徑
  2. 判斷是否設置了任務

若是沒有設置,拋出響應提示信息

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,表示使用查詢功能,打印當前事務的基本信息

  • 名稱
  • 開始時間
  • 已經持續了多久(暫時毫秒代替),TODO:後續優化

若是設置告終束的標誌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邏輯稍微有些複雜,今日時間實在有限,明日再詳細分析

其它

因爲天天空閒時間有限,本文就先到這

若是讀者還感受意猶未盡,敬請期待後續更新,或持續關注一下倉庫的狀態

歡迎評論區提需求,交流探討

本系列會不斷的更新迭代,直至產品初代完成

相關文章
相關標籤/搜索