-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added site runtime preview code behind ECS Config
- Loading branch information
1 parent
6e9e863
commit 0c54e48
Showing
7 changed files
with
296 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,4 +256,4 @@ | |
"The {0} represents profile's Azure Cloud Instances" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*/ | ||
|
||
import * as vscode from 'vscode'; | ||
|
||
export async function updateLaunchJsonConfig(url: string): Promise<void> { | ||
|
||
const workspaceFolders = vscode.workspace.workspaceFolders; | ||
if (!workspaceFolders) { | ||
vscode.window.showErrorMessage('No workspace folder is open.'); | ||
return; | ||
} | ||
|
||
const launchJsonPath = vscode.Uri.joinPath(workspaceFolders[0].uri, '.vscode', 'launch.json'); | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let launchJson: any; | ||
let launchJsonDoc: vscode.TextDocument | undefined; | ||
|
||
try { | ||
launchJsonDoc = await vscode.workspace.openTextDocument(launchJsonPath); | ||
const launchJsonText = launchJsonDoc.getText(); | ||
launchJson = launchJsonText ? JSON.parse(launchJsonText) : { configurations: [], compounds: [] }; | ||
} catch (error) { | ||
// If the file does not exist or is empty, initialize it | ||
launchJson = { configurations: [], compounds: [] }; | ||
} | ||
|
||
// Update or add the configurations for Microsoft Edge | ||
const edgeConfigurations = [ | ||
{ | ||
type: 'pwa-msedge', | ||
name: 'Launch Microsoft Edge', | ||
request: 'launch', | ||
runtimeArgs: ['--remote-debugging-port=9222'], | ||
url: url, | ||
presentation: { | ||
hidden: true | ||
} | ||
}, | ||
{ | ||
type: 'pwa-msedge', | ||
name: 'Launch Microsoft Edge in headless mode', | ||
request: 'launch', | ||
runtimeArgs: ['--headless', '--remote-debugging-port=9222'], | ||
url: url, | ||
presentation: { | ||
hidden: true | ||
} | ||
}, | ||
{ | ||
type: 'vscode-edge-devtools.debug', | ||
name: 'Open Edge DevTools', | ||
request: 'attach', | ||
url: url, | ||
presentation: { | ||
hidden: true | ||
} | ||
} | ||
]; | ||
|
||
// Update or add the compounds for Microsoft Edge | ||
const edgeCompounds = [ | ||
{ | ||
name: 'Launch Edge Headless and attach DevTools', | ||
configurations: ['Launch Microsoft Edge in headless mode', 'Open Edge DevTools'] | ||
}, | ||
{ | ||
name: 'Launch Edge and attach DevTools', | ||
configurations: ['Launch Microsoft Edge', 'Open Edge DevTools'] | ||
} | ||
]; | ||
|
||
// Merge the new configurations and compounds with the existing ones | ||
launchJson.configurations = [...launchJson.configurations, ...edgeConfigurations]; | ||
launchJson.compounds = [...launchJson.compounds, ...edgeCompounds]; | ||
|
||
// Write the updated launch.json file | ||
const launchJsonContent = JSON.stringify(launchJson, null, 4); | ||
await vscode.workspace.fs.writeFile(launchJsonPath, Buffer.from(launchJsonContent, 'utf8')); | ||
} catch (e) { | ||
if(e instanceof Error) { | ||
vscode.window.showErrorMessage(`Failed to update launch.json: ${e.message}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { updateLaunchJsonConfig } from './LaunchJsonHelper'; | ||
|
||
export class PreviewSite { | ||
|
||
static async launchBrowserAndDevToolsWithinVsCode(webSitePreviewURL: string): Promise<void> { | ||
|
||
const edgeToolsExtensionId = 'ms-edgedevtools.vscode-edge-devtools'; | ||
const edgeToolsExtension = vscode.extensions.getExtension(edgeToolsExtensionId); | ||
|
||
if (edgeToolsExtension) { | ||
// Preserve the original state of the launch.json file and .vscode folder | ||
const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; | ||
const vscodeFolderPath = workspaceFolder ? path.join(workspaceFolder.uri.fsPath, '.vscode') : null; | ||
const launchJsonPath = vscodeFolderPath ? path.join(vscodeFolderPath, 'launch.json') : null; | ||
let originalLaunchJsonContent: string | null = null; | ||
let vscodeFolderExisted = false; | ||
|
||
if (vscodeFolderPath && fs.existsSync(vscodeFolderPath)) { | ||
vscodeFolderExisted = true; | ||
if (launchJsonPath && fs.existsSync(launchJsonPath)) { | ||
originalLaunchJsonContent = fs.readFileSync(launchJsonPath, 'utf8'); | ||
} | ||
} | ||
|
||
await updateLaunchJsonConfig(webSitePreviewURL); | ||
|
||
try { | ||
// Added a 2-second delay before executing the launchProject command to handle the case where the launch.json file is not saved yet | ||
await new Promise(resolve => setTimeout(resolve, 2000)); | ||
await vscode.commands.executeCommand('vscode-edge-devtools-view.launchProject'); | ||
|
||
} finally { | ||
// Revert the changes made to the launch.json file and remove the .vscode folder if it was created | ||
|
||
// Added a 2-second delay to ensure that debug session is closed and then launch.json file is removed | ||
await new Promise(resolve => setTimeout(resolve, 2000)); | ||
if (launchJsonPath) { | ||
if (originalLaunchJsonContent !== null) { | ||
fs.writeFileSync(launchJsonPath, originalLaunchJsonContent, 'utf8'); | ||
} else if (fs.existsSync(launchJsonPath)) { | ||
fs.unlinkSync(launchJsonPath); | ||
} | ||
} | ||
|
||
if (vscodeFolderPath && !vscodeFolderExisted && fs.existsSync(vscodeFolderPath)) { | ||
const files = fs.readdirSync(vscodeFolderPath); | ||
if (files.length === 0) { | ||
fs.rmdirSync(vscodeFolderPath); | ||
} | ||
} | ||
} | ||
} else { | ||
const install = await vscode.window.showWarningMessage( | ||
`The extension "${edgeToolsExtensionId}" is required to run this command. Do you want to install it now?`, | ||
'Install', 'Cancel' | ||
); | ||
|
||
if (install === 'Install') { | ||
// Open the Extensions view with the specific extension | ||
vscode.commands.executeCommand('workbench.extensions.search', edgeToolsExtensionId); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters