forked from TehShrike/k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·234 lines (218 loc) · 5.74 KB
/
index.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env node
var router = require('lieutenant')
var api = require('./kanbanize_api.js')
var state = require('./state.js')
var subtask = require('./subtask.js')
var collapseArgs = require('./collapse_arguments.js')
var task = require('./task.js')
var editor = require('./editor.js')
var comment = require('./comment.js')
var displayTaskTable = require('./task_table.js')
var Table = require('cli-table')
function badRoute() {
console.log("k tasks")
console.log("k tasks all")
console.log("k work [task id]")
console.log("k details [OPTIONAL task id]")
console.log("k description [OPTIONAL task id]")
console.log("k subtasks [OPTIONAL task id]")
console.log("k add task [template name] [task title]")
console.log("k add subtask [subtask title]")
console.log("k add comment [OPTIONAL comment]")
console.log("k complete [subtask id]")
console.log("k move [left|right] [OPTIONAL taskid]")
console.log("k api [api function] [header1 value1 [header2 value2 ...]]")
console.log("k block [reason]")
console.log("k unblock [OPTIONAL task id]")
console.log("-----------")
console.log("k set key [api key]")
console.log("k set domain [domain name]")
console.log("k set board [board id]")
console.log("k set user [username]")
console.log("k set columns [column names]")
console.log("k set editor [path to editor]")
console.log("-----------")
console.log("k api [api function]")
}
function printArgs(name) {
return function() {
console.log(name, "called with", arguments)
}
}
function optionalTaskId(cb) {
return function(inputTaskId) {
if (parseInt(inputTaskId)) {
process.nextTick(cb.bind(null, inputTaskId))
} else {
taskGetter(cb)
}
}
}
function wrap(text, chars) {
var lines = text.split('\n')
return lines.map(function(line) {
var shortenedLines = []
while (line.length > chars) {
var trimAt = line.lastIndexOf(' ', chars)
if (trimAt == -1) {
trimAt = chars
}
var shortened = line.substr(0, trimAt)
line = line.substr(trimAt).trimLeft()
shortenedLines.push(shortened)
}
shortenedLines.push(line)
return shortenedLines
}).map(function joinWithNewline(ary) {
return ary.join('\n')
}).reduce(function(a, b) {
return a + '\n' + b
})
}
var taskSetter = state.setterFactory('taskId')
var taskGetter = state.getterFactory('taskId')
router({
tasks: {
all: displayTaskTable.all,
default: displayTaskTable
},
add: {
task: function addTask(template) {
var taskTitle = collapseArgs(arguments, 1)
api('create_new_task', {
title: taskTitle,
template: template
}, function(res) {
console.log('Created task', res.id)
})
},
subtask: subtask.add,
comment: function addComment() {
var newComment = collapseArgs(arguments)
if (newComment.length === 0) {
editor(comment.add)
} else {
comment.add(newComment)
}
}
},
set: {
editor: state.setterFactory('editor'),
key: state.setterFactory('key'),
domain: state.setterFactory('domain'),
board: state.setterFactory('board'),
user: state.setterFactory('user'),
columns: state.setterFactory('columns')
},
get: {
editor: state.getterFactory('editor'),
key: state.getterFactory('key'),
domain: state.getterFactory('domain'),
board: state.getterFactory('board'),
user: state.getterFactory('user'),
columns: state.getterFactory('columns')
},
clear: {
board: function() {
state.remove('boardColumns')
}
},
api: function(apiFunction) {
if (typeof apiFunction === 'string') {
var args = Array.prototype.slice.call(arguments)
args.shift()
var options = {}
var key
var value
while ((key = args.shift()) && (value = args.shift())) {
options[key] = value
}
api(apiFunction, options, function(response) {
console.log(require('util').inspect(response, { depth: null, colors: true }))
})
}
},
work: function(taskId) {
taskId = parseInt(taskId)
if (taskId > 0) {
taskSetter(taskId)
state.fetchAll(['board', 'user'], function(boardId, user) {
if (user && user !== 'all') {
api('edit_task', {
taskid: taskId,
boardid: boardId,
assignee: user
}, function() {
console.log('Task', taskId, 'has been assigned to you')
})
}
})
} else {
console.log("wat that's not a valid number c'mon")
}
},
subtasks: subtask.getAndDisplayTable,
complete: subtask.complete,
move: {
right: task.moveRight,
left: task.moveLeft
},
block: function() {
var reason = collapseArgs(arguments)
taskGetter(function(taskId) {
api('block_task', {
taskid: taskId,
event: 'block',
blockreason: reason
}, function() {
console.log("Task", taskId, "blocked:", reason)
})
})
},
unblock: optionalTaskId(function(taskId) {
api('block_task', {
taskid: taskId,
event: 'unblock'
}, function() {
console.log("Task", taskId, "unblocked")
})
}),
description: optionalTaskId(function(taskId) {
api('get_task_details', {
textformat: 'plain',
taskid: taskId
}, function(task) {
editor(task.description, function(newDescription) {
api('edit_task', {
taskid: taskId,
description: newDescription
})
})
})
}),
details: optionalTaskId(function(taskId) {
api('get_task_details', {
taskid: taskId,
textformat: 'plain',
history: 'yes',
event: 'comment'
}, function(task) {
var color = require('bash-color')
var bug = task.type === 'Bug'
var title = bug ? color.red(task.title) : task.title
var taskid = color.cyan(task.taskid)
var table = new Table({
head: [taskid, title],
colWidths: [11, 144]
})
if (task.description) {
table.push(['', wrap(task.description, 140)])
}
task.historydetails.reverse().forEach(function(comment) {
table.push([comment.author, wrap(comment.details, 140) ])
})
console.log(table.toString())
subtask.table(task)
})
})
}, badRoute)