diff --git a/vscode-extensions/debug-certificate-manager-vscode-extension/src/extension.ts b/vscode-extensions/debug-certificate-manager-vscode-extension/src/extension.ts index 3e027fa5574..bc35709db4a 100644 --- a/vscode-extensions/debug-certificate-manager-vscode-extension/src/extension.ts +++ b/vscode-extensions/debug-certificate-manager-vscode-extension/src/extension.ts @@ -209,21 +209,16 @@ export function activate(context: vscode.ExtensionContext): void { if (vscode.env.remoteName) { const markerPrefix: string = '<<>>'; const markerSuffix: string = '<<>>'; - const output: string = await runWorkspaceCommandAsync({ + homeDir = await runWorkspaceCommandAsync({ terminalOptions: { name: 'debug-certificate-manager', hideFromUser: true }, - // Wrapping the desired node output in markers to trim uninteresting shell output. commandLine: `node -p "'${markerPrefix}' + require('os').homedir() + '${markerSuffix}'"`, - terminal + terminal, + outputMarker: { + prefix: markerPrefix, + suffix: markerSuffix + } }); - terminal.writeLine(`Running command to resolve home directory: ${output}`); - - const startIndex: number = output.lastIndexOf(markerPrefix); - const endIndex: number = output.lastIndexOf(markerSuffix); - if (startIndex !== -1 && endIndex !== -1 && endIndex > startIndex) { - homeDir = output.substring(startIndex + markerPrefix.length, endIndex).trim(); - } else { - throw new Error('Failed to parse home directory from command output'); - } + terminal.writeLine(`Running command to resolve home directory: ${homeDir}`); } else { homeDir = require('os').homedir(); } diff --git a/vscode-extensions/playwright-local-browser-server-vscode-extension/src/extension.ts b/vscode-extensions/playwright-local-browser-server-vscode-extension/src/extension.ts index 251dac4e6e4..e18bee18bd4 100644 --- a/vscode-extensions/playwright-local-browser-server-vscode-extension/src/extension.ts +++ b/vscode-extensions/playwright-local-browser-server-vscode-extension/src/extension.ts @@ -47,20 +47,16 @@ async function writeExtensionInstalledFileAsync(terminal: ITerminal): Promise startIndex) { - tempDir = output.substring(startIndex + markerPrefix.length, endIndex).trim(); - } else { - throw new Error('Failed to parse temp directory from command output'); - } - // For remote environments, use the vscode-remote scheme // The workspace folder should have the correct scheme already const workspaceFolder: vscode.WorkspaceFolder | undefined = vscode.workspace.workspaceFolders?.[0]; diff --git a/vscode-extensions/vscode-shared/src/runWorkspaceCommandAsync.ts b/vscode-extensions/vscode-shared/src/runWorkspaceCommandAsync.ts index a84c376c055..8d00a91c42d 100644 --- a/vscode-extensions/vscode-shared/src/runWorkspaceCommandAsync.ts +++ b/vscode-extensions/vscode-shared/src/runWorkspaceCommandAsync.ts @@ -5,14 +5,37 @@ import { ITerminal } from '@rushstack/terminal'; import * as vscode from 'vscode'; import { stripVTControlCharacters } from 'node:util'; +/** + * Options for extracting a specific value from the command output using markers. + * When provided, the function will search for the markers in the command output and + * return only the content between them. This is useful for extracting specific + * values from shell output that may contain additional noise. + */ +export interface IOutputMarkerOptions { + /** + * The prefix marker used to identify the start of the desired output. + */ + prefix: string; + /** + * The suffix marker used to identify the end of the desired output. + */ + suffix: string; +} + export async function runWorkspaceCommandAsync({ terminalOptions, commandLine, - terminal + terminal, + outputMarker }: { terminalOptions: vscode.TerminalOptions; commandLine: string; terminal: ITerminal; + /** + * Optional marker options for extracting specific output from the command result. + * When provided, the returned output will be only the content between the markers. + */ + outputMarker?: IOutputMarkerOptions; }): Promise { const vsTerminal: vscode.Terminal = vscode.window.createTerminal(terminalOptions); @@ -66,7 +89,21 @@ export async function runWorkspaceCommandAsync({ startExecutionDisposable.dispose(); if (event.exitCode === 0) { - resolve(outputStream); + // If outputMarker was provided, extract the content between markers + if (outputMarker) { + const startIndex: number = outputStream.indexOf(outputMarker.prefix); + const endIndex: number = outputStream.indexOf(outputMarker.suffix); + if (startIndex !== -1 && endIndex !== -1 && endIndex > startIndex) { + const extractedOutput: string = outputStream + .substring(startIndex + outputMarker.prefix.length, endIndex) + .trim(); + resolve(extractedOutput); + } else { + reject(new Error('Failed to parse output from command: markers not found')); + } + } else { + resolve(outputStream); + } } else { reject(outputStream); }