|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | + |
| 5 | +import * as vscode from 'vscode'; |
| 6 | +import { Commands } from '../common/contributionUtils'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Registers the jsdbg command terminal integration. |
| 10 | + * This allows users to run `jsdbg node myScript.js` in any terminal |
| 11 | + * to start a debug session, similar to the JavaScript Debug Terminal. |
| 12 | + */ |
| 13 | +export function registerJsdbgCommand(context: vscode.ExtensionContext) { |
| 14 | + // Listen for command executions in all terminals |
| 15 | + context.subscriptions.push( |
| 16 | + vscode.window.onDidStartTerminalShellExecution(async event => { |
| 17 | + const commandLine = event.execution.commandLine; |
| 18 | + if (!commandLine || typeof commandLine.value !== 'string') { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const command = commandLine.value.trim(); |
| 23 | + |
| 24 | + // Check if the command starts with jsdbg |
| 25 | + if (command.startsWith('jsdbg ')) { |
| 26 | + // Extract the actual command to debug |
| 27 | + const actualCommand = command.substring('jsdbg '.length).trim(); |
| 28 | + |
| 29 | + if (!actualCommand) { |
| 30 | + vscode.window.showErrorMessage('Usage: jsdbg <command>'); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // Get the current working directory from the terminal |
| 35 | + const cwd = await getTerminalCwd(event.terminal); |
| 36 | + |
| 37 | + // Create a JavaScript Debug Terminal with the command |
| 38 | + await vscode.commands.executeCommand( |
| 39 | + Commands.CreateDebuggerTerminal, |
| 40 | + actualCommand, |
| 41 | + cwd ? vscode.workspace.getWorkspaceFolder(vscode.Uri.file(cwd)) : undefined, |
| 42 | + cwd ? { cwd } : undefined |
| 43 | + ); |
| 44 | + } |
| 45 | + }) |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Attempts to get the current working directory of a terminal. |
| 51 | + */ |
| 52 | +async function getTerminalCwd(terminal: vscode.Terminal): Promise<string | undefined> { |
| 53 | + // Try to get the CWD from shell integration |
| 54 | + const shellIntegration = terminal.shellIntegration; |
| 55 | + if (shellIntegration && shellIntegration.cwd) { |
| 56 | + return shellIntegration.cwd.fsPath; |
| 57 | + } |
| 58 | + |
| 59 | + // Fallback to workspace folder |
| 60 | + const folders = vscode.workspace.workspaceFolders; |
| 61 | + return folders?.[0]?.uri.fsPath; |
| 62 | +} |
0 commit comments