-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·118 lines (103 loc) · 3.16 KB
/
main.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
#!/usr/bin/env node
let a_time_crisis;
let debug = require("debug")("time-crisis");
let moment = require("moment");
let TimeCrisis = require("./time-crisis");
let yargs = require("yargs");
let progress = require("progress");
const VERSION = require("./package.json").version;
let args = yargs
// version
.version(VERSION)
// usage
.usage("-t <token> [options]")
// token
.alias("t", "token")
.describe("t", "Toggl API token")
// output
.alias("o", "output")
.describe("o", "output to a csv file")
// update
.alias("u", "update")
.boolean("u")
.describe("u", "save rounded entries to Toggl")
.argv;
if (args.token) {
a_time_crisis = new TimeCrisis(args.token);
a_time_crisis
.getTimeEntries()
.then(_processTimeEntries)
.catch(function (error) {
console.error(error);
});
} else {
yargs.showHelp("log");
}
async function _processTimeEntries(entries) {
debug(`Got ${entries.length} time entries`);
let timesheet_entries = new Map();
let bar = new progress("[:bar] :percent", {total: entries.length});
debug("Begin parsing time entries");
let timesheet_promises = entries.map(async function (entry) {
// a negative duration means an entry is in progress; do not touch these
if (Math.sign(entry.duration) !== -1) {
let entry_in_hours = TimeCrisis.convertSecondsToHours(entry.duration);
/**
* client data has to be extracted in a funny daisy chain way:
* [entry id] -> [project id] -> [client data]
*/
let projectData;
let clientData;
try {
projectData = await a_time_crisis.getProjectData(entry.pid);
clientData = await a_time_crisis.getClientData(projectData.cid);
} catch (e) {
// a project was not assigned to this entry; default name to empty
projectData = {name: ""};
clientData = {name: ""};
}
debug(`Processing entry: ${entry.description}`);
// create timesheet entry if one does not exist yet
if (!timesheet_entries.has(entry.description)) {
timesheet_entries.set(entry.description, {
clientName: clientData.name,
description: entry.description,
daysOfWeek: new Array(7).fill(0)
});
}
let saved_entry = timesheet_entries.get(entry.description);
saved_entry.daysOfWeek[
moment(entry.start).day()
] += TimeCrisis.roundHourToQuarterHour(entry_in_hours);
timesheet_entries.set(entry.description, saved_entry);
if (args.update) {
/**
* do not bother trying to save something that's already rounded
* (we'll get rate limited if we keep on trying)
*/
if (!TimeCrisis.hoursAreRounded(entry_in_hours)) {
debug(`Updating entry: ${entry.description}`);
await a_time_crisis.updateTimeEntry(entry.id, {
// Toggl loves seconds
duration: TimeCrisis.convertHoursToSeconds(
TimeCrisis.roundHourToQuarterHour(entry_in_hours)
)
});
}
}
} else {
console.info(
`Warning: "${entry.description}" is currently running and was not be accounted for.`
);
}
bar.tick();
});
if (args.output) {
await Promise.all(timesheet_promises);
debug("Exporting to CSV");
TimeCrisis.outputToCsv(timesheet_entries, args.output);
} else {
debug("Not exporting");
await Promise.all(timesheet_promises);
}
}