-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate-task.mjs
More file actions
97 lines (80 loc) · 2.11 KB
/
Copy pathcreate-task.mjs
File metadata and controls
97 lines (80 loc) · 2.11 KB
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
import { assertServerId, printError, printJson, todoistRequest } from './_todoist-client.mjs';
function parseInteger(value) {
if (value === undefined) return undefined;
const parsed = Number.parseInt(String(value), 10);
return Number.isNaN(parsed) ? null : parsed;
}
function parseArgs(argv) {
const args = {};
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i];
const next = argv[i + 1];
if (arg === '--content' && next) {
args.content = next;
i += 1;
} else if (arg === '--description' && next) {
args.description = next;
i += 1;
} else if (arg === '--project-id' && next) {
args.project_id = next;
i += 1;
} else if (arg === '--section-id' && next) {
args.section_id = next;
i += 1;
} else if (arg === '--priority' && next) {
args.priority = parseInteger(next);
i += 1;
} else if (arg === '--deadline-date' && next) {
args.deadline_date = next;
i += 1;
} else if (arg === '--due-string' && next) {
args.due_string = next;
i += 1;
}
}
return args;
}
const options = parseArgs(process.argv.slice(2));
if (!options.content) {
printError(new Error('missing_content'));
process.exit(2);
}
if (options.priority === null) {
printError(new Error('priority_must_be_integer'));
process.exit(2);
}
try {
const body = {
content: options.content
};
if (options.description) {
body.description = options.description;
}
if (options.project_id) {
body.project_id = assertServerId(options.project_id, 'project_id');
}
if (options.section_id) {
body.section_id = assertServerId(options.section_id, 'section_id');
}
if (options.priority !== undefined) {
body.priority = options.priority;
}
if (options.deadline_date) {
body.deadline = { date: options.deadline_date };
}
if (options.due_string) {
body.due = { string: options.due_string };
}
const data = await todoistRequest({
method: 'POST',
path: '/tasks',
body
});
printJson({
ok: true,
task: data
});
} catch (error) {
printError(error);
process.exit(1);
}