Files
vscodestat/src/extension.ts
T

108 lines
3.7 KiB
TypeScript

import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
console.log('Congratulations, your extension "vscodestat" is now active!');
// commande pour definir l'url
const setUrlCommand = vscode.commands.registerCommand('vscodestat.setUrl', async () => {
const url = await vscode.window.showInputBox({
prompt: 'Enter the URL:',
placeHolder: '',
});
if (url) {
await vscode.workspace.getConfiguration().update('vscodestat.url', url, vscode.ConfigurationTarget.Global);
vscode.window.showInformationMessage('URL set successfully.');
}
});
context.subscriptions.push(setUrlCommand);
// commande pour tester l'url
const callUrlCommand = 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(callUrlCommand);
// 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), source: 'vscode' });
}
});
// 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), source: 'vscode' });
});
// detecte un focus / blur de la fenetre vscode
vscode.window.onDidChangeWindowState(async (event) => {
console.log('Window state changed:', event.focused);
await makeHttpRequest({ event: 'focus', focused: event.focused });
});
}
export function deactivate() {}
/*
* extractProjectName will extract a project name from a path
* /root/docker/monitoringserver/controller/homeController.js
* => monitoringserver
*/
export 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);
}
}