Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/script/electron/simulationWorkbenchMain.js
13389 views
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
//@ts-check
7
8
const electron = require('electron');
9
const child_process = require('child_process');
10
const path = require('path');
11
12
const app = electron.app;
13
14
/**
15
* @type {Electron.BrowserWindow | null}
16
* The main window of the Electron application.
17
*/
18
let mainWindow = null;
19
20
function createWindow() {
21
const { width, height } = electron.screen.getPrimaryDisplay().workAreaSize;
22
mainWindow = new electron.BrowserWindow({
23
width: width * 0.75,
24
height: height,
25
webPreferences: {
26
nodeIntegration: true,
27
contextIsolation: false
28
}
29
});
30
mainWindow.loadURL(`file://${__dirname}/simulationWorkbench.html`);
31
mainWindow.on('closed', function () {
32
mainWindow = null;
33
});
34
}
35
36
app.on('ready', () => {
37
if (process.argv.includes('--help')) {
38
console.log(`Options:
39
--run-dir=DIRNAME Provide the run output directory name, e.g., 'out-20231201-151346'.
40
--grep=STRING Pre-populates simulation workbench 'grep' input box.`);
41
app.quit();
42
}
43
registerListeners();
44
createWindow();
45
});
46
47
app.on('window-all-closed', function () {
48
if (process.platform !== 'darwin') {
49
app.quit();
50
}
51
});
52
53
app.on('activate', function () {
54
if (mainWindow === null) {
55
createWindow();
56
}
57
});
58
59
// change to configure logging, e.g., to `console.debug`
60
const log = {
61
debug: (..._args) => { }
62
};
63
64
function registerListeners() {
65
66
/** @type {Map<string, child_process.ChildProcess>} */
67
const spawnedProcesses = new Map();
68
69
/**
70
* Spawns a new child process and sets up listeners for its stdout, stderr, and exit events.
71
*
72
* @param {Object} event - The event object from the Electron IPC.
73
* @param {Object} options - The options for the child process.
74
* @param {string} options.id - The unique identifier for the child process created by the renderer process.
75
* @param {Array<string>} options.processArgs - The arguments to pass to the child process.
76
*/
77
function spawnProcess(event, { id, processArgs }) {
78
79
log.debug(`main process: spawn-process (id: ${id}, processArgs: ${JSON.stringify(processArgs)})`);
80
81
const child = child_process.spawn(
82
'node',
83
[path.join(__dirname, '../../dist', 'simulationMain.js'), ...processArgs],
84
{ stdio: 'pipe' }
85
);
86
if (child.pid) {
87
child.stdout.setEncoding('utf8');
88
child.stdout.on('data', (data) => {
89
log.debug(`main process: stdout: ${data.toString()}`);
90
event.sender.send('stdout-data', { id, data });
91
});
92
child.stderr.setEncoding('utf8');
93
child.stderr.on('data', (data) => {
94
log.debug(`main process: stderr: ${data.toString()}`);
95
event.sender.send('stderr-data', { id, data });
96
});
97
child.on('exit', (code) => {
98
log.debug('main process: ' + JSON.stringify(code, null, '\t'));
99
spawnedProcesses.delete(id);
100
event.sender.send('process-exit', { id, code });
101
});
102
spawnedProcesses.set(id, child);
103
}
104
}
105
106
electron.ipcMain.on('spawn-process', spawnProcess);
107
108
electron.ipcMain.on('kill-process', (_event, { id }) => {
109
spawnedProcesses.get(id)?.kill('SIGTERM');
110
spawnedProcesses.delete(id);
111
});
112
113
electron.ipcMain.on('open-link', (_event, url) => {
114
electron.shell.openExternal(url);
115
});
116
117
electron.ipcMain.handle('processArgv', () => {
118
return process.argv;
119
});
120
}
121
122