index.js 4.4 KB

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