-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added site runtime preview code behind ECS Config #1052
base: main
Are you sure you want to change the base?
Changes from all commits
8c25291
5a8ad34
e342730
9fd0f92
7c88ce6
086105d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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( | ||
vscode.l10n.t('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'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we read the port number from configuration? |
||
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( | ||
vscode.l10n.t("Failed to update launch.json: ${0}", e.message)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that other folders are following kebab case for naming. Can we rename this to |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,106 @@ | ||||||
/* | ||||||
* 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'; | ||||||
import { ECSFeaturesClient } from '../../common/ecs-features/ecsFeatureClient'; | ||||||
import { EnableSiteRuntimePreview } from '../../common/ecs-features/ecsFeatureGates'; | ||||||
import { ITelemetry } from '../../common/OneDSLoggerTelemetry/telemetry/ITelemetry'; | ||||||
import { WorkspaceFolder } from 'vscode-languageclient/node'; | ||||||
import { getWebsiteRecordID } from '../../common/utilities/WorkspaceInfoFinderUtil'; | ||||||
import { ServiceEndpointCategory } from '../../common/services/Constants'; | ||||||
import { PPAPIService } from '../../common/services/PPAPIService'; | ||||||
import { VSCODE_EXTENSION_GET_WEBSITE_RECORD_ID_EMPTY } from '../../common/services/TelemetryConstants'; | ||||||
|
||||||
export class PreviewSite { | ||||||
|
||||||
static isSiteRuntimePreviewEnabled(): boolean { | ||||||
const enableSiteRuntimePreview = ECSFeaturesClient.getConfig(EnableSiteRuntimePreview).enableSiteRuntimePreview | ||||||
|
||||||
if(enableSiteRuntimePreview === undefined) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just check like this? That'll cover both undefined and null.
Suggested change
|
||||||
return false; | ||||||
} | ||||||
|
||||||
return enableSiteRuntimePreview; | ||||||
} | ||||||
|
||||||
static async getWebSiteURL(workspaceFolders: WorkspaceFolder[], stamp: ServiceEndpointCategory, envId: string, telemetry: ITelemetry): Promise<string> { | ||||||
|
||||||
const websiteRecordId = getWebsiteRecordID(workspaceFolders, telemetry); | ||||||
if (!websiteRecordId) { | ||||||
telemetry.sendTelemetryEvent(VSCODE_EXTENSION_GET_WEBSITE_RECORD_ID_EMPTY, { | ||||||
websiteRecordId: websiteRecordId | ||||||
}); | ||||||
return ""; | ||||||
} | ||||||
const websiteDetails = await PPAPIService.getWebsiteDetailsByWebsiteRecordId(stamp, envId, websiteRecordId, telemetry); | ||||||
return websiteDetails?.websiteUrl || ""; | ||||||
} | ||||||
|
||||||
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)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
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( | ||||||
vscode.l10n.t( | ||||||
`The extension Microsoft Edge Tools is required to run this command. Do you want to install it now?`, | ||||||
'Install', 'Cancel' | ||||||
)); | ||||||
Comment on lines
+92
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this working or do we need to pass |
||||||
|
||||||
if (install === vscode.l10n.t('Install')) { | ||||||
// Open the Extensions view with the specific extension | ||||||
vscode.commands.executeCommand('workbench.extensions.search', edgeToolsExtensionId); | ||||||
} | ||||||
|
||||||
return; | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: whitespace