-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
121 lines (103 loc) · 3.32 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
119
120
121
/* ==========================================================
WakaTime
Description: Analytics for programmers.
Maintainer: WakaTime <[email protected]>
License: BSD, see LICENSE for more details.
Website: https://wakatime.com/
author: @jvelezpo, @uziiuzair
===========================================================*/
'use strict';
let application = require('application');
const { shell } = require('uxp');
const Preferences = require('./lib/preferences');
const Libs = require('./lib/libs');
const { prompt, errorOpenWakatime } = require("./lib/dialog/dialogs");
const VERSION = '1.0.0';
let lastAction = 0,
lastFile = undefined;
const openDashboardWebsite = async () => {
const error = await errorOpenWakatime('Error', validation);
if (error.which === 0) {
const url = 'https://wakatime.com/';
shell.openExternal(url)
}
};
const getApiKey = async () => {
const preferences = await Preferences.createFromSettings();
const validation = Libs.validateKey(preferences.apiKey);
if (validation !== '') {
return '';
}
return preferences.apiKey;
}
const enoughTimePassed = () => {
return lastAction + 120000 < Date.now();
}
const sendHeartbeat = async (file, time, project, language, isWrite, lines) => {
const apiKey = await getApiKey();
try {
console.log('Testing 2');
await fetch('https://wakatime.com/api/v1/heartbeats', {
method: 'POST',
body: JSON.stringify({
time : time / 1000,
entity : file,
category : "designing",
type : "app",
project : file,
language,
is_write : true,
lines : lines,
plugin : "adobexd-wakatime/" + VERSION,
}),
headers: {
'Authorization': 'Basic ' + Libs.btoa(apiKey),
'Content-Type': 'application/json'
}
});
} catch (err) {
console.log('Error(heartbeats):', err);
}
lastAction = time;
lastFile = file;
};
const handleAction = (isWrite) => {
const currentDocument = application.activeDocument;
if (currentDocument) {
var time = Date.now();
if (isWrite || enoughTimePassed() || lastFile !== currentDocument.name) {
sendHeartbeat(currentDocument.name, time, currentDocument.name, undefined, isWrite, null);
}
}
}
(() => {
window.setInterval(async () => {
handleAction(true);
console.log('Testing');
}, 5000);
}
)();
/**
* Create the input modal where the user inputs his/her api key
*/
const openApiKeyDialog = async () => {
const preferences = await Preferences.createFromSettings();
const title = 'WakaTime';
const message = 'Enter your wakatime.com api key:';
const result = await prompt(title, message, 'api key', preferences.apiKey);
if (result.which === 1) {
const newApiKey = result.value;
preferences.apiKey = newApiKey;
const validation = Libs.validateKey(newApiKey)
if (validation !== '') {
openDashboardWebsite();
} else {
await prompt(title, 'api key set correctly');
}
}
};
module.exports = {
commands: {
apiKeyDialog: openApiKeyDialog
}
};