1
0

index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. const checkDialout = require("./back-end/checkDialout");
  12. const ipcHandlers = require('./back-end/ipcHandlers');
  13. const isProd = process.execPath.search('electron-prebuilt') === -1;
  14. // Keep a global reference of the window object, if you don't, the window will
  15. // be closed automatically when the JavaScript object is garbage collected.
  16. let mainWindow = null;
  17. // Quit when all windows are closed.
  18. app.on('window-all-closed', () => {
  19. // On OS X it is common for applications and their menu bar
  20. // to stay active until the user quits explicitly with Cmd + Q
  21. if (process.platform !== 'darwin') {
  22. app.quit();
  23. }
  24. });
  25. function launchApp() {
  26. //Clears any crusty downloads/API calls
  27. mainWindow.webContents.session.clearCache(() => {
  28. ipcHandlers();
  29. // load the index.html of the app.
  30. if (!isProd) {
  31. mainWindow.loadURL('http://localhost:3000/');
  32. } else {
  33. mainWindow.loadURL('file://' + __dirname + '/front-end/index.html');
  34. }
  35. });
  36. }
  37. function launchLinuxHelper() {
  38. // load the linux-help.html of the app.
  39. mainWindow.loadURL('file://' + __dirname + '/front-end/linux-help.html');
  40. }
  41. // This method will be called when Electron has finished
  42. // initialization and is ready to create browser windows.
  43. app.on('ready', function () {
  44. // Create the browser window.
  45. mainWindow = new BrowserWindow({
  46. width: 520,
  47. height: 300,
  48. 'min-width': 520,
  49. 'min-height': 300,
  50. 'accept-first-mouse': true
  51. });
  52. if (process.platform === "linux") {
  53. checkDialout(launchApp, err => {
  54. if (err.message === checkDialout.ERROR_MESSAGES.USER_NOT_IN_DIALOUT) {
  55. launchLinuxHelper();
  56. } else {
  57. //TODO: When another error occurs propogate the error somehow.
  58. launchApp();
  59. }
  60. });
  61. } else {
  62. launchApp();
  63. }
  64. // Open the DevTools.
  65. if (!isProd) mainWindow.webContents.openDevTools();
  66. // Emitted when the window is closed.
  67. mainWindow.on('closed', () => {
  68. // Dereference the window object, usually you would store windows
  69. // in an array if your app supports multi windows, this is the time
  70. // when you should delete the corresponding element.
  71. mainWindow = null;
  72. });
  73. });
  74. function handleSquirrelEvent() {
  75. if (process.argv.length === 1) {
  76. return false;
  77. }
  78. const ChildProcess = require('child_process');
  79. const path = require('path');
  80. const appFolder = path.resolve(process.execPath, '..');
  81. const rootAtomFolder = path.resolve(appFolder, '..');
  82. const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
  83. const exeName = path.basename(process.execPath);
  84. const spawn = function (command, args) {
  85. let spawnedProcess, error;
  86. try {
  87. spawnedProcess = ChildProcess.spawn(command, args, { detached: true });
  88. } catch (error) { }
  89. return spawnedProcess;
  90. };
  91. const spawnUpdate = function (args) {
  92. return spawn(updateDotExe, args);
  93. };
  94. const squirrelEvent = process.argv[1];
  95. switch (squirrelEvent) {
  96. case '--squirrel-install':
  97. case '--squirrel-updated':
  98. // Optionally do things such as:
  99. // - Add your .exe to the PATH
  100. // - Write to the registry for things like file associations and
  101. // explorer context menus
  102. // Install desktop and start menu shortcuts
  103. spawnUpdate(['--createShortcut', exeName]);
  104. setTimeout(app.quit, 1000);
  105. return true;
  106. case '--squirrel-uninstall':
  107. // Undo anything you did in the --squirrel-install and
  108. // --squirrel-updated handlers
  109. // Remove desktop and start menu shortcuts
  110. spawnUpdate(['--removeShortcut', exeName]);
  111. setTimeout(app.quit, 1000);
  112. return true;
  113. case '--squirrel-obsolete':
  114. // This is called on the outgoing version of your app before
  115. // we update to the new version - it's the opposite of
  116. // --squirrel-updated
  117. app.quit();
  118. return true;
  119. }
  120. }