2016-01-24 21:20:29 +08:00
|
|
|
import 'source-map-support/register';
|
2016-01-29 22:04:41 +08:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2017-04-29 22:52:12 +08:00
|
|
|
import { app, crashReporter } from 'electron';
|
|
|
|
import electronDownload from 'electron-dl';
|
|
|
|
|
2016-01-29 22:04:41 +08:00
|
|
|
import createLoginWindow from './components/login/loginWindow';
|
|
|
|
import createMainWindow from './components/mainWindow/mainWindow';
|
2017-10-06 01:32:48 +02:00
|
|
|
import createTrayIcon from './components/trayIcon/trayIcon';
|
2016-01-29 22:04:41 +08:00
|
|
|
import helpers from './helpers/helpers';
|
2016-02-23 16:46:14 +08:00
|
|
|
import inferFlash from './helpers/inferFlash';
|
2016-01-24 21:20:29 +08:00
|
|
|
|
2017-04-29 22:52:12 +08:00
|
|
|
const { isOSX } = helpers;
|
2016-01-19 19:53:10 +08:00
|
|
|
|
2016-05-26 22:50:40 +08:00
|
|
|
electronDownload();
|
|
|
|
|
2016-01-22 19:57:39 +08:00
|
|
|
const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json');
|
2016-01-29 22:04:41 +08:00
|
|
|
const appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
|
2015-07-06 10:31:09 +08:00
|
|
|
|
2017-08-16 09:20:43 -05:00
|
|
|
if (appArgs.processEnvs) {
|
|
|
|
Object.keys(appArgs.processEnvs).forEach((key) => {
|
|
|
|
/* eslint-env node */
|
|
|
|
process.env[key] = appArgs.processEnvs[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-29 22:04:41 +08:00
|
|
|
let mainWindow;
|
2016-01-23 10:09:47 +08:00
|
|
|
|
2016-03-26 15:06:50 +08:00
|
|
|
if (typeof appArgs.flashPluginDir === 'string') {
|
2017-04-29 22:52:12 +08:00
|
|
|
app.commandLine.appendSwitch('ppapi-flash-path', appArgs.flashPluginDir);
|
2016-03-26 15:06:50 +08:00
|
|
|
} else if (appArgs.flashPluginDir) {
|
2017-04-29 22:52:12 +08:00
|
|
|
const flashPath = inferFlash();
|
|
|
|
app.commandLine.appendSwitch('ppapi-flash-path', flashPath);
|
2016-02-23 16:46:14 +08:00
|
|
|
}
|
|
|
|
|
2016-02-23 21:31:47 +08:00
|
|
|
if (appArgs.ignoreCertificate) {
|
2017-04-29 22:52:12 +08:00
|
|
|
app.commandLine.appendSwitch('ignore-certificate-errors');
|
2016-01-25 23:42:28 +08:00
|
|
|
}
|
|
|
|
|
2017-07-13 17:23:07 +01:00
|
|
|
if (appArgs.ignoreGpuBlacklist) {
|
|
|
|
app.commandLine.appendSwitch('ignore-gpu-blacklist');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appArgs.enableEs3Apis) {
|
|
|
|
app.commandLine.appendSwitch('enable-es3-apis');
|
|
|
|
}
|
|
|
|
|
2017-07-05 15:07:31 +02:00
|
|
|
if (appArgs.diskCacheSize) {
|
|
|
|
app.commandLine.appendSwitch('disk-cache-size', appArgs.diskCacheSize);
|
|
|
|
}
|
|
|
|
|
2017-10-05 18:44:03 -04:00
|
|
|
if (appArgs.basicAuthUsername) {
|
|
|
|
app.commandLine.appendSwitch('basic-auth-username', appArgs.basicAuthUsername);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appArgs.basicAuthPassword) {
|
|
|
|
app.commandLine.appendSwitch('basic-auth-password', appArgs.basicAuthPassword);
|
|
|
|
}
|
|
|
|
|
2016-01-23 15:12:53 +08:00
|
|
|
// do nothing for setDockBadge if not OSX
|
|
|
|
let setDockBadge = () => {};
|
2016-01-24 02:02:23 +08:00
|
|
|
|
2016-01-23 15:12:53 +08:00
|
|
|
if (isOSX()) {
|
2017-04-29 22:52:12 +08:00
|
|
|
setDockBadge = app.dock.setBadge;
|
2016-01-23 15:12:53 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 22:04:41 +08:00
|
|
|
app.on('window-all-closed', () => {
|
2017-04-29 22:52:12 +08:00
|
|
|
if (!isOSX() || appArgs.fastQuit) {
|
|
|
|
app.quit();
|
|
|
|
}
|
2015-07-05 14:08:13 +08:00
|
|
|
});
|
|
|
|
|
2016-01-29 22:04:41 +08:00
|
|
|
app.on('activate', (event, hasVisibleWindows) => {
|
2017-04-29 22:52:12 +08:00
|
|
|
if (isOSX()) {
|
2017-11-14 08:05:01 -05:00
|
|
|
// this is called when the dock is clicked
|
2017-04-29 22:52:12 +08:00
|
|
|
if (!hasVisibleWindows) {
|
|
|
|
mainWindow.show();
|
2016-01-21 12:25:33 +08:00
|
|
|
}
|
2017-04-29 22:52:12 +08:00
|
|
|
}
|
2016-01-21 12:25:33 +08:00
|
|
|
});
|
|
|
|
|
2016-01-29 22:04:41 +08:00
|
|
|
app.on('before-quit', () => {
|
2017-04-29 22:52:12 +08:00
|
|
|
// not fired when the close button on the window is clicked
|
|
|
|
if (isOSX()) {
|
|
|
|
// need to force a quit as a workaround here to simulate the osx app hiding behaviour
|
|
|
|
// Somehow sokution at https://github.com/atom/electron/issues/444#issuecomment-76492576 does not work,
|
|
|
|
// e.prevent default appears to persist
|
|
|
|
|
|
|
|
// might cause issues in the future as before-quit and will-quit events are not called
|
|
|
|
app.exit(0);
|
|
|
|
}
|
2016-01-21 12:25:33 +08:00
|
|
|
});
|
|
|
|
|
2016-10-09 13:52:50 +08:00
|
|
|
if (appArgs.crashReporter) {
|
2017-04-29 22:52:12 +08:00
|
|
|
app.on('will-finish-launching', () => {
|
|
|
|
crashReporter.start({
|
2017-07-21 14:55:39 -05:00
|
|
|
companyName: appArgs.companyName || '',
|
2017-04-29 22:52:12 +08:00
|
|
|
productName: appArgs.name,
|
|
|
|
submitURL: appArgs.crashReporter,
|
|
|
|
autoSubmit: true,
|
2016-10-09 13:52:50 +08:00
|
|
|
});
|
2017-04-29 22:52:12 +08:00
|
|
|
});
|
2016-10-09 13:52:50 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 22:04:41 +08:00
|
|
|
app.on('ready', () => {
|
2017-04-29 22:52:12 +08:00
|
|
|
mainWindow = createMainWindow(appArgs, app.quit, setDockBadge);
|
2017-10-06 01:32:48 +02:00
|
|
|
createTrayIcon(appArgs, mainWindow);
|
2015-07-06 09:38:04 +08:00
|
|
|
});
|
2015-07-05 14:08:13 +08:00
|
|
|
|
2016-01-29 22:04:41 +08:00
|
|
|
app.on('login', (event, webContents, request, authInfo, callback) => {
|
2017-11-14 08:05:01 -05:00
|
|
|
// for http authentication
|
2017-04-29 22:52:12 +08:00
|
|
|
event.preventDefault();
|
2017-10-05 18:44:03 -04:00
|
|
|
|
|
|
|
if (appArgs.basicAuthUsername !== null && appArgs.basicAuthPassword !== null) {
|
|
|
|
callback(appArgs.basicAuthUsername, appArgs.basicAuthPassword);
|
|
|
|
} else {
|
|
|
|
createLoginWindow(callback);
|
|
|
|
}
|
2016-01-22 02:39:52 +08:00
|
|
|
});
|
2017-04-10 04:02:49 +02:00
|
|
|
|
|
|
|
if (appArgs.singleInstance) {
|
2017-04-29 22:52:12 +08:00
|
|
|
const shouldQuit = app.makeSingleInstance(() => {
|
|
|
|
// Someone tried to run a second instance, we should focus our window.
|
|
|
|
if (mainWindow) {
|
|
|
|
if (mainWindow.isMinimized()) {
|
|
|
|
mainWindow.restore();
|
|
|
|
}
|
|
|
|
mainWindow.focus();
|
2017-04-10 04:02:49 +02:00
|
|
|
}
|
2017-04-29 22:52:12 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (shouldQuit) {
|
|
|
|
app.quit();
|
|
|
|
}
|
2017-04-10 04:02:49 +02:00
|
|
|
}
|