vscodestat/src/extension.ts
2024-07-08 14:16:24 +02:00

114 lines
3.9 KiB
TypeScript

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:',
placeHolder: ''
});
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;
}
await makeHttpRequest({ event: 'ping'});
vscode.window.showInformationMessage('callUrl done');
});
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);
// vscode.window.showInformationMessage(`Opened file: ${filePath}`);
await makeHttpRequest({ event: 'open', project: extractProjectName(filePath) });
}
});
// detecte une sauvegarde de fichier
vscode.workspace.onDidSaveTextDocument(async document => {
const filePath = document.fileName;
console.log('Saved file:', filePath);
// vscode.window.showInformationMessage(`Saved file: ${filePath}`);
await makeHttpRequest({ event: 'save', project: extractProjectName(filePath) });
});
// 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() { }
/*
* extractProjectName will extract a project name from a path
* /root/docker/monitoringserver/controller/homeController.js
* => monitoringserver
*/
function extractProjectName(path: string) {
// des c'est l'un des fois l'autre ?
// /root/docker/vscodestat/src/extension.ts
// \root\docker\vscodestat\src\extension.ts
path = path.replace(/\\/g, '/');
const match = path.match(/\/docker\/([^/]+)/);
if (match) {
return match[1];
}
return null;
}
/*
* 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;
}
// vscode.window.showInformationMessage('Calling URL: ' + url);
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);
// vscode.window.showInformationMessage('HTTP request successful.');
} catch (error:any) {
console.error('Error making HTTP request:', error);
// vscode.window.showErrorMessage('Error making HTTP request: ' + error.message);
}
}