1
0

index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. if (require('electron-squirrel-startup')) return;
  3. // this should be placed at top of main.js to handle setup events quickly
  4. if (handleSquirrelEvent()) {
  5. // squirrel event handled and app will exit in 1000ms, so don't do anything else
  6. return;
  7. }
  8. const electron = require('electron');
  9. const app = electron.app; // Module to control application life.
  10. const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
  11. // Keep a global reference of the window object, if you don't, the window will
  12. // be closed automatically when the JavaScript object is garbage collected.
  13. var mainWindow = null;
  14. // Quit when all windows are closed.
  15. app.on('window-all-closed', function() {
  16. // On OS X it is common for applications and their menu bar
  17. // to stay active until the user quits explicitly with Cmd + Q
  18. if (process.platform != 'darwin') {
  19. app.quit();
  20. }
  21. });
  22. // This method will be called when Electron has finished
  23. // initialization and is ready to create browser windows.
  24. app.on('ready', function() {
  25. // Create the browser window.
  26. mainWindow = new BrowserWindow({
  27. width: 520,
  28. height: 300,
  29. 'min-width': 520,
  30. 'min-height': 300,
  31. 'accept-first-mouse': true
  32. });
  33. // and load the index.html of the app.
  34. mainWindow.loadURL('file://' + __dirname + '/front-end/index.html');
  35. // Open the DevTools.
  36. // mainWindow.webContents.openDevTools();
  37. // Emitted when the window is closed.
  38. mainWindow.on('closed', function() {
  39. // Dereference the window object, usually you would store windows
  40. // in an array if your app supports multi windows, this is the time
  41. // when you should delete the corresponding element.
  42. mainWindow = null;
  43. });
  44. });
  45. function handleSquirrelEvent() {
  46. if (process.argv.length === 1) {
  47. return false;
  48. }
  49. const ChildProcess = require('child_process');
  50. const path = require('path');
  51. const appFolder = path.resolve(process.execPath, '..');
  52. const rootAtomFolder = path.resolve(appFolder, '..');
  53. const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
  54. const exeName = path.basename(process.execPath);
  55. const spawn = function(command, args) {
  56. let spawnedProcess, error;
  57. try {
  58. spawnedProcess = ChildProcess.spawn(command, args, {detached: true});
  59. } catch (error) {}
  60. return spawnedProcess;
  61. };
  62. const spawnUpdate = function(args) {
  63. return spawn(updateDotExe, args);
  64. };
  65. const squirrelEvent = process.argv[1];
  66. switch (squirrelEvent) {
  67. case '--squirrel-install':
  68. case '--squirrel-updated':
  69. // Optionally do things such as:
  70. // - Add your .exe to the PATH
  71. // - Write to the registry for things like file associations and
  72. // explorer context menus
  73. // Install desktop and start menu shortcuts
  74. spawnUpdate(['--createShortcut', exeName]);
  75. setTimeout(app.quit, 1000);
  76. return true;
  77. case '--squirrel-uninstall':
  78. // Undo anything you did in the --squirrel-install and
  79. // --squirrel-updated handlers
  80. // Remove desktop and start menu shortcuts
  81. spawnUpdate(['--removeShortcut', exeName]);
  82. setTimeout(app.quit, 1000);
  83. return true;
  84. case '--squirrel-obsolete':
  85. // This is called on the outgoing version of your app before
  86. // we update to the new version - it's the opposite of
  87. // --squirrel-updated
  88. app.quit();
  89. return true;
  90. }
  91. };