import { fs } from '@platform/fs'; import { app } from 'electron'; import * as devTools from '../helpers/devTools/main'; import { init as initIpc, IpcClient, IpcMessage, MAIN_ID } from '../helpers/ipc/main'; import * as logger from '../helpers/logger/main'; import { init as initSettings } from '../helpers/settings/main'; import { WindowsMain } from '../helpers/windows/main'; import * as t from '../types'; export * from '../types'; export * from '../helpers/screen/main'; export { is } from '../helpers/is/main'; export { devTools, logger }; /** * Initializes [Main] process systems (safely). */ export async function init( args: { appName?: string; ipc?: t.IpcClient; log?: t.IMainLog | string; settings?: t.IMainSettingsClient; windows?: t.IWindows; } = {}, ): Promise> { await ready(); const { appName } = args; const id = MAIN_ID; try { // Initiaize modules. const ipc = args.ipc || initIpc(); const settings = args.settings || initSettings({ ipc }); const windows = args.windows || WindowsMain.instance({ ipc }); const log = typeof args.log === 'object' ? args.log // Logger already exists and was provided. : initLog({ ipc, dir: args.log, appName }); // Initialize a new log. devTools.listen({ ipc, windows }); // Finish up. const main: t.IMain = { id, ipc, log, settings, windows }; return main; } catch (error) { console.log('MAIN/INIT:', error); // eslint-disable-line throw error; } } /** * Promise that waits until the app is ready or returns immediately if already `ready`. */ export function ready() { return new Promise((resolve, reject) => { if (app.isReady()) { resolve(); } else { app.on('ready', () => resolve()); } }); } /** * [Internal] */ function initLog(args: { ipc: IpcClient; dir?: string; appName?: string }) { // Initialize electron log directires. // https://electronjs.org/docs/api/app#appsetapplogspathpath // // NOTE: this is necessary when using electron 6.x // commented out here so as not to fail when consumers use 5.x // put this line prior to init in the calling code when working with 6. // const major = parseInt((process.versions.electron || '0').split('.')[0], 10); if (major >= 6) { app.setAppLogsPath(args.dir); } // Determine app name and dir. const { ipc } = args; const name = (args.appName || app.getName()).replace(/\s/g, '-').toLowerCase(); const dir = args.dir || fs.join(fs.dirname(app.getPath('logs')), name); // Setup the logger return logger.init({ ipc, dir }); }