123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- import { BrowserWindow, ipcMain, app } from 'electron';
- import { join } from 'path';
- import { URL } from 'url';
- import * as pty from 'node-pty';
- import * as os from 'os';
- console.log('getAppPath', app.getAppPath());
- const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
- // const nodePath = os.platform() === 'win32' ? '.\\node_modules\\node\\bin\\node.exe' : './node_modules/node/bin/node';
- interface ProcessEnv { [key: string]: string; }
- async function createWindow() {
- const browserWindow = new BrowserWindow({
- show: false, // Use 'ready-to-show' event to show window
- width: 750,
- height: 600,
- resizable: false,
- autoHideMenuBar: true,
- webPreferences: {
- webviewTag: false, // The webview tag is not recommended. Consider alternatives like iframe or Electron's BrowserView. https://www.electronjs.org/docs/latest/api/webview-tag#warning
- preload: join(__dirname, '../../preload/dist/index.cjs'),
- },
- });
- /**
- * If you install `show: true` then it can cause issues when trying to close the window.
- * Use `show: false` and listener events `ready-to-show` to fix these issues.
- *
- * @see https://github.com/electron/electron/issues/25012
- */
- browserWindow.on('ready-to-show', () => {
- browserWindow?.show();
- let ptyProcess: pty.IPty | undefined;
- if (import.meta.env.DEV) {
- browserWindow?.webContents.openDevTools();
- }
- ipcMain.on('start-Benmark-test', (_, data) => {
- console.log('hey', data);
- const {
- url,
- count,
- userStartId,
- testSceneNum,
- roomId, stop } = data;
- if (stop) {
- try {
- if (ptyProcess) {
- browserWindow.webContents.send('terminal.incomingData', 'kill process(0), 压力测试结束!!');
- ptyProcess.kill();
- console.log('kill process');
- }
- } catch (err) {
- try {
- if (ptyProcess) {
- browserWindow.webContents.send('terminal.incomingData', 'kill process(1), 压力测试结束!!!');
- ptyProcess.kill('SIGKILL');
- console.log('kill process');
- }
- } catch (e) {
- // couldn't kill the process
- }
- }
- } else {
- const BenmarkFilePath = join(__dirname, '../../main/command/test.mjs');
- const nodePathProduction = join(process.resourcesPath, 'app/node_modules/node/bin/node.exe');
- const nodePath = app.isPackaged ? nodePathProduction : 'node';
- // browserWindow.webContents.send('terminal.resourcesPath', app.isPackaged);
- ptyProcess = pty.spawn(shell, [`${nodePath} ${BenmarkFilePath}`, `${url}`, `${count}`,
- `${userStartId}`, `${testSceneNum}`, `${roomId}`], {
- name: 'xterm-color',
- cols: 80,
- rows: 30,
- cwd: process.env.HOME,
- env: process.env as unknown as ProcessEnv,
- });
- ptyProcess.on('data', function (data) {
- // console.log('data', data);
- browserWindow.webContents.send('terminal.incomingData', data);
- });
- }
- // console.log('pty', pty);
- // ptyProcess.write('ls\r');
- // ptyProcess.resize(100, 40);
- // ptyProcess.write('ls\r');
- });
- });
- /**
- * URL for main window.
- * Vite dev server for development.
- * `file://../renderer/index.html` for production and test
- */
- const pageUrl = import.meta.env.DEV && import.meta.env.VITE_DEV_SERVER_URL !== undefined
- ? import.meta.env.VITE_DEV_SERVER_URL
- : new URL('../renderer/dist/index.html', 'file://' + __dirname).toString();
- await browserWindow.loadURL(pageUrl);
- return browserWindow;
- }
- /**
- * Restore existing BrowserWindow or Create new BrowserWindow
- */
- export async function restoreOrCreateWindow() {
- let window = BrowserWindow.getAllWindows().find(w => !w.isDestroyed());
- if (window === undefined) {
- window = await createWindow();
- }
- if (window.isMinimized()) {
- window.restore();
- }
- window.focus();
- }
|