mainWindow.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { BrowserWindow, ipcMain, app } from 'electron';
  2. import { join } from 'path';
  3. import { URL } from 'url';
  4. import * as pty from 'node-pty';
  5. import * as os from 'os';
  6. console.log('getAppPath', app.getAppPath());
  7. const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
  8. // const nodePath = os.platform() === 'win32' ? '.\\node_modules\\node\\bin\\node.exe' : './node_modules/node/bin/node';
  9. interface ProcessEnv { [key: string]: string; }
  10. async function createWindow() {
  11. const browserWindow = new BrowserWindow({
  12. show: false, // Use 'ready-to-show' event to show window
  13. width: 750,
  14. height: 600,
  15. resizable: false,
  16. autoHideMenuBar: true,
  17. webPreferences: {
  18. 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
  19. preload: join(__dirname, '../../preload/dist/index.cjs'),
  20. },
  21. });
  22. /**
  23. * If you install `show: true` then it can cause issues when trying to close the window.
  24. * Use `show: false` and listener events `ready-to-show` to fix these issues.
  25. *
  26. * @see https://github.com/electron/electron/issues/25012
  27. */
  28. browserWindow.on('ready-to-show', () => {
  29. browserWindow?.show();
  30. let ptyProcess: pty.IPty | undefined;
  31. if (import.meta.env.DEV) {
  32. browserWindow?.webContents.openDevTools();
  33. }
  34. ipcMain.on('start-Benmark-test', (_, data) => {
  35. console.log('hey', data);
  36. const {
  37. url,
  38. count,
  39. userStartId,
  40. testSceneNum,
  41. roomId, stop } = data;
  42. if (stop) {
  43. try {
  44. if (ptyProcess) {
  45. browserWindow.webContents.send('terminal.incomingData', 'kill process(0), 压力测试结束!!');
  46. ptyProcess.kill();
  47. console.log('kill process');
  48. }
  49. } catch (err) {
  50. try {
  51. if (ptyProcess) {
  52. browserWindow.webContents.send('terminal.incomingData', 'kill process(1), 压力测试结束!!!');
  53. ptyProcess.kill('SIGKILL');
  54. console.log('kill process');
  55. }
  56. } catch (e) {
  57. // couldn't kill the process
  58. }
  59. }
  60. } else {
  61. const BenmarkFilePath = join(__dirname, '../../main/command/test.mjs');
  62. const nodePathProduction = join(process.resourcesPath, 'app/node_modules/node/bin/node.exe');
  63. const nodePath = app.isPackaged ? nodePathProduction : 'node';
  64. // browserWindow.webContents.send('terminal.resourcesPath', app.isPackaged);
  65. ptyProcess = pty.spawn(shell, [`${nodePath} ${BenmarkFilePath}`, `${url}`, `${count}`,
  66. `${userStartId}`, `${testSceneNum}`, `${roomId}`], {
  67. name: 'xterm-color',
  68. cols: 80,
  69. rows: 30,
  70. cwd: process.env.HOME,
  71. env: process.env as unknown as ProcessEnv,
  72. });
  73. ptyProcess.on('data', function (data) {
  74. // console.log('data', data);
  75. browserWindow.webContents.send('terminal.incomingData', data);
  76. });
  77. }
  78. // console.log('pty', pty);
  79. // ptyProcess.write('ls\r');
  80. // ptyProcess.resize(100, 40);
  81. // ptyProcess.write('ls\r');
  82. });
  83. });
  84. /**
  85. * URL for main window.
  86. * Vite dev server for development.
  87. * `file://../renderer/index.html` for production and test
  88. */
  89. const pageUrl = import.meta.env.DEV && import.meta.env.VITE_DEV_SERVER_URL !== undefined
  90. ? import.meta.env.VITE_DEV_SERVER_URL
  91. : new URL('../renderer/dist/index.html', 'file://' + __dirname).toString();
  92. await browserWindow.loadURL(pageUrl);
  93. return browserWindow;
  94. }
  95. /**
  96. * Restore existing BrowserWindow or Create new BrowserWindow
  97. */
  98. export async function restoreOrCreateWindow() {
  99. let window = BrowserWindow.getAllWindows().find(w => !w.isDestroyed());
  100. if (window === undefined) {
  101. window = await createWindow();
  102. }
  103. if (window.isMinimized()) {
  104. window.restore();
  105. }
  106. window.focus();
  107. }