index.js 4.0 KB

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