1 | import 'source-map-support/register';
|
2 | import fs from 'fs';
|
3 | import path from 'path';
|
4 | import { app, crashReporter, globalShortcut } from 'electron';
|
5 | import electronDownload from 'electron-dl';
|
6 |
|
7 | import createLoginWindow from './components/login/loginWindow';
|
8 | import createMainWindow from './components/mainWindow/mainWindow';
|
9 | import createTrayIcon from './components/trayIcon/trayIcon';
|
10 | import helpers from './helpers/helpers';
|
11 | import inferFlash from './helpers/inferFlash';
|
12 |
|
13 | const electronSquirrelStartup = require('electron-squirrel-startup');
|
14 |
|
15 |
|
16 |
|
17 | if (electronSquirrelStartup) {
|
18 | app.exit();
|
19 | }
|
20 |
|
21 | const { isOSX } = helpers;
|
22 |
|
23 | const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json');
|
24 | const appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
|
25 |
|
26 | const fileDownloadOptions = Object.assign({}, appArgs.fileDownloadOptions);
|
27 | electronDownload(fileDownloadOptions);
|
28 |
|
29 | if (appArgs.processEnvs) {
|
30 | Object.keys(appArgs.processEnvs).forEach((key) => {
|
31 |
|
32 | process.env[key] = appArgs.processEnvs[key];
|
33 | });
|
34 | }
|
35 |
|
36 | let mainWindow;
|
37 |
|
38 | if (typeof appArgs.flashPluginDir === 'string') {
|
39 | app.commandLine.appendSwitch('ppapi-flash-path', appArgs.flashPluginDir);
|
40 | } else if (appArgs.flashPluginDir) {
|
41 | const flashPath = inferFlash();
|
42 | app.commandLine.appendSwitch('ppapi-flash-path', flashPath);
|
43 | }
|
44 |
|
45 | if (appArgs.ignoreCertificate) {
|
46 | app.commandLine.appendSwitch('ignore-certificate-errors');
|
47 | }
|
48 |
|
49 | if (appArgs.disableGpu) {
|
50 | app.disableHardwareAcceleration();
|
51 | }
|
52 |
|
53 | if (appArgs.ignoreGpuBlacklist) {
|
54 | app.commandLine.appendSwitch('ignore-gpu-blacklist');
|
55 | }
|
56 |
|
57 | if (appArgs.enableEs3Apis) {
|
58 | app.commandLine.appendSwitch('enable-es3-apis');
|
59 | }
|
60 |
|
61 | if (appArgs.diskCacheSize) {
|
62 | app.commandLine.appendSwitch('disk-cache-size', appArgs.diskCacheSize);
|
63 | }
|
64 |
|
65 | if (appArgs.basicAuthUsername) {
|
66 | app.commandLine.appendSwitch(
|
67 | 'basic-auth-username',
|
68 | appArgs.basicAuthUsername,
|
69 | );
|
70 | }
|
71 |
|
72 | if (appArgs.basicAuthPassword) {
|
73 | app.commandLine.appendSwitch(
|
74 | 'basic-auth-password',
|
75 | appArgs.basicAuthPassword,
|
76 | );
|
77 | }
|
78 |
|
79 |
|
80 | let setDockBadge = () => {};
|
81 |
|
82 | if (isOSX()) {
|
83 | let currentBadgeCount = 0;
|
84 |
|
85 | setDockBadge = (count, bounce = false) => {
|
86 | app.dock.setBadge(count);
|
87 | if (bounce && count > currentBadgeCount) app.dock.bounce();
|
88 | currentBadgeCount = count;
|
89 | };
|
90 | }
|
91 |
|
92 | app.on('window-all-closed', () => {
|
93 | if (!isOSX() || appArgs.fastQuit) {
|
94 | app.quit();
|
95 | }
|
96 | });
|
97 |
|
98 | app.on('activate', (event, hasVisibleWindows) => {
|
99 | if (isOSX()) {
|
100 |
|
101 | if (!hasVisibleWindows) {
|
102 | mainWindow.show();
|
103 | }
|
104 | }
|
105 | });
|
106 |
|
107 | app.on('before-quit', () => {
|
108 |
|
109 | if (isOSX()) {
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | app.exit(0);
|
116 | }
|
117 | });
|
118 |
|
119 | if (appArgs.crashReporter) {
|
120 | app.on('will-finish-launching', () => {
|
121 | crashReporter.start({
|
122 | companyName: appArgs.companyName || '',
|
123 | productName: appArgs.name,
|
124 | submitURL: appArgs.crashReporter,
|
125 | uploadToServer: true,
|
126 | });
|
127 | });
|
128 | }
|
129 |
|
130 |
|
131 | const shouldQuit = appArgs.singleInstance && !app.requestSingleInstanceLock();
|
132 | if (shouldQuit) {
|
133 | app.quit();
|
134 | } else {
|
135 | app.on('second-instance', () => {
|
136 | if (mainWindow) {
|
137 | if (!mainWindow.isVisible()) {
|
138 |
|
139 | mainWindow.show();
|
140 | }
|
141 | if (mainWindow.isMinimized()) {
|
142 |
|
143 | mainWindow.restore();
|
144 | }
|
145 | mainWindow.focus();
|
146 | }
|
147 | });
|
148 |
|
149 | app.on('ready', () => {
|
150 | mainWindow = createMainWindow(appArgs, app.quit, setDockBadge);
|
151 | createTrayIcon(appArgs, mainWindow);
|
152 |
|
153 |
|
154 | if (appArgs.globalShortcuts) {
|
155 | appArgs.globalShortcuts.forEach((shortcut) => {
|
156 | globalShortcut.register(shortcut.key, () => {
|
157 | shortcut.inputEvents.forEach((inputEvent) => {
|
158 | mainWindow.webContents.sendInputEvent(inputEvent);
|
159 | });
|
160 | });
|
161 | });
|
162 | }
|
163 | });
|
164 | }
|
165 |
|
166 | app.on('new-window-for-tab', () => {
|
167 | mainWindow.emit('new-tab');
|
168 | });
|
169 |
|
170 | app.on('login', (event, webContents, request, authInfo, callback) => {
|
171 |
|
172 | event.preventDefault();
|
173 |
|
174 | if (
|
175 | appArgs.basicAuthUsername !== null &&
|
176 | appArgs.basicAuthPassword !== null
|
177 | ) {
|
178 | callback(appArgs.basicAuthUsername, appArgs.basicAuthPassword);
|
179 | } else {
|
180 | createLoginWindow(callback);
|
181 | }
|
182 | });
|