forked from Nevermole/btt-toggl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (152 loc) · 9.35 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
#!/usr/bin/env node
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const moment = require("moment");
const fs = __importStar(require("fs"));
const token = __importStar(require("./token.json"));
const apiKey = token.api_token;
const currentFileName = __dirname + '/current.json';
function getCurrentTimeEntry() {
return __awaiter(this, void 0, void 0, function* () {
var config = {
method: 'GET',
url: 'https://www.toggl.com/api/v8/time_entries/current',
auth: {
username: apiKey,
password: 'api_token'
}
};
const result = yield axios_1.default(config);
if (!result.data && !result.data.data) {
return null;
}
else {
fs.writeFileSync(currentFileName, JSON.stringify(result.data.data, null, 2));
return result.data.data;
}
});
}
function startATimeEntry() {
return __awaiter(this, void 0, void 0, function* () {
const current = JSON.parse(fs.readFileSync(currentFileName).toString());
const entry = { 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 = {
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 = yield axios_1.default(config);
if (!result.data && !result.data.data) {
return null;
}
else {
return result.data.data;
}
});
}
function stopATimeEntry(current) {
return __awaiter(this, void 0, void 0, function* () {
var config = {
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 = yield axios_1.default(config);
if (!result.data && !result.data.data) {
return null;
}
else {
return result.data.data;
}
});
}
function generateStatus(entry = undefined) {
return __awaiter(this, void 0, void 0, function* () {
let current;
if (entry == undefined) {
current = yield 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;
});
}
function toggle() {
return __awaiter(this, void 0, void 0, function* () {
const current = yield getCurrentTimeEntry();
if (current) {
yield stopATimeEntry(current);
return yield generateStatus();
}
else {
const timeEntry = yield startATimeEntry();
return yield 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));
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