2017-11-05 02:49:23 +02:00
|
|
|
// This is the basic initialization for the Electron MAIN process
|
|
|
|
|
2017-11-04 13:26:49 +02:00
|
|
|
const electronApp = require('electron').app;
|
2021-10-01 20:35:27 +02:00
|
|
|
require('@electron/remote/main').initialize();
|
2020-10-09 19:35:46 +02:00
|
|
|
const ElectronAppWrapper = require('./ElectronAppWrapper').default;
|
2024-02-07 20:04:29 +02:00
|
|
|
const { pathExistsSync, readFileSync } = require('fs-extra');
|
2017-11-05 02:17:48 +02:00
|
|
|
const { initBridge } = require('./bridge');
|
2023-07-27 17:05:56 +02:00
|
|
|
const Logger = require('@joplin/utils/Logger').default;
|
2020-11-07 17:59:37 +02:00
|
|
|
const FsDriverNode = require('@joplin/lib/fs-driver-node').default;
|
|
|
|
const envFromArgs = require('@joplin/lib/envFromArgs');
|
2020-11-11 16:22:42 +02:00
|
|
|
const packageInfo = require('./packageInfo.js');
|
2021-10-16 11:07:41 +02:00
|
|
|
const { isCallbackUrl } = require('@joplin/lib/callbackUrlUtils');
|
2024-02-22 23:30:21 +02:00
|
|
|
const determineBaseAppDirs = require('@joplin/lib/determineBaseAppDirs').default;
|
2024-07-26 13:22:49 +02:00
|
|
|
const registerCustomProtocols = require('./utils/customProtocols/registerCustomProtocols.js').default;
|
2020-11-11 16:22:42 +02:00
|
|
|
|
|
|
|
// Electron takes the application name from package.json `name` and
|
|
|
|
// displays this in the tray icon toolip and message box titles, however in
|
|
|
|
// our case it's a string like "@joplin/app-desktop". It's also supposed to
|
|
|
|
// check the productName key but is not doing it, so here set the
|
|
|
|
// application name to the right string.
|
|
|
|
electronApp.name = packageInfo.name;
|
2017-11-03 01:26:08 +02:00
|
|
|
|
2017-11-04 13:26:49 +02:00
|
|
|
process.on('unhandledRejection', (reason, p) => {
|
|
|
|
console.error('Unhandled promise rejection', p, 'reason:', reason);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2018-04-23 21:50:29 +02:00
|
|
|
// Likewise, we want to know if a profile is specified early, in particular
|
|
|
|
// to save the window state data.
|
2024-02-07 20:04:29 +02:00
|
|
|
function getProfileFromArgs(args) {
|
2018-04-23 21:50:29 +02:00
|
|
|
if (!args) return null;
|
|
|
|
const profileIndex = args.indexOf('--profile');
|
|
|
|
if (profileIndex <= 0 || profileIndex >= args.length - 1) return null;
|
|
|
|
const profileValue = args[profileIndex + 1];
|
|
|
|
return profileValue ? profileValue : null;
|
|
|
|
}
|
|
|
|
|
2017-11-14 12:53:18 +02:00
|
|
|
Logger.fsDriver_ = new FsDriverNode();
|
|
|
|
|
2017-11-13 20:47:35 +02:00
|
|
|
const env = envFromArgs(process.argv);
|
2024-02-07 20:04:29 +02:00
|
|
|
const profileFromArgs = getProfileFromArgs(process.argv);
|
2020-09-01 23:25:23 +02:00
|
|
|
const isDebugMode = !!process.argv && process.argv.indexOf('--debug') >= 0;
|
2017-11-13 20:47:35 +02:00
|
|
|
|
2024-02-07 20:04:29 +02:00
|
|
|
// We initialize all these variables here because they are needed from the main process. They are
|
|
|
|
// then passed to the renderer process via the bridge.
|
|
|
|
const appId = `net.cozic.joplin${env === 'dev' ? 'dev' : ''}-desktop`;
|
|
|
|
let appName = env === 'dev' ? 'joplindev' : 'joplin';
|
|
|
|
if (appId.indexOf('-desktop') >= 0) appName += '-desktop';
|
2024-02-22 23:30:21 +02:00
|
|
|
const { rootProfileDir } = determineBaseAppDirs(profileFromArgs, appName);
|
2024-02-07 20:04:29 +02:00
|
|
|
const settingsPath = `${rootProfileDir}/settings.json`;
|
|
|
|
let autoUploadCrashDumps = false;
|
|
|
|
|
|
|
|
if (pathExistsSync(settingsPath)) {
|
|
|
|
const settingsContent = readFileSync(settingsPath, 'utf8');
|
|
|
|
try {
|
|
|
|
const settings = JSON.parse(settingsContent);
|
|
|
|
autoUploadCrashDumps = !!settings && !!settings.autoUploadCrashDumps;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Could not load settings: ${settingsPath}:`, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 11:07:41 +02:00
|
|
|
electronApp.setAsDefaultProtocolClient('joplin');
|
2024-07-26 13:22:49 +02:00
|
|
|
void registerCustomProtocols();
|
2021-10-16 11:07:41 +02:00
|
|
|
|
|
|
|
const initialCallbackUrl = process.argv.find((arg) => isCallbackUrl(arg));
|
|
|
|
|
2024-02-07 20:04:29 +02:00
|
|
|
const wrapper = new ElectronAppWrapper(electronApp, env, rootProfileDir, isDebugMode, initialCallbackUrl);
|
2017-11-04 15:23:15 +02:00
|
|
|
|
2024-02-07 20:04:29 +02:00
|
|
|
initBridge(wrapper, appId, appName, rootProfileDir, autoUploadCrashDumps);
|
2017-11-04 15:23:15 +02:00
|
|
|
|
2017-11-05 02:17:48 +02:00
|
|
|
wrapper.start().catch((error) => {
|
|
|
|
console.error('Electron App fatal error:');
|
2017-11-04 13:26:49 +02:00
|
|
|
console.error(error);
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|