From 830c4a45ee86ab4328d835c46b1fc352a88c4136 Mon Sep 17 00:00:00 2001 From: Hinton Date: Mon, 5 Oct 2020 20:05:48 +0200 Subject: [PATCH] Replace cosole logs with logService --- src/main.ts | 2 +- src/services/nativeMessaging.service.ts | 54 ++++++++++++++----------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/main.ts b/src/main.ts index 450ac644..142f03de 100644 --- a/src/main.ts +++ b/src/main.ts @@ -119,7 +119,7 @@ export class Main { this.biometricMain = new BiometricDarwinMain(this.storageService, this.i18nService); } - this.nativeMessagingService = new NativeMessagingService(app.getPath('userData'), app.getAppPath()); + this.nativeMessagingService = new NativeMessagingService(this.logService, app.getPath('userData'), app.getAppPath()); } bootstrap() { diff --git a/src/services/nativeMessaging.service.ts b/src/services/nativeMessaging.service.ts index 236f5ff4..ffe57652 100644 --- a/src/services/nativeMessaging.service.ts +++ b/src/services/nativeMessaging.service.ts @@ -1,12 +1,14 @@ -import * as fs from 'fs'; +import { promises as fs, existsSync } from 'fs'; import * as ipc from 'node-ipc'; import * as path from 'path'; import * as util from 'util'; +import { LogService } from 'jslib/abstractions/log.service'; + export class NativeMessagingService { private connected = false; - constructor(private userPath: string, private appPath: string) {} + constructor(private logService: LogService, private userPath: string, private appPath: string) {} listen() { ipc.config.id = 'bitwarden'; @@ -47,32 +49,15 @@ export class NativeMessagingService { const firefoxJson = {...baseJson, ...{ 'allowed_origins': ['446900e4-71c2-419f-a6a7-df9c091e268b']}} const chromeJson = {...baseJson, ...{ 'allowed_origins': ['chrome-extension://ijeheppnniijonkinoakkofcdhdfojda/']}} - fs.mkdir(path.join(this.userPath, 'browsers'), (err) => console.log); + if (!existsSync(path.join(this.userPath, 'browsers'))) { + fs.mkdir(path.join(this.userPath, 'browsers')) + .catch(this.logService.error) + } this.writeManifest('firefox.json', firefoxJson); this.writeManifest('chrome.json', chromeJson); } - private writeManifest(filename: string, manifest: object) { - fs.writeFile( - path.join(this.userPath, 'browsers', filename), - JSON.stringify(manifest, null, 2), - (err) => console.log - ); - } - - private binaryName() { - switch (process.platform) { - case 'win32': - return 'app-win.exe' - case 'darwin': - return 'app-linux' - case 'linux': - default: - return 'app-macos' - } - } - // Setup registry and/or directories // TODO: Do other browsers use different directories? enableManifest() { @@ -89,6 +74,25 @@ export class NativeMessagingService { } } + private writeManifest(filename: string, manifest: object) { + fs.writeFile( + path.join(this.userPath, 'browsers', filename), + JSON.stringify(manifest, null, 2) + ).catch(this.logService.error); + } + + private binaryName() { + switch (process.platform) { + case 'win32': + return 'app-win.exe' + case 'darwin': + return 'app-linux' + case 'linux': + default: + return 'app-macos' + } + } + private createWindowsRegistry(check: string, location: string, jsonFile: string) { const regedit = require('regedit'); regedit.setExternalVBSLocation('resources/regedit/vbs'); @@ -97,6 +101,8 @@ export class NativeMessagingService { const createKey = util.promisify(regedit.createKey); const putValue = util.promisify(regedit.putValue); + this.logService.debug(`Adding registry: ${location}`) + // Check installed list(check) .then(() => { @@ -115,6 +121,6 @@ export class NativeMessagingService { return putValue(obj); }) - .catch() + .catch(this.logService.error) } }