-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
151 lines (123 loc) · 4.77 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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const path = require("path");
const Octokit = require("@octokit/core").Octokit;
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "ishowoff" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let disposable = vscode.commands.registerCommand('ishowoff.helloWorld', function () {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
vscode.window.showInformationMessage(`Hello World from ishowoff! ${vscode.workspace.name}`);
});
let showOffActivate = vscode.commands.registerCommand('ishowoff.activatePresence', function () {
vscode.window.showInformationMessage('Github ShowOff Activated !');
const configuration = vscode.workspace.getConfiguration('ishowoff');
const token = configuration.get('github.token');
const username = configuration.get('github.username');
const repo = configuration.get('github.actionsRepo');
const interval = configuration.get('github.interval');
const theme = configuration.get('formatting.theme');
const customCommands = configuration.get('runs.customCommands');
// register on config changed
vscode.workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration('ishowoff')) {
vscode.window.showInformationMessage('IShowOff detected changes in configuration. A reload is required.',
'Reload VS Code'
).then((value) => {
if (value == "Reload VS Code") {
vscode.commands.executeCommand('workbench.action.reloadWindow')
}
});
}
})
if (checkConfig(token, username, repo, theme, interval)) {
updateActivity(token, username, repo, theme, customCommands, interval);
}
else {
vscode.window.showInformationMessage('IShowOff requires configuration. Please set the token and other information.',
'Open Settings'
).then((value) => {
if (value == "Open Settings") {
vscode.commands.executeCommand("workbench.action.openSettings", "ishowoff")
}
}
);
}
})
let showOffDisable = vscode.commands.registerCommand('ishowoff.disablePresence', function () {
vscode.window.showInformationMessage('Github ShowOff Disabled !');
})
vscode.commands.executeCommand('ishowoff.activatePresence')
context.subscriptions.push(disposable, showOffActivate, showOffDisable);
}
// This method is called when your extension is deactivated
function deactivate() { }
function updateActivity(token, username, repo, theme, customCommands, interval) {
const startTime = Math.floor(Date.now() / 1000);
const octokit = new Octokit({
auth: token
});
setInterval(async () => {
await octokit.request('POST /repos/{owner}/{repo}/dispatches', {
owner: username,
repo: repo,
event_type: 'update-from-vscode',
client_payload: {
arguments: `starttime=${startTime} theme=${theme} primary-text=${getWorkspaceName()} lang=${findFileType()} editor-text="Visual Studio Code" ${customCommands}`
},
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});
}, interval * 60 * 1000);
}
function getFileType(activeEditor) {
const document = activeEditor ? activeEditor.document : vscode.workspace.activeTextEditor.document;
return document.languageId;
}
function findFileType() {
// Get the file type of the currently active editor
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor == undefined) return "none";
const fileType = getFileType(activeEditor);
console.log(`Current file type: ${fileType}`);
return fileType;
}
function getWorkspaceName() {
if (vscode.workspace.name) {
return vscode.workspace.name;
} else {
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
return path.basename(activeEditor.document.fileName);
}
else {
return "Idling";
}
}
}
function checkConfig(token, username, repo, theme, interval) {
if (token == undefined || token == "XXXXXXXX" || token.trim() == ""
|| username == undefined || username.trim() == ""
|| repo == undefined || repo.trim() == ""
|| interval == undefined || interval < 1
|| theme == undefined || theme.trim() == "") {
return false;
}
return true;
}
module.exports = {
activate,
deactivate
}