Skip to content

Commit e33707e

Browse files
Copilothediet
andcommitted
Implement jsdbg command for debugging Node.js from any terminal
Co-authored-by: hediet <2931520+hediet@users.noreply.github.com>
1 parent 243718a commit e33707e

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { debugNpmScript } from './ui/debugNpmScript';
2525
import { DebugSessionTracker } from './ui/debugSessionTracker';
2626
import { registerDebugTerminalUI } from './ui/debugTerminalUI';
2727
import { ExtensionApiFactory } from './ui/extensionApi';
28+
import { registerJsdbgCommand } from './ui/jsdbgCommand';
2829
import { attachProcess, pickProcess } from './ui/processPicker';
2930
import { registerProfilingCommand } from './ui/profiling';
3031
import { registerRequestCDPProxy } from './ui/requestCDPProxy';
@@ -104,6 +105,7 @@ export function activate(context: vscode.ExtensionContext): IExports {
104105
registerCompanionBrowserLaunch(context);
105106
registerCustomBreakpointsUI(context, debugSessionTracker);
106107
registerDebugTerminalUI(context, services.get(DelegateLauncherFactory), services);
108+
registerJsdbgCommand(context);
107109
registerProfilingCommand(context, services);
108110
registerAutoAttach(context, services.get(DelegateLauncherFactory), services);
109111
registerRevealPage(context, debugSessionTracker);

src/ui/jsdbgCommand.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)