electron-starter.js 3.3 KB

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