This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdid.js
478 lines (404 loc) · 11.9 KB
/
did.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/env node
var did = require('commander');
var request = require('request');
var clc = require('cli-color');
var BPromise = require('bluebird');
var openurl = require('openurl');
var path = require('path-extra');
var fs = require('fs');
var BASE_URL = 'https://idonethis.com/';
var API_VERSION = 'v0.1'
var API_URL = BASE_URL + 'api/' + API_VERSION + '/';
var apiToken = process.env.IDONETHIS_API_TOKEN;
var title = clc.red.underline;
var promptText = highlight = clc.red;
var row = clc.white;
var success = clc.green;
var warn = clc.yellow;
var error = clc.red.bold;
// load config file, if exists
var configFilePath = path.join(path.datadir('did'), 'didconfig.json');
var config = {};
try {
config = fs.readFileSync(configFilePath, {
encoding: 'utf8'
});
config = JSON.parse(config);
} catch (e) {
if ( !fs.existsSync(path.datadir('did')) ) {
fs.mkdirSync(path.datadir('did'));
}
config = {};
}
function limitCheck(val) {
val = parseInt(val);
if ( !isFinite(val) ) {
return 10;
}
return Math.floor(val);
}
function saveConfig() {
try {
fs.writeFileSync(configFilePath, JSON.stringify(config), {
encoding: 'utf8'
});
console.log(success('Config saved!'));
} catch(e){
console.log(error('Error saving your config!'), err);
process.exit(1);
}
}
function openURL(url) {
openurl.open(url);
}
function setup() {
if ( did.apiToken ) {
apiToken = did.apiToken;
} else if ( config.apiToken ) {
apiToken = config.apiToken;
}
if ( !apiToken ) {
console.error('Missing API Token - can be set on your env as "IDONETHIS_API_TOKEN" or passed in with -t or --apiToken');
process.exit(1);
}
return request.defaults({
headers: {
Authorization: 'TOKEN ' + apiToken
},
json: true
});
}
function getTeams() {
return new BPromise(function(resolve) {
var req = setup();
req({
method: 'GET',
url: API_URL + 'teams/'
}, function(err, resp, body) {
if ( err ) {
console.error('ERR: ' + err.code);
process.exit(1);
}
if ( resp.statusCode !== 200 ) {
console.error('Request Authorization failed! check your token!');
process.exit(1);
}
if ( !body.ok ) {
console.error('Something went wrong!');
console.log(body);
process.exit(1);
}
if ( !body.results.length ) {
console.log(warn('You are not on any teams'));
process.exit(0);
}
resolve(body.results);
});
});
}
function teams() {
getTeams()
.then(function(teams) {
console.log(title('Teams:'));
teams.forEach(function(team){
console.log(row(team.name));
});
process.exit(0);
});
}
function sendDone(team, task) {
return new BPromise(function(resolve) {
var req = setup();
if ( did.goal ) {
task = '[] ' + task;
}
req({
method: 'POST',
url: API_URL + 'dones/',
body: {
raw_text: task,
team: team
}
}, function(err, resp, body) {
if ( err ) {
console.error('ERR: ' + err.code);
process.exit(1);
}
if ( resp.statusCode !== 201 ) {
console.error('Request Authorization failed! check your token!');
process.exit(1);
}
if ( !body.ok ) {
console.error('Something went wrong!');
console.log(body);
process.exit(1);
}
resolve(body.result);
});
})
}
function prompt(msg){
return new BPromise(function(resolve) {
process.stdout.write(msg);
process.stdin.setEncoding('utf8');
process.stdin.once('data', function(val){
resolve(val.trim());
}).resume();
});
}
function interactiveDone(check, worker) {
var resolver = BPromise.defer();
var loop = function() {
if ( !check() ) {
return resolver.resolve();
}
return BPromise.cast(worker())
.then(loop)
.catch(resolver.reject);
};
process.nextTick(loop);
return resolver.promise;
}
function interactiveMode(team) {
var done;
interactiveDone(function() {
return done !== 'done';
}, function() {
return prompt(promptText('Enter done for ' + team + ' (done to end): '))
.then(function(input) {
done = input;
})
.then(function() {
if (done !== 'done' && done !== '') {
return sendDone(team, done);
}
if ( done === '' ) {
console.log(warn('You didn\'t enter anything!\n'));
}
return BPromise.resolve;
})
.then(function(newDone) {
if (newDone.owner) {
console.log(success.underline('Your done was created!'));
console.log(success('created for ' + newDone.owner + ' in the team "' + team + '"'));
console.log(success('Text: ' + newDone.raw_text + '\n'));
}
return BPromise.resolve;
});
})
.then(function() {
process.exit();
});
}
function createDone(team, task) {
var hasDefaults = !task && config.defaultTeam;
if (hasDefaults || (did.interactive && hasDefaults)) {
task = team;
team = config.defaultTeam;
}
if ((!task && !team) || (!task && !did.interactive)) {
console.log(error('You must provide a team and a task'));
process.exit(1);
}
getTeams()
.then(function(teams) {
for (var i = 0; i < teams.length; i++) {
if ( teams[i].name === team ) {
if ( did.interactive ) {
return interactiveMode(teams[i].short_name);
}
return sendDone(teams[i].short_name, task)
.then(function(done){
console.log(success.underline('Your ' + (did.goal ? 'goal' : 'done') + ' was created!'));
console.log(success('created for ' + done.owner + ' in the team "' + team + '"'));
console.log(success('Text: ' + done.raw_text));
});
}
}
console.log(error('You are not a part of the team: ' + team));
process.exit(1);
});
}
function open(team) {
if ( !team ) {
openURL(BASE_URL + 'home/');
process.exit();
}
getTeams()
.then(function(teams) {
for (var i = 0; i < teams.length; i++) {
if ( teams[i].name === team ) {
openURL(BASE_URL + 'cal/' + teams[i].short_name);
process.exit();
}
}
console.log(error('You are not a part of the team: ' + team));
process.exit(1);
});
}
function updateConfig() {
if (!did.defaultTeam && !did.apiToken) {
console.log( JSON.stringify( config, null, 2 ) );
console.log(success('Set new config values with --default-team and --api-token'));
process.exit();
}
if ( did.defaultTeam ) {
config.defaultTeam = did.defaultTeam;
}
if ( did.apiToken ) {
config.apiToken = did.apiToken;
}
saveConfig();
}
function collectTags(val, tags) {
if (val.indexOf(',') >= 0) {
val.split(',').forEach(function(tag) {
tags.push(val);
});
} else {
tags.push(val);
}
return tags;
}
function buildFilter(key, value) {
return key + '=' + value + '&';
}
function buildFilterString() {
return new BPromise(function(resolve) {
var requestFilters = '?';
if (did.team) {
requestFilters += buildFilter('team', did.team);
}
if (did.owner) {
requestFilters += buildFilter('owner', did.owner);
}
if (did.doneDate) {
requestFilters += buildFilter('done_date', did.doneDate);
}
if (did.doneAfter) {
requestFilters += buildFilter('done_date_after', did.doneAfter);
}
if (did.doneBefore) {
requestFilters += buildFilter('done_date_before', did.doneBefore);
}
if (did.tag && did.tag.length) {
requestFilters += buildFilter('tags', did.tag.join(','));
}
if (did.sortBy) {
requestFilters += buildFilter('order_by', did.sortBy);
}
if (did.limit) {
requestFilters += buildFilter('page_size', did.limit);
}
if (did.page) {
requestFilters += buildFilter('page', did.page);
}
resolve(requestFilters.slice(0, requestFilters.length - 1));
});
}
function fetchDones(filterString) {
return new BPromise(function(resolve) {
var req = setup();
req({
method: 'GET',
url: API_URL + 'dones/' + filterString
}, function(err, resp, body) {
if (err) {
console.log(error('ERR: ' + err.code));
process.exit(1);
}
if (resp.statusCode !== 200) {
if (resp.statusCode === 400) {
console.log(error('Invalid request data!'));
console.log(body.errors);
} else if (resp.statusCode === 404) {
console.log(warn('Page not found'));
} else {
console.log(error('Request failed! check your token!'));
}
process.exit(1);
}
if ( !body.ok ) {
console.log(error('Something went wrong!'));
console.log(body);
process.exit(1);
}
resolve(body);
});
});
}
function outputDones(body) {
if ( !body.results || !body.results.length ) {
console.log(warn('No dones found!'));
return;
}
var page = did.page || 1;
var limit = did.limit || 10;
console.log(success('Found ' + body.count + ' dones'));
console.log(success('Showing page ' + page + ' of ' + Math.ceil(body.count / limit) + '\n'));
body.results.forEach(function(done) {
console.log('On ' + done.done_date + ', ' + highlight(done.owner) + ' did:');
console.log(unescape(done.raw_text) + '\n');
});
}
function listDones() {
if ( did.team ) {
getTeams()
.then(function(teams) {
for (var i = 0; i < teams.length; i++) {
if ( teams[i].name === did.team ) {
did.team = teams[i].short_name;
return buildFilterString();
}
}
console.log(error('You are not a part of the team: ' + team));
process.exit(1);
})
.then(fetchDones)
.then(outputDones);
} else {
buildFilterString()
.then(fetchDones)
.then(outputDones);
}
}
did
.version('v0.5.0')
.option('-t, --api-token <token>', 'Specify or save an API Token')
.option('-g, --goal', 'Make this task a goal')
.option('-i, --interactive', 'interactive done entry mode')
.option('-m, --team <team>', 'Specify a team')
.option('-o, --owner <owner>', 'Specify an owner')
.option('-d, --done-date <date>', 'Specify a date that a done must be equal to. Format is \'YYYY-MM-DD\'. The special values \'today\' and \'yesterday\' are also valid.')
.option('-a, --done-after <date>', 'Specify a date that dones must have been created on or after. Format is \'YYYY-MM-DD\'.')
.option('-b, --done-before <date>', 'Specify a date that dones must have been created on or before. Format is \'YYYY-MM-DD\'.')
.option('-s, --sort-by <sortby>', 'Specify the order dones are returned in. can be: \'created\', \'done_date\', \'-created\', or \'-done_date\'. The negative sign indicates a decreasing order. default is done_date')
.option('-l, --limit <limit>', 'Specify a number of dones to fetch. default is 10, max is 100.', limitCheck)
.option('-p, --page <page>', 'Specify a page number to fetch if there are more dones than could be fetched.')
.option('--tag <tag>', 'Specify a tag. can be repeated to add multiple tags.', collectTags, [])
.option('--default-team <team>', 'Set a default team in your config');
did
.command('do [team] [task]')
.description('Create a done for the specified team')
.action(createDone);
did
.command('teams')
.description('List your teams')
.action(teams);
did
.command('open [team]')
.description('Open iDoneThis in your default browser, optionally providing the team to view')
.action(open)
did
.command('config')
.description('Configure did using the --default-team and --api-token options')
.action(updateConfig);
did
.command('dones')
.description('show dones, which can be filtered using options')
.action(listDones);
did
.command('* [team] [task]')
.description('Shorthand for \'do\'. If you set a default team, you can just type in your done (quoted)')
.action(createDone);
did.parse(process.argv);