Path: blob/main/extensions/copilot/test/simulation/workbench/components/openInVSCode.tsx
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as cp from 'child_process';6import { promises as fs } from 'fs';7import * as mobxlite from 'mobx-react-lite';8import * as path from 'path';9import * as React from 'react';10import { ISimulationTest } from '../stores/simulationTestsProvider';111213export const OpenInVSCodeButton = mobxlite.observer(({ test }: { test: ISimulationTest }) => {14const simulationInputPath = test.simulationInputPath;15const matchResult = test.name.match(/case-\d+/);16if (!simulationInputPath || !matchResult) {17return null;18}19return (20<button21className='test-open-in-vscode'22onClick={() => { openInVSCode(simulationInputPath, matchResult[0]); }}>23Open In VS Code Insiders24</button>25);26});272829async function openInVSCode(simulationInputPath: string, caseName: string) {30try {31const conversation = JSON.parse(await fs.readFile(path.join(simulationInputPath, `${caseName}.conversation.json`), 'utf8'));32if (!Array.isArray(conversation) || conversation.length === 0 || !conversation[0].repo_folder) {33return;34}35const repoPath = path.join(simulationInputPath, 'repos', conversation[0].repo_folder);36cp.execFileSync('code-insiders', [repoPath]);3738const state = JSON.parse(await fs.readFile(path.join(simulationInputPath, conversation[0].stateFile), 'utf8'));39const documentFilePath = state?.activeTextEditor?.documentFilePath;40if (documentFilePath) {41const selection = state?.activeTextEditor?.selections?.[0];42const line = selection?.active?.line;43cp.execFileSync('code-insiders', ['-g', path.join(simulationInputPath, documentFilePath + (line ? `:${line + 1}` : ''))]);44}4546} catch (e) {47console.error(e);48}49}505152