-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
97 lines (84 loc) · 2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
import * as fs from 'fs';
import * as path from 'path';
import * as program from 'commander';
import DataBase from './lib/data_base';
import Command from './lib/cli_command';
import TodoList from './lib/todo_list';
// instantiation the db
const dataBase = new DataBase();
// read origin data
const originData = dataBase.readData();
// instantiation todolist
const todoList = new TodoList(originData);
// instantiation command
const command = new Command(todoList);
let pkgConfig = {
version: 'beta',
}
try {
pkgConfig = JSON.parse(fs.readFileSync(path.join(__dirname, './package.json'), 'utf-8'));
} catch(e) {
console.error(e);
}
program.version(pkgConfig.version).usage('. Make your own todolist in cli .');
// set default command
if (!process.argv.slice(2).length) {
command.listPending()
}
// cli add todo item
program
.command('add <content>')
.alias('a')
.description('Add new todo item to list')
.action(function(content, cmd){
content = process.argv.slice(3).join(' ');
command.add(content);
})
// remove todo item
program
.command('remove [id]')
.alias('r')
.description('Remove todo item')
.option('-a --all', 'Clear all todo items')
.action(function(id, options) {
if (options.all) {
command.removeAll();
return;
}
id = parseInt(id);
command.remove(id);
})
// list todolist
program
.command('ls')
.description('List all todolist include checked item')
.action(function(cli, options) {
command.listAll();
})
// check todo item
program
.command('check <id>')
.alias('c')
.description('Check todo item as completed')
.action(id => {
id = parseInt(id);
command.check(id);
})
// uncheck todo item
program
.command('uncheck <id>')
.alias('uc')
.description('Uncheck todo item as pending')
.action(id => {
id = parseInt(id);
command.uncheck(id);
})
// resort todo list
program
.command('resort')
.description('Resort todo list id')
.action(() => {
command.resort();
})
program.parse(process.argv);