Make a TIME Management Tool for the CLI (part 7)
This is the 8th day of my participation in the August More Text Challenge. For details, see “August More Text Challenge”.
preface
The previous article focused on the development of instructions related to task management for data export of various dates
This article will cover instructions:
- Delete a task:
timec task -d [name]
- I added a
option
parameter-d
, indicating that the task is to be removed
- I added a
- Set record default output location:
timec upPath <recordFilepath>
- All subsequent transactions added/completed by user instructions are recorded here
- Start/finish a specific transaction:
timec thing -s [name]
- Among them
-s
Identifier. This parameter is optional for whether to temporarily store the task state
- Among them
This effect
TODO: Make up a picture
The function development
Delete task command
This functionality simply requires adding an optional command to the task management command developed yesterday
- The first to use
option
Methods registration--del
Optional parameters - By deconstructing from
cmdObj
Get on thedel
The value of the
commander.command("task [name]")
.option('-d, --del'.'Delete task or thing')
.action((name, cmdObj) = > {
const { del } = cmdObj
/ /... code
}
Copy the code
If del is true, the del option is set
- Remove this item from the task array using the splice method
- Update the configuration file after the deletion
- If the mission is cleared, then
defaultTaskIdx
Set to 1 - Otherwise, use the first item on the current task list as the ongoing task
- If the mission is cleared, then
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))
Copy the code
Delete the task function, it is easy to do
Set the default output location of the record
Functional background
In the absence of the introduction of instructions to record the time taken to do the task
You need to manually add these records to the records file
The expectation is that the transaction time will be automatically calculated and the record automatically written to the target file by subsequent instructions
If the address of the object file is spelled in the parameter, it will be troublesome every time you use it
To avoid this little hassle, save the path of the target file to the configuration file
Instruction development
The storage location corresponds to the recordFilepath property in the configuration file
{
"recordFilepath":"/Users/sugar/Documents/fe-project/time-control/test.md"
}
Copy the code
Register with commander.command and set it to upPath
- The naming of EMM feels a little strange. I have not thought of a better naming method for the time being, and will be updated uniformly in the optimization stage later
/** * change the location of the default record file */
commander.command("upPath <recordFilepath>")
// .alias('urp')
.description('update config recordFilepath')
.action((recordFilePath) = > {
/ /... code
})
Copy the code
The absolute path of the output file is obtained by the command CWD and the relative path of the incoming file recordFilePath
This absolute path is then assigned to the recordFilePath property of the configuration file
The fs.existsSync method determines whether the file exists, and if it does not, it is created automatically
Finally, update the configuration file via fs.writeFileSync
const config = require(configPath)
const fullPath = path.resolve(cwd, recordFilePath)
config.recordFilepath = fullPath
if(! existsSync(fullPath)) {// Automatically create an empty file
createFile(fullPath, ' '.false)
}
writeFileSync(configPath, JSON.stringify(config))
console.log(Set recordFilePath success:, fullPath);
Copy the code
This completes the development of instructions for setting the path to the output file
Transaction management instruction development
It is expected that a simple timec thing -s [name] will complete the addition of the transaction, and then the transaction will be automatically written to the file
Where — stopOption is optional, it signals the end of the work and writes it to a file
The directive is first registered, and then the relevant configuration items are retrieved from the configuration file
The structure of the thing attribute is as follows
{
"name":"abc"."startTime":"The 2021-08-08 22:18:19"
}
Copy the code
Store the name of the current ongoing transaction and the transaction start time, respectively
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]
})
Copy the code
Make some judgments first to avoid causing errors
- Determines whether the write file path is set
- Determines whether the task is set
If not, a response prompt is thrown
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
}
Copy the code
If no event name is passed in, the query function is used to print the basic information about the current transaction
- The name of the
- The start time
- How long has it been going on (temporarily milliseconds instead), TODO: follow-up optimization
If the stop flag is set, the transaction record is written to the file and the configuration file is updated
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: hour, minute, second
console.log(`-duration: The ${Date.now() - s} mss`);
console.log('-- -- -- -- -- -);
return
}
Copy the code
The actual writeRecord logic is a little more complicated, but time is limited today, so we will analyze it in detail tomorrow
other
Because of the limited free time each day, this article will stop there
If you don’t have enough, stay tuned for updates, or keep an eye on the status of the warehouse
Welcome to comment section to raise demand, exchange discussion
This series will continue to be updated and iterated until the first generation of the product is completed
- The warehouse address