2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-05-29 11:10:46 +00:00
nativefier/app/src/main.js

183 lines
4.7 KiB
JavaScript
Raw Normal View History

import 'source-map-support/register';
2016-01-29 14:04:41 +00:00
import fs from 'fs';
import path from 'path';
import { app, crashReporter, globalShortcut } from 'electron';
import electronDownload from 'electron-dl';
2016-01-29 14:04:41 +00:00
import createLoginWindow from './components/login/loginWindow';
import createMainWindow from './components/mainWindow/mainWindow';
import createTrayIcon from './components/trayIcon/trayIcon';
2016-01-29 14:04:41 +00:00
import helpers from './helpers/helpers';
import inferFlash from './helpers/inferFlash';
Support packaging nativefier applications into Squirrel-based installers (#744) [Squirrel](https://github.com/Squirrel/Squirrel.Windows) is *"an installation and update framework for Windows desktop apps "*. This PR adds `electron-squirrel-startup`, allowing to package nativefier applications into squirrel-based setup installers. Squirrel require this entrypoint to perform desktop and startup menu creations, without showing the UI on setup launches. - References: https://github.com/mongodb-js/electron-squirrel-startup - Resolves `electron-winstaller` and `electron-installer-windows` support of desktop / startup menu shortcuts for nativefier packaged applications. - The `electron-squirrel-startup` entrypoint has no effect on both Linux and Darwin, only on Windows - Supporting it directly inside `nativefier` avoids having to "hack" around the existing `main.js` and including dependencies from `electron-squirrel-startup` in an intermediate package to be included in a third layer for the final installer executable - The following script based on both `nativefier` and `electron-winstaller` templates represents a portable proof of concept for this merge request : ```js var nativefier = require('nativefier').default; var electronInstaller = require('electron-winstaller'); var options = { name: 'Web WhatsApp', targetUrl: 'http://web.whatsapp.com', platform: 'windows', arch: 'x64', version: '0.36.4', out: '.', overwrite: false, asar: false, counter: false, bounce: false, width: 1280, height: 800, showMenuBar: false, fastQuit: false, userAgent: 'Mozilla ...', ignoreCertificate: false, ignoreGpuBlacklist: false, enableEs3Apis: false, insecure: false, honest: false, zoom: 1.0, singleInstance: false, fileDownloadOptions: { saveAs: true }, processEnvs: { GOOGLE_API_KEY: '<your-google-api-key>' } }; nativefier(options, function(error, appPath) { if (error) { console.error(error); return; } console.log('App has been nativefied to', appPath); resultPromise = electronInstaller.createWindowsInstaller({ appDirectory: 'Web WhatsApp-win32-x64', outputDirectory: './', authors: 'Web WhatsApp', exe: 'Web WhatsApp.exe' }); resultPromise.then(() => console.log('It worked!'), e => console.log(`No dice: ${e.message}`)); }); ```
2019-02-08 14:57:32 +00:00
const electronSquirrelStartup = require('electron-squirrel-startup');
// Entrypoint for electron-squirrel-startup.
// See https://github.com/jiahaog/nativefier/pull/744 for sample use case
if (electronSquirrelStartup) {
app.exit();
Support packaging nativefier applications into Squirrel-based installers (#744) [Squirrel](https://github.com/Squirrel/Squirrel.Windows) is *"an installation and update framework for Windows desktop apps "*. This PR adds `electron-squirrel-startup`, allowing to package nativefier applications into squirrel-based setup installers. Squirrel require this entrypoint to perform desktop and startup menu creations, without showing the UI on setup launches. - References: https://github.com/mongodb-js/electron-squirrel-startup - Resolves `electron-winstaller` and `electron-installer-windows` support of desktop / startup menu shortcuts for nativefier packaged applications. - The `electron-squirrel-startup` entrypoint has no effect on both Linux and Darwin, only on Windows - Supporting it directly inside `nativefier` avoids having to "hack" around the existing `main.js` and including dependencies from `electron-squirrel-startup` in an intermediate package to be included in a third layer for the final installer executable - The following script based on both `nativefier` and `electron-winstaller` templates represents a portable proof of concept for this merge request : ```js var nativefier = require('nativefier').default; var electronInstaller = require('electron-winstaller'); var options = { name: 'Web WhatsApp', targetUrl: 'http://web.whatsapp.com', platform: 'windows', arch: 'x64', version: '0.36.4', out: '.', overwrite: false, asar: false, counter: false, bounce: false, width: 1280, height: 800, showMenuBar: false, fastQuit: false, userAgent: 'Mozilla ...', ignoreCertificate: false, ignoreGpuBlacklist: false, enableEs3Apis: false, insecure: false, honest: false, zoom: 1.0, singleInstance: false, fileDownloadOptions: { saveAs: true }, processEnvs: { GOOGLE_API_KEY: '<your-google-api-key>' } }; nativefier(options, function(error, appPath) { if (error) { console.error(error); return; } console.log('App has been nativefied to', appPath); resultPromise = electronInstaller.createWindowsInstaller({ appDirectory: 'Web WhatsApp-win32-x64', outputDirectory: './', authors: 'Web WhatsApp', exe: 'Web WhatsApp.exe' }); resultPromise.then(() => console.log('It worked!'), e => console.log(`No dice: ${e.message}`)); }); ```
2019-02-08 14:57:32 +00:00
}
const { isOSX } = helpers;
const APP_ARGS_FILE_PATH = path.join(__dirname, '..', 'nativefier.json');
2016-01-29 14:04:41 +00:00
const appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));
const fileDownloadOptions = Object.assign({}, appArgs.fileDownloadOptions);
electronDownload(fileDownloadOptions);
if (appArgs.processEnvs) {
Object.keys(appArgs.processEnvs).forEach((key) => {
/* eslint-env node */
process.env[key] = appArgs.processEnvs[key];
});
}
2016-01-29 14:04:41 +00:00
let mainWindow;
if (typeof appArgs.flashPluginDir === 'string') {
app.commandLine.appendSwitch('ppapi-flash-path', appArgs.flashPluginDir);
} else if (appArgs.flashPluginDir) {
const flashPath = inferFlash();
app.commandLine.appendSwitch('ppapi-flash-path', flashPath);
}
if (appArgs.ignoreCertificate) {
app.commandLine.appendSwitch('ignore-certificate-errors');
}
if (appArgs.disableGpu) {
app.disableHardwareAcceleration();
}
if (appArgs.ignoreGpuBlacklist) {
app.commandLine.appendSwitch('ignore-gpu-blacklist');
}
if (appArgs.enableEs3Apis) {
app.commandLine.appendSwitch('enable-es3-apis');
}
if (appArgs.diskCacheSize) {
app.commandLine.appendSwitch('disk-cache-size', appArgs.diskCacheSize);
}
if (appArgs.basicAuthUsername) {
2018-05-24 07:02:44 +00:00
app.commandLine.appendSwitch(
'basic-auth-username',
appArgs.basicAuthUsername,
);
}
if (appArgs.basicAuthPassword) {
2018-05-24 07:02:44 +00:00
app.commandLine.appendSwitch(
'basic-auth-password',
appArgs.basicAuthPassword,
);
}
// do nothing for setDockBadge if not OSX
let setDockBadge = () => {};
2016-01-23 18:02:23 +00:00
if (isOSX()) {
let currentBadgeCount = 0;
setDockBadge = (count, bounce = false) => {
app.dock.setBadge(count);
if (bounce && count > currentBadgeCount) app.dock.bounce();
currentBadgeCount = count;
};
}
2016-01-29 14:04:41 +00:00
app.on('window-all-closed', () => {
if (!isOSX() || appArgs.fastQuit) {
app.quit();
}
2015-07-05 06:08:13 +00:00
});
2016-01-29 14:04:41 +00:00
app.on('activate', (event, hasVisibleWindows) => {
if (isOSX()) {
// this is called when the dock is clicked
if (!hasVisibleWindows) {
mainWindow.show();
}
}
});
2016-01-29 14:04:41 +00:00
app.on('before-quit', () => {
// 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);
}
});
if (appArgs.crashReporter) {
app.on('will-finish-launching', () => {
crashReporter.start({
companyName: appArgs.companyName || '',
productName: appArgs.name,
submitURL: appArgs.crashReporter,
uploadToServer: true,
});
});
}
// quit if singleInstance mode and there's already another instance running
const shouldQuit = appArgs.singleInstance && !app.requestSingleInstanceLock();
if (shouldQuit) {
app.quit();
} else {
app.on('second-instance', () => {
if (mainWindow) {
if (!mainWindow.isVisible()) {
// try
mainWindow.show();
}
if (mainWindow.isMinimized()) {
// minimized
mainWindow.restore();
}
mainWindow.focus();
}
});
app.on('ready', () => {
mainWindow = createMainWindow(appArgs, app.quit, setDockBadge);
createTrayIcon(appArgs, mainWindow);
// Register global shortcuts
if (appArgs.globalShortcuts) {
appArgs.globalShortcuts.forEach((shortcut) => {
globalShortcut.register(shortcut.key, () => {
shortcut.inputEvents.forEach((inputEvent) => {
mainWindow.webContents.sendInputEvent(inputEvent);
});
});
});
}
});
}
2015-07-05 06:08:13 +00:00
app.on('new-window-for-tab', () => {
mainWindow.emit('new-tab');
});
2016-01-29 14:04:41 +00:00
app.on('login', (event, webContents, request, authInfo, callback) => {
// for http authentication
event.preventDefault();
2018-05-24 07:02:44 +00:00
if (
appArgs.basicAuthUsername !== null &&
appArgs.basicAuthPassword !== null
) {
callback(appArgs.basicAuthUsername, appArgs.basicAuthPassword);
} else {
createLoginWindow(callback);
}
2016-01-21 18:39:52 +00:00
});