This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·106 lines (101 loc) · 2.56 KB
/
cli.js
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
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
"use strict";
const { trash, createApi, createLogger, render, ctime } = require('./lib');
require('yargs')
.command(
'forget [hash...]',
'Delete overdue task',
(yargs) => {
yargs.positional('hash', { describe: 'task hash' });
},
forget,
)
.command(
'list',
'List active tasks',
(yargs) => {},
list,
)
.command(
'remember',
'List overdue tasks',
(yargs) => {},
remember,
)
.option('all', {
alias: 'a',
type: 'boolean',
description: 'All of the overdue task',
})
.option('ctime', {
type: 'string',
description: 'Filter by created time'
})
.option('log', {
type: 'string',
description: 'Path to log directory',
})
.option('debug', {
type: 'boolean',
description: 'Debug mode',
})
.option('token', {
type: 'string',
description: 'Todoist api token',
})
.strict()
.argv;
async function forget(argv) {
global.debug = argv.debug;
const logger = createLogger(argv.log);
const api = createApi(argv.token || process.env.TODOIST_API_TOKEN);
const today = new Date();
try {
const tasks = (await api.read()).filter(task => !argv.ctime || ctime(task, argv.ctime));
const targets = argv.all ? tasks : argv.hash.map(hash => {
const reg = new RegExp(hash + '.+');
const matched = tasks.filter(task => argv.all || task.hash.match(reg));
if (matched.length !== 1) throw new Error(`ambiguous argument: '${hash}'`);
return matched[0];
});
targets.forEach(task => {
console.info(render.forget(task));
api.delete(task);
trash.add(task);
logger.info('Deleted', task);
});
} catch (e) {
console.error(e);
logger.error(e);
}
}
async function list(argv) {
global.debug = argv.debug;
const logger = createLogger(argv.log);
const api = createApi(argv.token || process.env.TODOIST_API_TOKEN);
try {
const targets = (await api.read()).filter(task => !argv.ctime || ctime(task, argv.ctime));
targets.forEach(target => {
console.info(render.list(target));
});
} catch (e) {
console.error(e);
logger.error(e);
}
}
async function remember(argv) {
global.debug = argv.debug;
const logger = createLogger(argv.log);
const api = createApi(argv.token || process.env.TODOIST_API_TOKEN);
try {
const targets = (await trash.read()).filter(task => {
return !argv.ctime || ctime(task, argv.ctime);
});
targets.forEach(task => {
console.info(render.remember(task));
});
} catch(e) {
console.error(e);
logger.error(e);
}
}