Path: blob/main/extensions/copilot/script/electron/simulationWorkbenchMain.js
13389 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*--------------------------------------------------------------------------------------------*/45//@ts-check67const electron = require('electron');8const child_process = require('child_process');9const path = require('path');1011const app = electron.app;1213/**14* @type {Electron.BrowserWindow | null}15* The main window of the Electron application.16*/17let mainWindow = null;1819function createWindow() {20const { width, height } = electron.screen.getPrimaryDisplay().workAreaSize;21mainWindow = new electron.BrowserWindow({22width: width * 0.75,23height: height,24webPreferences: {25nodeIntegration: true,26contextIsolation: false27}28});29mainWindow.loadURL(`file://${__dirname}/simulationWorkbench.html`);30mainWindow.on('closed', function () {31mainWindow = null;32});33}3435app.on('ready', () => {36if (process.argv.includes('--help')) {37console.log(`Options:38--run-dir=DIRNAME Provide the run output directory name, e.g., 'out-20231201-151346'.39--grep=STRING Pre-populates simulation workbench 'grep' input box.`);40app.quit();41}42registerListeners();43createWindow();44});4546app.on('window-all-closed', function () {47if (process.platform !== 'darwin') {48app.quit();49}50});5152app.on('activate', function () {53if (mainWindow === null) {54createWindow();55}56});5758// change to configure logging, e.g., to `console.debug`59const log = {60debug: (..._args) => { }61};6263function registerListeners() {6465/** @type {Map<string, child_process.ChildProcess>} */66const spawnedProcesses = new Map();6768/**69* Spawns a new child process and sets up listeners for its stdout, stderr, and exit events.70*71* @param {Object} event - The event object from the Electron IPC.72* @param {Object} options - The options for the child process.73* @param {string} options.id - The unique identifier for the child process created by the renderer process.74* @param {Array<string>} options.processArgs - The arguments to pass to the child process.75*/76function spawnProcess(event, { id, processArgs }) {7778log.debug(`main process: spawn-process (id: ${id}, processArgs: ${JSON.stringify(processArgs)})`);7980const child = child_process.spawn(81'node',82[path.join(__dirname, '../../dist', 'simulationMain.js'), ...processArgs],83{ stdio: 'pipe' }84);85if (child.pid) {86child.stdout.setEncoding('utf8');87child.stdout.on('data', (data) => {88log.debug(`main process: stdout: ${data.toString()}`);89event.sender.send('stdout-data', { id, data });90});91child.stderr.setEncoding('utf8');92child.stderr.on('data', (data) => {93log.debug(`main process: stderr: ${data.toString()}`);94event.sender.send('stderr-data', { id, data });95});96child.on('exit', (code) => {97log.debug('main process: ' + JSON.stringify(code, null, '\t'));98spawnedProcesses.delete(id);99event.sender.send('process-exit', { id, code });100});101spawnedProcesses.set(id, child);102}103}104105electron.ipcMain.on('spawn-process', spawnProcess);106107electron.ipcMain.on('kill-process', (_event, { id }) => {108spawnedProcesses.get(id)?.kill('SIGTERM');109spawnedProcesses.delete(id);110});111112electron.ipcMain.on('open-link', (_event, url) => {113electron.shell.openExternal(url);114});115116electron.ipcMain.handle('processArgv', () => {117return process.argv;118});119}120121122