vscodestat/src/extension.ts

104 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-04-04 09:59:43 +02:00
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
console.log('Congratulations, your extension "vscodestat" is now active!');
// crée une commande de test
let disposable = vscode.commands.registerCommand('vscodestat.helloWorld', () => {
vscode.window.showInformationMessage('Hello World from vscodestat!');
});
context.subscriptions.push(disposable);
// commande pour definir l'url
let disposable2 = vscode.commands.registerCommand('vscodestat.setUrl', async () => {
const url = await vscode.window.showInputBox({
prompt: 'Enter the URL:',
2024-04-04 12:35:29 +02:00
placeHolder: ''
2024-04-04 09:59:43 +02:00
});
if (url) {
vscode.workspace.getConfiguration().update('vscodestat.url', url, vscode.ConfigurationTarget.Global);
vscode.window.showInformationMessage('URL set successfully.');
}
});
context.subscriptions.push(disposable2);
// commande pour tester l'url
let disposable3 = vscode.commands.registerCommand('vscodestat.callUrl', async () => {
const storedUrl = vscode.workspace.getConfiguration().get('vscodestat.url') as string;
if (!storedUrl) {
vscode.window.showWarningMessage('URL is not set. Please set the URL first.');
return;
}
2024-04-04 10:04:05 +02:00
await makeHttpRequest({ event: 'ping'});
2024-04-04 09:59:43 +02:00
});
context.subscriptions.push(disposable3);
// detecte une ouverture de fichier / nouvel onglet
vscode.window.onDidChangeActiveTextEditor(async editor => {
if (editor) {
const filePath = editor.document.fileName;
console.log('Opened file:', filePath);
2024-04-04 12:30:39 +02:00
// vscode.window.showInformationMessage(`Opened file: ${filePath}`);
2024-04-04 10:04:05 +02:00
await makeHttpRequest({ event: 'open', project: extractProjectName(filePath) });
2024-04-04 09:59:43 +02:00
}
});
// detecte une sauvegarde de fichier
vscode.workspace.onDidSaveTextDocument(async document => {
const filePath = document.fileName;
console.log('Saved file:', filePath);
2024-04-04 12:30:39 +02:00
// vscode.window.showInformationMessage(`Saved file: ${filePath}`);
2024-04-04 10:04:05 +02:00
await makeHttpRequest({ event: 'save', project: extractProjectName(filePath) });
2024-04-04 09:59:43 +02:00
});
// detecte un focus / blur de la fenetre vscode
vscode.window.onDidChangeWindowState(async event => {
console.log('Window state changed:', event.focused);
await makeHttpRequest({ focus: event.focused });
});
}
export function deactivate() { }
2024-04-04 10:04:05 +02:00
2024-04-04 10:06:19 +02:00
/*
* extractProjectName will extract a project name from a path
* /root/docker/monitoringserver/controller/homeController.js
* => monitoringserver
*/
2024-04-04 12:30:39 +02:00
function extractProjectName(path: string) {
2024-04-04 10:04:05 +02:00
const match = path.match(/\/root\/(docker|projects)\/([^/]+)/);
2024-04-04 12:30:39 +02:00
return match?.[2];
2024-04-04 10:04:05 +02:00
}
2024-04-04 10:06:19 +02:00
/*
* fonction qui envoie un event au serveur
*/
async function makeHttpRequest(json: Object) {
try {
const url = vscode.workspace.getConfiguration().get('vscodestat.url') as string;
if (!url) {
return;
}
2024-04-04 12:30:39 +02:00
// vscode.window.showInformationMessage('Calling URL: ' + url);
2024-04-04 10:06:19 +02:00
const rawResponse = await fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(json)
});
const content = await rawResponse.json();
console.log(content);
2024-04-04 12:30:39 +02:00
// vscode.window.showInformationMessage('HTTP request successful.');
2024-04-04 10:06:19 +02:00
} catch (error:any) {
console.error('Error making HTTP request:', error);
2024-04-04 12:30:39 +02:00
// vscode.window.showErrorMessage('Error making HTTP request: ' + error.message);
2024-04-04 10:06:19 +02:00
}
}