bitwarden-desktop/src/main.ts

91 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-02-15 21:27:56 +01:00
import { app, BrowserWindow } from 'electron';
import * as path from 'path';
2018-01-16 21:58:17 +01:00
2018-02-14 05:38:18 +01:00
import { I18nService } from './services/i18n.service';
2018-02-08 19:10:13 +01:00
import { MenuMain } from './main/menu.main';
import { MessagingMain } from './main/messaging.main';
2018-02-11 05:24:22 +01:00
import { PowerMonitorMain } from './main/powerMonitor.main';
2018-02-12 22:07:14 +01:00
import { UpdaterMain } from './main/updater.main';
2018-04-24 22:30:47 +02:00
2018-04-25 05:53:20 +02:00
import { ConstantsService } from 'jslib/services/constants.service';
import { KeytarStorageListener } from 'jslib/electron/keytarStorageListener';
2018-04-24 22:30:47 +02:00
import { ElectronLogService } from 'jslib/electron/services/electronLog.service';
2018-04-26 21:44:07 +02:00
import { ElectronMainMessagingService } from 'jslib/electron/services/electronMainMessaging.service';
2018-04-24 22:30:47 +02:00
import { ElectronStorageService } from 'jslib/electron/services/electronStorage.service';
import { WindowMain } from 'jslib/electron/window.main';
2018-01-23 19:59:01 +01:00
2018-02-14 05:38:18 +01:00
export class Main {
2018-04-24 22:30:47 +02:00
logService: ElectronLogService;
2018-02-14 05:38:18 +01:00
i18nService: I18nService;
2018-04-24 22:30:47 +02:00
storageService: ElectronStorageService;
2018-04-26 21:44:07 +02:00
messagingService: ElectronMainMessagingService;
keytarStorageListener: KeytarStorageListener;
2018-02-14 05:38:18 +01:00
windowMain: WindowMain;
messagingMain: MessagingMain;
updaterMain: UpdaterMain;
menuMain: MenuMain;
powerMonitorMain: PowerMonitorMain;
constructor() {
2018-02-15 21:27:56 +01:00
// Set paths for portable builds
let appDataPath = null;
if (process.env.BITWARDEN_APPDATA_DIR != null) {
appDataPath = process.env.BITWARDEN_APPDATA_DIR;
2018-02-23 23:11:18 +01:00
} else if (process.platform === 'win32' && process.env.PORTABLE_EXECUTABLE_DIR != null) {
appDataPath = path.join(process.env.PORTABLE_EXECUTABLE_DIR, 'bitwarden-appdata');
2018-02-23 23:11:18 +01:00
} else if (process.platform === 'linux' && process.env.SNAP_USER_DATA != null) {
2018-02-23 23:16:00 +01:00
appDataPath = path.join(process.env.SNAP_USER_DATA, 'appdata');
}
if (appDataPath != null) {
2018-02-15 21:27:56 +01:00
app.setPath('userData', appDataPath);
}
2018-02-25 15:51:21 +01:00
app.setPath('logs', path.join(app.getPath('userData'), 'logs'));
2018-02-15 21:27:56 +01:00
2018-02-14 05:38:18 +01:00
const args = process.argv.slice(1);
const watch = args.some((val) => val === '--watch');
if (watch) {
// tslint:disable-next-line
require('electron-reload')(__dirname, {});
}
2018-04-24 22:30:47 +02:00
this.logService = new ElectronLogService(null, app.getPath('userData'));
2018-02-14 05:38:18 +01:00
this.i18nService = new I18nService('en', './locales/');
2018-04-24 22:30:47 +02:00
this.storageService = new ElectronStorageService();
2018-02-14 05:38:18 +01:00
2018-04-24 22:30:47 +02:00
this.windowMain = new WindowMain(this.storageService);
2018-02-14 05:38:18 +01:00
this.messagingMain = new MessagingMain(this);
this.updaterMain = new UpdaterMain(this);
this.menuMain = new MenuMain(this);
this.powerMonitorMain = new PowerMonitorMain(this);
2018-04-26 21:44:07 +02:00
this.messagingService = new ElectronMainMessagingService(this.windowMain, (message) => {
this.messagingMain.onMessage(message);
});
this.keytarStorageListener = new KeytarStorageListener('Bitwarden');
2018-02-14 05:38:18 +01:00
}
bootstrap() {
this.keytarStorageListener.init();
2018-04-25 05:53:20 +02:00
this.windowMain.init().then(async () => {
const locale = await this.storageService.get<string>(ConstantsService.localeKey);
await this.i18nService.init(locale != null ? locale : app.getLocale());
this.messagingMain.init();
this.menuMain.init();
this.powerMonitorMain.init();
await this.updaterMain.init();
}, (e: any) => {
// tslint:disable-next-line
console.error(e);
2018-02-14 05:38:18 +01:00
});
}
2018-01-16 23:30:57 +01:00
}
2018-02-14 05:38:18 +01:00
const main = new Main();
main.bootstrap();