-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
executable file
·58 lines (48 loc) · 1.43 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env -S node --experimental-strip-types --experimental-transform-types --no-warnings
import {
handleAddTask,
handleDeleteTask,
handleListTasks,
handleUpdateTask,
handleUpdateTaskStatus,
} from "./src/task-utils.ts";
import { Command, TaskStatus } from "./src/task.interface.ts";
// Map command names to handlers
const commandsListener: Record<Command, Function> = {
add: handleAddTask,
update: handleUpdateTask,
"mark-done": (taskId: string) =>
handleUpdateTaskStatus(taskId, TaskStatus.Done),
"mark-in-progress": (taskId: string) =>
handleUpdateTaskStatus(taskId, TaskStatus.InProgress),
delete: handleDeleteTask,
list: handleListTasks,
};
function main() {
try {
// Get the command
const command = process.argv[2];
// Get list of command arguments
const commandArgs = process.argv.slice(3);
if (!Object.values(Command).includes(command as Command)) {
throw new Error(
`Command is not valid, use one of the next: ${Object.values(
Command
).join(", ")}`
);
}
const commandHandler = commandsListener[command];
if (!commandHandler) {
throw new Error(`Handler action not defined for ${command} command.`);
}
console.debug("Executing command: ", {
command,
commandArgs,
});
// Execute command handler
commandHandler(...commandArgs);
} catch (error) {
console.error("Error: ", error.message);
}
}
main();