|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +//import * as nls from 'vscode-nls'; |
| 8 | +import * as util from 'util'; |
| 9 | + |
| 10 | +const trackers = new Set<string>(); |
| 11 | + |
| 12 | +const PATTERN = 'listening on.* (https?://\\S+|[0-9]+)'; // matches "listening on port 3000" or "Now listening on: https://localhost:5001" |
| 13 | +const URI_FORMAT = 'http://localhost:%s'; |
| 14 | +const WEB_ROOT = '${workspaceFolder}'; |
| 15 | + |
| 16 | +interface ServerReadyAction { |
| 17 | + pattern: string; |
| 18 | + action?: 'openExternally' | 'debugWithChrome'; |
| 19 | + urlFormat?: string; |
| 20 | + webRoot?: string; |
| 21 | +} |
| 22 | + |
| 23 | +export function activate(context: vscode.ExtensionContext) { |
| 24 | + |
| 25 | + context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('*', { |
| 26 | + resolveDebugConfiguration(_folder: vscode.WorkspaceFolder | undefined, debugConfiguration: vscode.DebugConfiguration) { |
| 27 | + const args: ServerReadyAction = debugConfiguration.serverReadyAction; |
| 28 | + if (debugConfiguration.type && args) { |
| 29 | + startTrackerForType(context, debugConfiguration.type); |
| 30 | + } |
| 31 | + return debugConfiguration; |
| 32 | + } |
| 33 | + })); |
| 34 | +} |
| 35 | + |
| 36 | +function startTrackerForType(context: vscode.ExtensionContext, type: string) { |
| 37 | + |
| 38 | + if (!trackers.has(type)) { |
| 39 | + trackers.add(type); |
| 40 | + |
| 41 | + // scan debug console output for a PORT message |
| 42 | + context.subscriptions.push(vscode.debug.registerDebugAdapterTrackerFactory(type, { |
| 43 | + createDebugAdapterTracker(session: vscode.DebugSession) { |
| 44 | + const args: ServerReadyAction = session.configuration.serverReadyAction; |
| 45 | + if (args) { |
| 46 | + const regexp = new RegExp(args.pattern || PATTERN); |
| 47 | + let hasFired = false; |
| 48 | + return { |
| 49 | + onDidSendMessage: m => { |
| 50 | + if (!hasFired && m.type === 'event' && m.event === 'output' && m.body.output) { |
| 51 | + const result = regexp.exec(m.body.output); |
| 52 | + if (result && result.length === 2) { |
| 53 | + openExternalWithString(session, result[1]); |
| 54 | + hasFired = true; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + }; |
| 59 | + } |
| 60 | + return undefined; |
| 61 | + } |
| 62 | + })); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +function openExternalWithString(session: vscode.DebugSession, portOrUriString: string) { |
| 67 | + |
| 68 | + if (portOrUriString) { |
| 69 | + if (/^[0-9]+$/.test(portOrUriString)) { |
| 70 | + const args: ServerReadyAction = session.configuration.serverReadyAction; |
| 71 | + portOrUriString = util.format(args.urlFormat || URI_FORMAT, portOrUriString); |
| 72 | + } |
| 73 | + openExternalWithUri(session, portOrUriString); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function openExternalWithUri(session: vscode.DebugSession, uri: string) { |
| 78 | + |
| 79 | + const args: ServerReadyAction = session.configuration.serverReadyAction; |
| 80 | + switch (args.action || 'openExternally') { |
| 81 | + case 'openExternally': |
| 82 | + vscode.env.openExternal(vscode.Uri.parse(uri)); |
| 83 | + break; |
| 84 | + case 'debugWithChrome': |
| 85 | + vscode.debug.startDebugging(session.workspaceFolder, { |
| 86 | + type: 'chrome', |
| 87 | + name: 'Chrome Debug', |
| 88 | + request: 'launch', |
| 89 | + url: uri, |
| 90 | + webRoot: args.webRoot || WEB_ROOT |
| 91 | + }); |
| 92 | + break; |
| 93 | + default: |
| 94 | + // not supported |
| 95 | + break; |
| 96 | + } |
| 97 | +} |
0 commit comments