electron-starter.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const electron = require('electron');
  2. const fs = require('fs');
  3. // setup server port in order to avoid port conflict
  4. process.env.SERVER_PORT = 3080;
  5. // launch server
  6. require('./dist/src/app');
  7. // Module to control application life.
  8. const app = electron.app;
  9. // Module to create native browser window.
  10. const BrowserWindow = electron.BrowserWindow;
  11. const path = require('path');
  12. const url = require('url');
  13. // Keep a global reference of the window object, if you don't, the window will
  14. // be closed automatically when the JavaScript object is garbage collected.
  15. let mainWindow;
  16. function createWindow() {
  17. // Create the browser window.
  18. mainWindow = new BrowserWindow({
  19. width: 1200,
  20. height: 800,
  21. webPreferences: {
  22. // devTools: true,
  23. webSecurity: false,
  24. nodeIntegration: true,
  25. enableRemoteModule: true,
  26. },
  27. });
  28. // replace IS_ELECTRON to yes, beacause axios relative url
  29. const envConfig = path.join(__dirname, './build/env-config.js');
  30. fs.readFile(envConfig, 'utf8', function (err, files) {
  31. const result = files.replace(/{{IS_ELECTRON}}/g, 'yes');
  32. fs.writeFile(envConfig, result, 'utf8', function (err) {
  33. if (err) {
  34. console.log(err);
  35. }
  36. });
  37. });
  38. // get system lang
  39. mainWindow.webContents.executeJavaScript(`
  40. localStorage.setItem('lang', '${app.getLocale()}');
  41. `);
  42. // mainWindow.maximize();
  43. // mainWindow.show();
  44. // and load the index.html of the app.
  45. const startUrl =
  46. process.env.ELECTRON_START_URL ||
  47. url.format({
  48. pathname: path.join(__dirname, './build/index.html'),
  49. protocol: 'file:',
  50. slashes: true,
  51. });
  52. mainWindow.loadURL(startUrl);
  53. // mainWindow.loadURL('http://127.0.0.1:3000');
  54. mainWindow.focus();
  55. // Emitted when the window is closed.
  56. mainWindow.on('closed', function () {
  57. // Dereference the window object, usually you would store windows
  58. // in an array if your app supports multi windows, this is the time
  59. // when you should delete the corresponding element.
  60. mainWindow = null;
  61. });
  62. }
  63. // This method will be called when Electron has finished
  64. // initialization and is ready to create browser windows.
  65. // Some APIs can only be used after this event occurs.
  66. app.on('ready', createWindow);
  67. // Quit when all windows are closed.
  68. app.on('window-all-closed', function () {
  69. // On OS X it is common for applications and their menu bar
  70. // to stay active until the user quits explicitly with Cmd + Q
  71. if (process.platform !== 'darwin') {
  72. app.quit();
  73. }
  74. });
  75. app.on('activate', function () {
  76. // On OS X it's common to re-create a window in the app when the
  77. // dock icon is clicked and there are no other windows open.
  78. if (mainWindow === null) {
  79. createWindow();
  80. }
  81. });