-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ji Sang Seo
committed
Apr 6, 2024
1 parent
2423ec5
commit b969847
Showing
5 changed files
with
179 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env node | ||
|
||
import { Command } from "commander"; | ||
import { clean } from "../src/task/clean.mjs"; | ||
|
||
async function cleanCommand() { | ||
const program = new Command(); | ||
program | ||
.name("oh-my-task-clean") | ||
.description("Clean project task history from global history") | ||
.option("-c, --complete", "Clean all progresses with state of complete") | ||
.option("-i, --idle", "Clean all progresses with state of idle"); | ||
|
||
program.parse(); | ||
|
||
const options = program.opts(); | ||
|
||
const cleanOptions = { | ||
selection: true, | ||
complete: false, | ||
idle: false, | ||
}; | ||
|
||
if (options.idle) { | ||
cleanOptions.idle = true; | ||
} | ||
|
||
if (options.complete) { | ||
cleanOptions.complete = true; | ||
} | ||
|
||
if (cleanOptions.idle || cleanOptions.complete) { | ||
cleanOptions.selection = false; | ||
} | ||
|
||
await clean(cleanOptions); | ||
} | ||
|
||
await cleanCommand().catch((error) => console.error(error)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import * as context from "../context.mjs"; | ||
import { TASK_STATUS, TASK_STATUS_TEXT } from "../status.mjs"; | ||
import * as input from "../input.mjs"; | ||
import chalk from "chalk"; | ||
|
||
export async function clean( | ||
cleanOptions = { | ||
selection: true, | ||
complete: false, | ||
idle: false, | ||
} | ||
) { | ||
let taskCollection = await context.manifest.getHistory(); | ||
|
||
if (cleanOptions.selection) { | ||
const taskList = Object.keys(taskCollection).map((taskKey) => { | ||
return { | ||
name: taskCollection[taskKey].title, | ||
value: taskKey, | ||
}; | ||
}); | ||
|
||
const selectedTask = await input.select("Select Task to clean:", taskList, [ | ||
"name", | ||
]); | ||
const taskTitle = taskCollection[selectedTask].title; | ||
|
||
await context.manifest.deleteHistory(selectedTask); | ||
|
||
console.log( | ||
`Successfully cleaned task:: ${chalk.gray(taskTitle)}(${chalk.greenBright( | ||
selectedTask | ||
)})` | ||
); | ||
return; | ||
} | ||
|
||
if (cleanOptions.complete) { | ||
const cleanResult = await batchClean(taskCollection, TASK_STATUS.COMPLETE); | ||
|
||
if (cleanResult.success) { | ||
console.log( | ||
`Total ${chalk.greenBright( | ||
cleanResult.total | ||
)} task with status ${chalk.bgBlueBright( | ||
TASK_STATUS_TEXT[TASK_STATUS.COMPLETE] | ||
)} has been deleted` | ||
); | ||
} else { | ||
throw new Error("Error occured while running batch clean"); | ||
} | ||
} | ||
|
||
if (cleanOptions.idle) { | ||
const cleanResult = await batchClean(taskCollection, TASK_STATUS.IDLE); | ||
|
||
if (cleanResult.success) { | ||
console.log( | ||
`Total ${chalk.greenBright( | ||
cleanResult.total | ||
)} task with status ${chalk.bgBlueBright( | ||
TASK_STATUS_TEXT[TASK_STATUS.IDLE] | ||
)} has been deleted` | ||
); | ||
} else { | ||
throw new Error("Error occured while running batch clean"); | ||
} | ||
} | ||
} | ||
|
||
async function batchClean(taskCollection, statusType) { | ||
const reciept = { | ||
success: true, | ||
total: 0, | ||
status: statusType, | ||
}; | ||
|
||
const updatedTaskCollection = { ...taskCollection }; | ||
|
||
const targets = Object.keys(updatedTaskCollection).filter( | ||
(taskKey) => updatedTaskCollection[taskKey].status === statusType | ||
); | ||
console.log(targets, updatedTaskCollection); | ||
|
||
reciept.total = targets.length; | ||
|
||
targets.forEach((target) => { | ||
delete updatedTaskCollection[target]; | ||
}); | ||
|
||
await context.manifest.overwriteHistroy(updatedTaskCollection); | ||
reciept.success = true; | ||
|
||
return reciept; | ||
} |