import * as vscode from 'vscode'; import { ServerInstance } from '../simulator/serverInstance'; import { SimulatorView } from '../simulator/simulatorView'; export async function start(server: ServerInstance) { const config = getCustomConfiguration(); let portNumber = config.portNumber; portNumber = typeof portNumber === 'number' ? portNumber : config.portNumber || null; return server.start(portNumber, (await selectWorkspace()) ?? '').then( () => { /*vscode.window.showInformationMessage( `PayPay Simulator is started and listening on port ${portNumber}` );*/ return portNumber; }, (err) => { if (err.message === 'already started') { vscode.window.showErrorMessage('PayPay Simulator is already running'); } else if (err.code === 'EADDRINUSE') { vscode.window.showErrorMessage( `Port number ${portNumber} is already in use` ); } else { vscode.window.showErrorMessage(err.message); } } ); } export async function stop(server: ServerInstance) { try { await server.stop(); //vscode.window.showInformationMessage('PayPay Simulator stopped'); } catch (err) { vscode.window.showErrorMessage(err.message); } } export function openBrowser() { vscode.commands .executeCommand('paypay.devtool.simulator.geturl') .then((simulatorUrl) => { vscode.commands.executeCommand( 'vscode.open', vscode.Uri.parse(String(simulatorUrl)) ); }); } export async function openPreview( context: vscode.ExtensionContext, server: ServerInstance | undefined, simulatorView: SimulatorView | undefined ) { if ((server?.getUrl() ?? '').length === 0) { server = await vscode.commands.executeCommand( 'paypay.devtool.simulator.start' ); } if (server) { if (simulatorView) { simulatorView.webPanel?.reveal(vscode.ViewColumn.Two); } else { simulatorView = new SimulatorView(context); } return simulatorView; } } export async function prepare(server?: ServerInstance) { if ((server?.getUrl() ?? '').length > 0) { await vscode.commands.executeCommand('paypay.devtool.simulator.stop'); } server = await vscode.commands.executeCommand( 'paypay.devtool.simulator.start' ); if (server) { return await vscode.commands.executeCommand( 'paypay.devtool.simulator.geturl' ); } return ''; } function getCustomConfiguration() { return ( vscode.workspace.getConfiguration('paypay') || { portNumber: null, } ); } async function selectWorkspace() { let workspacePath = ''; if (vscode.workspace.workspaceFolders?.length === 0) { return vscode.window.showErrorMessage( 'Please open a folder or workspace before starting the Simulator' ); } else if ( vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length === 1 && vscode.workspace.workspaceFolders[0].uri.fsPath ) { workspacePath = vscode.workspace.workspaceFolders[0]?.uri.fsPath; } else { workspacePath = (await pickWorkspace()) ?? ''; } return workspacePath; } async function pickWorkspace() { let workspaceNames = vscode.workspace.workspaceFolders?.map((a) => a.name); if ((workspaceNames?.length ?? 0) > 0) { let projName = await vscode.window.showQuickPick(workspaceNames!, { placeHolder: 'Please choose a Project', }); return vscode.workspace.workspaceFolders?.find((i) => i.name === projName) ?.uri.fsPath; } return ''; }