-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
172 lines (154 loc) · 5.03 KB
/
extension.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const vscode = require('vscode');
const path = require('path');
const axios = require('axios');
let sessionActive = false;
let apiKey = '';
let apiUrl = '';
let eventBody = {
sessionStarted: null,
activeFile: null,
sessionEnded: null,
};
async function activate(context) {
const fileLocation = vscode.Uri.joinPath(context.extensionUri, 'data.json');
try {
// Check if data.json exists
const fileExists = await vscode.workspace.fs.stat(fileLocation).then(
() => true,
() => false
);
if (fileExists) {
// If the file exists, read its content
const dataBuffer = await vscode.workspace.fs.readFile(fileLocation);
const storedData = JSON.parse(dataBuffer.toString());
apiKey = storedData.apiKey;
apiUrl = storedData.apiUrl;
// Checking the validity of the stored API key
const checkResponse = await checkValidityAPI(apiKey);
await confirmAndSendTelemetryEvent(fileLocation, checkResponse);
} else {
// If the file doesn't exist, ask the user for the API key
apiKey = await vscode.window.showInputBox({
prompt: 'Enter API Key',
placeHolder: 'Get it from vscode.tharindu.me',
});
// Check the validity of the API key
const checkResponse = await checkValidityAPI(apiKey);
await confirmAndSendTelemetryEvent(fileLocation, checkResponse);
}
} catch (error) {
// Handle errors gracefully
console.error(error);
vscode.window.showErrorMessage(
'Error reading data.json. Please check the extension logs.'
);
}
}
async function deactivate() {
const endTime = new Date();
await sendTelemetryEvent(apiUrl, 'sessionEnded', { endTime });
}
async function confirmAndSendTelemetryEvent(fileLocation, checkResponse) {
if (!checkResponse.data.profile.valid) {
vscode.window.showErrorMessage(
'Stored API Key is invalid. Please re-enter.'
);
// Give the user a chance to correct the invalid API key
apiKey = await vscode.window.showInputBox({
prompt: 'Enter API Key',
placeHolder: 'Get it from vscode.tharindu.me',
});
//Check validity of the new API key
checkResponse = await checkValidityAPI(apiKey);
await confirmAndSendTelemetryEvent(fileLocation, checkResponse);
} else {
//Get latest data and save it to data.json
newUrl = checkResponse.data.profile.data.url;
newKey = checkResponse.data.profile.data.api;
dataToStore = { apiKey: newKey, apiUrl: newUrl };
const dataBuffer = Buffer.from(JSON.stringify(dataToStore), 'utf8');
await vscode.workspace.fs.writeFile(fileLocation, dataBuffer);
if (checkResponse.data.profile.data.enabled) {
//Send start session event
const startTime = new Date();
sendTelemetryEvent(newUrl, 'sessionStarted', { startTime });
sessionActive = true;
//Listen for changes in the active text editor
vscode.window.onDidChangeActiveTextEditor((editor) => {
if (editor) {
const activeFile = path.basename(editor.document.fileName);
const timeSwitched = new Date();
sendTelemetryEvent(newUrl, 'fileSwitched', {
activeFile,
timeSwitched,
});
}
});
} else {
vscode.window.showInformationMessage(
'Extension is disabled. Please enable it from vscode.tharindu.me'
);
}
}
return;
}
async function checkValidityAPI(apiKey) {
const checkUrl = `https://vscode.tharindu.me/api/check/${apiKey}`;
try {
const checkResponse = await axios.get(checkUrl);
return checkResponse;
} catch (error) {
if (error.response && error.response.status === 401) {
vscode.window.showErrorMessage('Invalid API Key. Please re-enter.');
newKey = await vscode.window.showInputBox({
prompt: 'Enter API Key',
placeHolder: 'Get it from vscode.tharindu.me',
});
return checkValidityAPI(newKey);
} else {
// Handle other errors
console.error(error);
vscode.window.showErrorMessage('Error checking API validity.');
throw error; // rethrow the error if it's not a 401
}
}
}
async function sendTelemetryEvent(apiUrl, eventName, data) {
if (!apiUrl) {
vscode.window.showErrorMessage(
'API URL is not set. Please set it in extension settings.'
);
return;
}
switch (eventName) {
case 'sessionStarted':
eventBody.sessionStarted = { valid: true, time: data.startTime };
break;
case 'fileSwitched':
eventBody.activeFile = {
valid: true,
name: data.activeFile,
time: data.timeSwitched,
};
break;
case 'sessionEnded':
eventBody.sessionEnded = { valid: true, time: data.endTime };
break;
default:
break;
}
try {
const response = await axios.post(apiUrl, eventBody, {
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
console.error(error);
vscode.window.showErrorMessage(
'Check your receiving URL in extension settings, and restart the VS Code.'
);
}
}
module.exports = {
activate,
deactivate,
};