2017-11-05 00:49:23 +00:00
|
|
|
// This is the basic initialization for the Electron MAIN process
|
|
|
|
|
|
2025-07-23 14:24:49 -07:00
|
|
|
import './utils/sourceMapSetup';
|
|
|
|
|
import { app as electronApp } from 'electron';
|
2021-10-01 19:35:27 +01:00
|
|
|
require('@electron/remote/main').initialize();
|
2025-07-23 14:24:49 -07:00
|
|
|
import ElectronAppWrapper from './ElectronAppWrapper';
|
|
|
|
|
import { pathExistsSync, readFileSync, mkdirpSync } from 'fs-extra';
|
|
|
|
|
import { initBridge } from './bridge';
|
|
|
|
|
import Logger from '@joplin/utils/Logger';
|
|
|
|
|
import FsDriverNode from '@joplin/lib/fs-driver-node';
|
2020-11-07 15:59:37 +00:00
|
|
|
const envFromArgs = require('@joplin/lib/envFromArgs');
|
2020-11-11 14:22:42 +00:00
|
|
|
const packageInfo = require('./packageInfo.js');
|
2025-07-23 14:24:49 -07:00
|
|
|
import { isCallbackUrl } from '@joplin/lib/callbackUrlUtils';
|
|
|
|
|
import determineBaseAppDirs from '@joplin/lib/determineBaseAppDirs';
|
|
|
|
|
import registerCustomProtocols from './utils/customProtocols/registerCustomProtocols';
|
2020-11-11 14:22:42 +00: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.
|
2024-08-08 11:53:43 -07:00
|
|
|
electronApp.setName(packageInfo.name);
|
2017-11-02 23:26:08 +00:00
|
|
|
|
2017-11-04 11:26:49 +00:00
|
|
|
process.on('unhandledRejection', (reason, p) => {
|
|
|
|
|
console.error('Unhandled promise rejection', p, 'reason:', reason);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-23 14:24:49 -07:00
|
|
|
const getFlagValueFromArgs = (args: string[], flag: string, defaultValue: string|null) => {
|
2018-04-23 21:50:29 +02:00
|
|
|
if (!args) return null;
|
2025-03-16 10:18:32 +00:00
|
|
|
const index = args.indexOf(flag);
|
|
|
|
|
if (index <= 0 || index >= args.length - 1) return defaultValue;
|
|
|
|
|
const value = args[index + 1];
|
|
|
|
|
return value ? value : defaultValue;
|
|
|
|
|
};
|
2018-04-23 21:50:29 +02:00
|
|
|
|
2017-11-14 10:53:18 +00:00
|
|
|
Logger.fsDriver_ = new FsDriverNode();
|
|
|
|
|
|
2017-11-13 18:47:35 +00:00
|
|
|
const env = envFromArgs(process.argv);
|
2025-03-16 10:18:32 +00:00
|
|
|
const profileFromArgs = getFlagValueFromArgs(process.argv, '--profile', null);
|
2020-09-01 22:25:23 +01:00
|
|
|
const isDebugMode = !!process.argv && process.argv.indexOf('--debug') >= 0;
|
2025-03-25 10:50:16 -07:00
|
|
|
const isEndToEndTesting = !!process.argv?.includes('--running-tests');
|
2025-03-16 10:18:32 +00:00
|
|
|
const altInstanceId = getFlagValueFromArgs(process.argv, '--alt-instance-id', '');
|
2017-11-13 18:47:35 +00:00
|
|
|
|
2024-02-07 18:04:29 +00: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';
|
2025-03-16 10:18:32 +00:00
|
|
|
const { rootProfileDir } = determineBaseAppDirs(profileFromArgs, appName, altInstanceId);
|
2025-03-27 22:14:37 +01:00
|
|
|
|
|
|
|
|
// We create the profile dir as soon as we know where it's going to be located since it's used in
|
|
|
|
|
// various places early in the initialisation code.
|
|
|
|
|
mkdirpSync(rootProfileDir);
|
|
|
|
|
|
2025-08-26 00:56:42 -07:00
|
|
|
// Required for correct display of Windows notifications. Should be done near the beginning of startup. See
|
|
|
|
|
// https://www.electron.build/nsis.html#guid-vs-application-name
|
|
|
|
|
electronApp.setAppUserModelId(appId);
|
|
|
|
|
|
2024-02-07 18:04:29 +00: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 10:07:41 +01:00
|
|
|
electronApp.setAsDefaultProtocolClient('joplin');
|
2024-07-26 04:22:49 -07:00
|
|
|
void registerCustomProtocols();
|
2021-10-16 10:07:41 +01:00
|
|
|
|
|
|
|
|
const initialCallbackUrl = process.argv.find((arg) => isCallbackUrl(arg));
|
|
|
|
|
|
2025-03-25 10:50:16 -07:00
|
|
|
const wrapper = new ElectronAppWrapper(electronApp, {
|
|
|
|
|
env, profilePath: rootProfileDir, isDebugMode, initialCallbackUrl, isEndToEndTesting,
|
|
|
|
|
});
|
2017-11-04 13:23:15 +00:00
|
|
|
|
2025-07-23 14:24:49 -07:00
|
|
|
|
|
|
|
|
type ExtendedGlobal = {
|
|
|
|
|
joplinBridge: unknown;
|
|
|
|
|
};
|
|
|
|
|
(globalThis as unknown as ExtendedGlobal).joplinBridge = (
|
|
|
|
|
initBridge(wrapper, appId, appName, rootProfileDir, autoUploadCrashDumps, altInstanceId)
|
|
|
|
|
);
|
2017-11-04 13:23:15 +00:00
|
|
|
|
2017-11-05 00:17:48 +00:00
|
|
|
wrapper.start().catch((error) => {
|
|
|
|
|
console.error('Electron App fatal error:');
|
2017-11-04 11:26:49 +00:00
|
|
|
console.error(error);
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|