index.js 3.9 KB

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