-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·76 lines (67 loc) · 1.98 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
#!/usr/bin/env node
'use strict'
const request = require('request')
const os = require('os')
const path = require('path')
const config = require(path.join(os.homedir(), '.google-tasks-rollover', 'config.json'))
const goAuth2 = require('google-oauth2')(config)
const scope = 'https://www.googleapis.com/auth/tasks'
const base = 'https://www.googleapis.com/tasks/v1/lists/' + config.task_list + '/tasks'
async function rollOver(scope) {
const authCode = await getAuthCode(scope)
const accessToken = await getTokensForAuthCode(authCode)
const tasks = await getOverdueTasks(accessToken) || []
console.log(`Found ${tasks.length} overdue tasks`)
return Promise.all(tasks.map(task => {
task.due = (new Date()).toISOString()
return requestP({
method: 'put',
headers: {
Authorization: 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
url: task.selfLink,
body: JSON.stringify(task)
})
}))
}
async function getOverdueTasks(token) {
const d = new Date()
const res = await requestP({
method: 'get',
url: base,
headers: { Authorization: 'Bearer ' + token },
qs: { dueMax: d.toISOString() }
})
return JSON.parse(res.body).items
}
async function getAuthCode(scope) {
return new Promise((resolve, reject) => {
goAuth2.getAuthCode(scope, (err, result) => {
if (err) return reject(err)
resolve(result)
})
})
}
async function getTokensForAuthCode(authCode) {
return new Promise((resolve, reject) => {
goAuth2.getTokensForAuthCode(authCode, (err, result) => {
if (err) return reject(err)
resolve(result.access_token)
})
})
}
async function requestP (opts) {
return new Promise((resolve, reject) => {
request(opts, (err, res) => {
if (err || (res.status && res.status !== 200)) reject(err || res)
resolve(res)
})
})
}
rollOver(scope)
.then(() => {
console.log("All overdue tasks moved to current date")
process.exit(0)
})
.catch(console.log)