UNPKG

ctrlshiftleft

Version:

AI-powered toolkit for embedding QA and security testing into development workflows

184 lines (150 loc) 7.78 kB
// The module 'vscode' is special in VS Code extensions // It's provided by VS Code when the extension is running const vscode = require('vscode'); const path = require('path'); const { exec } = require('child_process'); const { promisify } = require('util'); const execAsync = promisify(exec); /** * @param {vscode.ExtensionContext} context */ function activate(context) { console.log('Ctrl+Shift+Left extension is now active'); // Create output channel const outputChannel = vscode.window.createOutputChannel('Ctrl+Shift+Left'); context.subscriptions.push(outputChannel); // Create status bar item const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100); statusBarItem.text = '$(shield) CSL: Off'; statusBarItem.tooltip = 'Ctrl+Shift+Left: Security Monitoring Inactive'; statusBarItem.command = 'ctrlshiftleft.toggleWatcher'; statusBarItem.show(); context.subscriptions.push(statusBarItem); // Generate Tests command const generateTests = vscode.commands.registerCommand('ctrlshiftleft.generateTests', async () => { const editor = vscode.window.activeTextEditor; if (!editor) { vscode.window.showErrorMessage('No active editor found'); return; } outputChannel.clear(); outputChannel.show(); outputChannel.appendLine(`Generating tests for ${editor.document.fileName}`); vscode.window.showInformationMessage(`Generating tests for ${path.basename(editor.document.fileName)}`); // Get test directory try { const workspaceFolder = vscode.workspace.getWorkspaceFolder(editor.document.uri); const testDir = path.join(workspaceFolder.uri.fsPath, 'tests'); outputChannel.appendLine(`Target test directory: ${testDir}`); // This is where we would call the CLI, but for testing just show a success message outputChannel.appendLine('Tests generated successfully! This is a test message.'); // Show success notification vscode.window.showInformationMessage('Tests generated successfully!'); } catch (error) { outputChannel.appendLine(`Error: ${error.message}`); vscode.window.showErrorMessage(`Error generating tests: ${error.message}`); } }); // Run Tests command const runTests = vscode.commands.registerCommand('ctrlshiftleft.runTests', async () => { outputChannel.clear(); outputChannel.show(); outputChannel.appendLine('Running tests...'); vscode.window.showInformationMessage('Running tests...'); // This is where we would call the CLI, but for testing just show a success message outputChannel.appendLine('Tests complete: 3 passed, 0 failed'); // Show success notification vscode.window.showInformationMessage('Tests complete: 3 passed, 0 failed'); }); // Analyze Security Risks command const analyzeSecurityRisks = vscode.commands.registerCommand('ctrlshiftleft.analyzeSecurityRisks', async () => { const editor = vscode.window.activeTextEditor; if (!editor) { vscode.window.showErrorMessage('No active editor found'); return; } outputChannel.clear(); outputChannel.show(); outputChannel.appendLine(`Analyzing security risks for ${editor.document.fileName}`); vscode.window.showInformationMessage(`Analyzing security risks for ${path.basename(editor.document.fileName)}`); // Simulate security analysis outputChannel.appendLine('Security analysis complete!'); outputChannel.appendLine('Found 2 potential security issues:'); outputChannel.appendLine('1. [HIGH] Potential XSS vulnerability in user input handling'); outputChannel.appendLine('2. [MEDIUM] Insecure storage of sensitive data'); // Add some diagnostics to show in the editor const diagnostics = []; // Example: Find a line with "password" in it const text = editor.document.getText(); const passwordRegex = /password/i; let match; let line = 0; for (let i = 0; i < editor.document.lineCount; i++) { const lineText = editor.document.lineAt(i).text; if (passwordRegex.test(lineText)) { const diagnostic = new vscode.Diagnostic( new vscode.Range(i, 0, i, lineText.length), 'Potential security issue: Insecure storage of sensitive data', vscode.DiagnosticSeverity.Warning ); diagnostics.push(diagnostic); } } // Create a diagnostic collection and set the diagnostics const collection = vscode.languages.createDiagnosticCollection('ctrlshiftleft'); collection.set(editor.document.uri, diagnostics); // Show success notification vscode.window.showInformationMessage('Security analysis complete! Check the Problems panel for issues.'); }); // Generate Checklist command const generateChecklist = vscode.commands.registerCommand('ctrlshiftleft.generateChecklist', async () => { const editor = vscode.window.activeTextEditor; if (!editor) { vscode.window.showErrorMessage('No active editor found'); return; } outputChannel.clear(); outputChannel.show(); outputChannel.appendLine(`Generating QA & Security checklist for ${editor.document.fileName}`); vscode.window.showInformationMessage(`Generating checklist for ${path.basename(editor.document.fileName)}`); // Simulate checklist generation outputChannel.appendLine('Checklist generated successfully!'); outputChannel.appendLine('----------------------'); outputChannel.appendLine('QA & Security Checklist:'); outputChannel.appendLine('----------------------'); outputChannel.appendLine('1. [✓] Input validation implemented'); outputChannel.appendLine('2. [✓] Error handling implemented'); outputChannel.appendLine('3. [✗] Security tokens properly managed'); outputChannel.appendLine('4. [✗] Data sanitization implemented'); outputChannel.appendLine('5. [✓] Tests cover main functionality'); // Show success notification vscode.window.showInformationMessage('Checklist generated successfully!'); }); // Toggle Watcher command const toggleWatcher = vscode.commands.registerCommand('ctrlshiftleft.toggleWatcher', async () => { if (statusBarItem.text.includes('Off')) { statusBarItem.text = '$(shield) CSL: On'; statusBarItem.tooltip = 'Ctrl+Shift+Left: Security Monitoring Active'; vscode.window.showInformationMessage('Ctrl+Shift+Left watcher is now active'); } else { statusBarItem.text = '$(shield) CSL: Off'; statusBarItem.tooltip = 'Ctrl+Shift+Left: Security Monitoring Inactive'; vscode.window.showInformationMessage('Ctrl+Shift+Left watcher has been disabled'); } }); // Register all commands context.subscriptions.push( generateTests, runTests, analyzeSecurityRisks, generateChecklist, toggleWatcher ); // Show welcome message vscode.window.showInformationMessage('Ctrl+Shift+Left extension is now active!'); } function deactivate() {} module.exports = { activate, deactivate };