forked from Nevermole/btt-toggl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
139 lines (127 loc) · 3.55 KB
/
index.ts
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
#!/usr/bin/env node
import axios, { AxiosRequestConfig } from 'axios'
import moment = require('moment')
import * as fs from 'fs'
import * as token from './token.json'
const apiKey = token.api_token
const currentFileName = __dirname +'/current.json'
interface TimeEntry {
duration: number
description: string
id: string
pid: string
wid: string
}
async function getCurrentTimeEntry(): Promise<TimeEntry> {
var config: AxiosRequestConfig = {
method: 'GET',
url: 'https://www.toggl.com/api/v8/time_entries/current',
auth: {
username: apiKey,
password: 'api_token'
}
}
const result: any = await axios(config)
if (!result.data && !result.data.data) {
return null
} else {
fs.writeFileSync(currentFileName, JSON.stringify(result.data.data, null, 2))
return result.data.data
}
}
async function startATimeEntry(): Promise<TimeEntry> {
const current: TimeEntry = JSON.parse(fs.readFileSync(currentFileName).toString())
const entry: any = {time_entry: {
tags: ['automated', 'touchbar'],
created_with: 'curl',
billable: true
}
}
if (current) {
entry.time_entry.pid = current.pid
entry.time_entry.wid = current.wid
entry.time_entry.description = current.description
}
var config: AxiosRequestConfig = {
method: 'POST',
url: 'https://www.toggl.com/api/v8/time_entries/start',
auth: {
username: apiKey,
password: 'api_token'
},
headers: {
'Content-encoding': 'application/json'
},
data: entry
}
const result: any = await axios(config)
if (!result.data && !result.data.data) {
return null
} else {
return result.data.data
}
}
async function stopATimeEntry(current: TimeEntry): Promise<boolean> {
var config: AxiosRequestConfig = {
method: 'PUT',
url: `https://www.toggl.com/api/v8/time_entries/${current.id}/stop`,
auth: {
username: apiKey,
password: 'api_token'
},
headers: {
'Content-encoding': 'application/json'
}
}
const result: any = await axios(config)
if (!result.data && !result.data.data) {
return null
} else {
return result.data.data
}
}
async function generateStatus(entry: TimeEntry? = undefined) {
let current: TimeEntry
if (entry == undefined) {
current = await getCurrentTimeEntry()
} else {
current = entry
}
let statusText = '--:--'
let icon = __dirname + '/inactive.png'
if (current) {
icon = __dirname + '/active.png'
const duration = moment.duration(
new Date().valueOf() / 1000 + current.duration,
'seconds'
)
statusText = `${moment(duration._data).format("H:mm")} ${
current.description
}`
}
const status = {
text: statusText,
background_color: '0, 0, 0, 0',
icon_path: icon
}
return status
}
async function toggle() {
const current = await getCurrentTimeEntry()
if (current) {
await stopATimeEntry(current)
return await generateStatus()
} else {
const timeEntry = await startATimeEntry()
return await generateStatus(timeEntry)
}
}
if (process.argv.indexOf('toggle') !== -1) {
toggle().then(status => {
console.log(JSON.stringify(status))
})
} else {
generateStatus().then(status => {
console.log(JSON.stringify(status))
})
}