|
| 1 | +'use strict'; |
| 2 | +const electron = require('electron'); |
| 3 | +const app = electron.app; // Module to control application life. |
| 4 | + |
| 5 | +const handleSetupEvent = function() { |
| 6 | + if (process.argv.length === 1) { |
| 7 | + return false; |
| 8 | + } |
| 9 | + |
| 10 | + const ChildProcess = require('child_process'); |
| 11 | + const path = require('path'); |
| 12 | + |
| 13 | + const appFolder = path.resolve(process.execPath, '..'); |
| 14 | + const rootAtomFolder = path.resolve(appFolder, '..'); |
| 15 | + const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe')); |
| 16 | + const exeName = path.basename(process.execPath); |
| 17 | + |
| 18 | + const spawn = function(command, args) { |
| 19 | + let spawnedProcess, error; |
| 20 | + |
| 21 | + try { |
| 22 | + spawnedProcess = ChildProcess.spawn(command, args, {detached: true}); |
| 23 | + } catch (error) {} |
| 24 | + |
| 25 | + return spawnedProcess; |
| 26 | + }; |
| 27 | + |
| 28 | + const spawnUpdate = function(args) { |
| 29 | + return spawn(updateDotExe, args); |
| 30 | + }; |
| 31 | + |
| 32 | + const squirrelEvent = process.argv[1]; |
| 33 | + switch (squirrelEvent) { |
| 34 | + case '--squirrel-install': |
| 35 | + case '--squirrel-updated': |
| 36 | + // Optionally do things such as: |
| 37 | + // |
| 38 | + // - Install desktop and start menu shortcuts |
| 39 | + // - Add your .exe to the PATH |
| 40 | + // - Write to the registry for things like file associations and |
| 41 | + // explorer context menus |
| 42 | + |
| 43 | + spawnUpdate(['--createShortcut', exeName]); |
| 44 | + |
| 45 | + setTimeout(app.quit, 1000); |
| 46 | + return true; |
| 47 | + |
| 48 | + case '--squirrel-uninstall': |
| 49 | + // Undo anything you did in the --squirrel-install and |
| 50 | + // --squirrel-updated handlers |
| 51 | + |
| 52 | + spawnUpdate(['--removeShortcut', exeName]); |
| 53 | + |
| 54 | + setTimeout(app.quit, 1000); |
| 55 | + return true; |
| 56 | + |
| 57 | + case '--squirrel-obsolete': |
| 58 | + // This is called on the outgoing version of your app before |
| 59 | + // we update to the new version - it's the opposite of |
| 60 | + // --squirrel-updated |
| 61 | + |
| 62 | + setTimeout(app.quit, 1000); |
| 63 | + return true; |
| 64 | + } |
| 65 | +}; |
| 66 | +if (handleSetupEvent()) { |
| 67 | + return; |
| 68 | +} |
| 69 | + |
| 70 | +// Quit when all windows are closed. |
| 71 | +app.on('window-all-closed', function() { |
| 72 | + app.quit(); |
| 73 | +}); |
| 74 | + |
| 75 | +let mainWindow; |
| 76 | + |
| 77 | +// This method will be called when Electron has finished |
| 78 | +// initialization and is ready to create browser windows. |
| 79 | +app.on('ready', function() { |
| 80 | + const protocol = electron.protocol; |
| 81 | + protocol.registerFileProtocol('app', function(request, callback) { |
| 82 | + let url = request.url.substr(12); |
| 83 | + callback({path: require('path').normalize(__dirname + '/birdex.asar/' + url)}); |
| 84 | + }, function (error) { |
| 85 | + if (error) |
| 86 | + console.error('Failed to register protocol') |
| 87 | + }); |
| 88 | + |
| 89 | + // bugfix for <img ng-src> requesting unsafe:app://birdex/path |
| 90 | + protocol.registerFileProtocol('unsafe', function(request, callback) { |
| 91 | + let url = request.url.substr(19); |
| 92 | + callback({path: require('path').normalize(__dirname + '/birdex.asar/' + url)}); |
| 93 | + }, function (error) { |
| 94 | + if (error) |
| 95 | + console.error('Failed to register protocol') |
| 96 | + }); |
| 97 | + |
| 98 | + // Create the browser window. |
| 99 | + mainWindow = new electron.BrowserWindow({ |
| 100 | + width: 1280 |
| 101 | + , height: 720 |
| 102 | + , minHeight: 720 |
| 103 | + , minWidth: 1024 |
| 104 | + , 'web-preferences': { |
| 105 | + 'web-security': false |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + // and load the index.html of the app. |
| 110 | + mainWindow.loadURL('app://birdex/index.html'); |
| 111 | + // mainWindow.loadURL('http://new.001.birdex.org/'); |
| 112 | + // mainWindow.loadURL('http://oleg.dev:8000/index-dev.html'); |
| 113 | + // mainWindow.loadURL('http://oleg.dev:8000/index.html'); |
| 114 | + |
| 115 | + // Open the DevTools. |
| 116 | + // mainWindow.webContents.openDevTools(); |
| 117 | + |
| 118 | + // Emitted when the window is closed. |
| 119 | + mainWindow.on('closed', function() { |
| 120 | + // Dereference the window object, usually you would store windows |
| 121 | + // in an array if your app supports multi windows, this is the time |
| 122 | + // when you should delete the corresponding element. |
| 123 | + mainWindow = null; |
| 124 | + }); |
| 125 | +}); |
0 commit comments