-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·93 lines (80 loc) · 3.97 KB
/
index.js
File metadata and controls
executable file
·93 lines (80 loc) · 3.97 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
#! /usr/bin/env node
import { Command } from 'commander'
import fs from 'fs'
import os from 'os'
import capi from 'axios'
capi.defaults.baseURL = 'https://api.clickup.com/api/v2/'
const err = (e) => console.log(e.response.status, e.response?.data || e)
const log = (r) => console.log(config.debug ? (r.data || r) : (r.data?.id || r.id))
const read = (f) => fs.readFileSync(f,'utf8').replace(/\`/g,'\`')
const capp = new Command()
let config = {}
function merge(opts, extra={}, fileProp) {
if (opts.assignees) opts.assignees = opts.assignees.map(_ => config.users[_] || _)
if (config.users[opts.assignee]) opts.assignee = config.users[opts.assignee]
if (opts.lists) opts.lists = opts.lists.map(_ => config.lists[_] || _)
if (config.lists[opts.list]) opts.list = config.lists[opts.list]
if (fileProp && opts.file) opts[fileProp] = read(opts.file)
return Object.assign({}, config.defaults, extra, opts, opts.json && JSON.parse(opts.json))
}
const taskCmd = (app, name, desc) => app.command(name).description(desc)
.option('-f, --file <filePath>', 'Markdown Description from file')
.option('-t, --parent <task_id>', 'Parent Task Id')
.option('-i, --priority <priority>', 'Task Priority (1-5)')
.option('-a, --assignees <user...>', 'Comma seperated user initals or ids')
.option('-e, --time_estimate <estimate>', 'Time Estimate (ms)')
.option('-s, --status <status>', 'Task Status')
.option('-p, --points <points>', 'Sprint Points')
.option('-n, --name <name>', 'Task Name')
.option('-j, --json <json>', 'Custom Fields as JSON')
.option('-d, --description <description>', 'Task Description')
.option('-l, --list <list...>', 'comma seperated lists names or ids')
capp.name('clickup').description('clickup cli (v1.0.18)')
.option('-v, --verbose').option('-c, --config', 'Configuration File', os.homedir() + '/.clickup')
.hook('preAction', (cmd) => {
config = Object.assign({users:{}, lists:{}}, JSON.parse(read(cmd.opts().config)))
capi.defaults.headers.common['Authorization'] = config.auth
config.debug = cmd.opts().verbose
if (config.debug) console.log('CONFIG:', config)
})
taskCmd(capp, 'create', 'Create task')
.argument('<name>', 'Task Name')
.action((name, opts) => {
let data = merge(opts, { name: name }, 'markdown_description')
capi.post('list/'+ (opts.list || config.defaults.list) +'/task', data).then(log).catch(err)
})
taskCmd(capp, 'update', 'Update Task')
.argument('<task_id>', 'Task Id')
.argument('[name]', 'Task Name')
.action((tid, name, opts) => {
let data = merge(opts, { name: name }, 'markdown_description')
capi.put('task/'+tid, data).then(log).catch(err)
})
capp.command('delete').description('Delete task')
.argument('<task_id>', 'Task Id')
.action((tid, opts) => capi.delete('task/'+tid).then(log).catch(err))
capp.command('comment').description('add comment')
.argument('<task_id>', 'Task Id')
.argument('[message]', 'Comment Text')
.option('-f, --file <filePath>', 'Read from file')
.option('-n, --notify_all', 'Notify all')
.option('-a, --assignee <user_id>', 'Assign to user')
.action((tid, text, opts) => {
let data = merge(opts, { comment_text: text }, 'comment_text')
capi.post('task/'+tid+'/comment', data).then(log).catch(err)
})
capp.command('check').description('add checklist')
.argument('<task_id>', 'Task Id')
.argument('[item]', 'Checklist Item')
.option('-f, --file <filePath>', 'List of Items')
.option('-n, --name <name>', 'Checklist Name', 'Checklist')
.action(async (tid, item, opts) => {
try {
let cid = (await capi.get('task/'+tid)).data.checklists.find(_ => _.name=opts.name)?.id
cid = cid || (await capi.post('task/'+tid+'/checklist', {name: opts.name})).data.checklist.id
if (opts.file) for (let i of read(opts.file).split(/\n/)) {
capi.post('checklist/'+cid+'/checklist_item', {name: i}).then(log).catch(err)
} else capi.post('checklist/'+cid+'/checklist_item', {name: item}).then(log).catch(err)
} catch (e) { err(e) }
})
capp.parse()