Skip to content

Commit

Permalink
feature: 'clean' command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ji Sang Seo committed Apr 6, 2024
1 parent 2423ec5 commit b969847
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 5 deletions.
39 changes: 39 additions & 0 deletions cli/oh-my-task-clean.mjs
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));
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"omt-list": "cli/oh-my-task-list.mjs",
"omt-select": "cli/oh-my-task-select.mjs",
"omt-pr": "cli/oh-my-task-pr.mjs",
"omt-finish": "cli/oh-my-task-finish.mjs"
"omt-finish": "cli/oh-my-task-finish.mjs",
"omt-clean": "cli/oh-my-task-clean.mjs"
},
"scripts": {
"release": "node ./release.mjs",
Expand All @@ -24,7 +25,8 @@
"omt-select": "node ./cli/oh-my-task-select.mjs",
"omt-pr": "node ./cli/oh-my-task-pr.mjs",
"omt-finish": "node ./cli/oh-my-task-finish.mjs",
"omt-git": "node ./cli/oh-my-task-git.mjs"
"omt-git": "node ./cli/oh-my-task-git.mjs",
"omt-clean": "node ./cli/oh-my-task-clean.mjs"
},
"keywords": [
"oh-my-task",
Expand Down
34 changes: 34 additions & 0 deletions src/manifest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,40 @@ export class Manifest {
return fs.writeFile(this.historyPath, stringify(historyCollection));
}

async deleteHistory(taskKey) {
const fileContent = await fs
.readFile(this.historyPath, "utf8")
.catch((e) => "");

const historyCollection = parse(fileContent) ?? {};
const { project } = await this.getConfig();

if (!historyCollection[project.key]) {
historyCollection[project.key] = {};
}

if (historyCollection[project.key][taskKey]) {
delete historyCollection[project.key][taskKey];
}
return fs.writeFile(this.historyPath, stringify(historyCollection));
}

async overwriteHistroy(value) {
const fileContent = await fs
.readFile(this.historyPath, "utf8")
.catch((e) => "");

const historyCollection = parse(fileContent) ?? {};
const { project } = await this.getConfig();

if (!historyCollection[project.key]) {
historyCollection[project.key] = {};
}

historyCollection[project.key] = value;
return fs.writeFile(this.historyPath, stringify(historyCollection));
}

/**
* Initialize project history (history.yaml).
* This method is only used for initializing project.
Expand Down
10 changes: 7 additions & 3 deletions src/task/Task.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ export class Task {
const previousStateText = TASK_STATUS_TEXT[this.getProgress()];
const currentStateText = TASK_STATUS_TEXT[TASK_STATUS.WORKING];

console.log(`Switching to Task ${chalk.blueBright(this.key)}..`);
console.log(
`Switching to Task ${chalk.gray(this.title)}(${chalk.blueBright(
this.key
)})..`
);

this.setProgress(TASK_STATUS.WORKING);

console.log(
`Task ${chalk.blueBright(
`Task ${chalk.gray(this.title)}(${chalk.blueBright(
this.key
)} progress changed complete (${chalk.gray(
)}) progress changed complete (${chalk.gray(
previousStateText
)} -> ${chalk.green(currentStateText)})`
);
Expand Down
95 changes: 95 additions & 0 deletions src/task/clean.mjs
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;
}

0 comments on commit b969847

Please sign in to comment.