Browse Source

Log update logs to file instead of showing dialogs (#3043)

* Log update logs to file instead of showing dialogs

* fix lint

* remove lind-disable comment
Ivan Rukhavets 4 years ago
parent
commit
c4a900f435

+ 1 - 0
packages/apps-electron/package.json

@@ -7,6 +7,7 @@
     "@babel/polyfill": "^7.10.1",
     "@polkadot/dev": "^0.55.12",
     "@polkadot/react-components": "0.47.0-beta.8",
+    "electron-log": "^4.2.2",
     "electron-updater": "^4.3.3"
   },
   "devDependencies": {

+ 0 - 67
packages/apps-electron/src/electron.ts

@@ -1,67 +0,0 @@
-// Copyright 2017-2020 @polkadot/apps authors & contributors
-// This software may be modified and distributed under the terms
-// of the Apache-2.0 license. See the LICENSE file for details.
-
-import { app, BrowserWindow, dialog, screen } from 'electron';
-import { autoUpdater } from 'electron-updater';
-import path from 'path';
-import { features } from './featureToggles';
-import { registerAccountStoreHandlers } from './main/account-store';
-
-const ENV = process.env.NODE_ENV || 'production';
-const isDev = ENV === 'development';
-
-function createWindow (): Promise<unknown> {
-  const { height, width } = screen.getPrimaryDisplay().workAreaSize;
-
-  const win = new BrowserWindow({
-    height,
-    webPreferences: {
-      contextIsolation: true,
-      enableRemoteModule: false,
-      nodeIntegration: false,
-      preload: path.join(__dirname, 'preload.js')
-    },
-    width
-  });
-
-  if (isDev) {
-    win.webContents.openDevTools();
-
-    return win.loadURL('http://0.0.0.0:3000/');
-  }
-
-  const mainFilePath = path.resolve(__dirname, 'index.html');
-
-  return win.loadFile(mainFilePath);
-}
-
-const onReady = async () => {
-  registerAccountStoreHandlers();
-  await createWindow();
-
-  if (features.autoUpdater) {
-    await autoUpdater.checkForUpdatesAndNotify();
-  }
-};
-
-app.whenReady().then(onReady).catch(console.error);
-
-if (features.autoUpdater) {
-  autoUpdater.on('update-not-available', () => {
-    dialog.showMessageBox({
-      message: 'Current version is up-to-date.',
-      title: 'No Updates'
-    }).catch(console.error);
-  });
-
-  autoUpdater.on('error', (error) => {
-    if (!error) {
-      return;
-    }
-
-    const err: Error = error as Error;
-
-    dialog.showErrorBox('Auto update error: ', (err.stack || err).toString());
-  });
-}

+ 19 - 0
packages/apps-electron/src/electron/autoUpdater.ts

@@ -0,0 +1,19 @@
+// Copyright 2017-2020 @polkadot/apps authors & contributors
+// This software may be modified and distributed under the terms
+// of the Apache-2.0 license. See the LICENSE file for details.
+
+import { AppUpdater } from 'electron-updater';
+
+export async function setupAutoUpdater (): Promise<void> {
+  const { autoUpdater } = await import('electron-updater');
+
+  await setLogger(autoUpdater);
+  autoUpdater.checkForUpdatesAndNotify().catch(console.error);
+}
+
+async function setLogger (autoUpdater: AppUpdater): Promise<void> {
+  const log = await import('electron-log');
+
+  log.transports.file.level = 'debug';
+  autoUpdater.logger = log;
+}

+ 22 - 0
packages/apps-electron/src/electron/index.ts

@@ -0,0 +1,22 @@
+// Copyright 2017-2020 @polkadot/apps authors & contributors
+// This software may be modified and distributed under the terms
+// of the Apache-2.0 license. See the LICENSE file for details.
+
+import { app } from 'electron';
+import { features } from '../featureToggles';
+import { registerAccountStoreHandlers } from '../main/account-store';
+import { setupAutoUpdater } from './autoUpdater';
+import { createWindow } from './window';
+
+const ENV = process.env.NODE_ENV || 'production';
+
+const onReady = async () => {
+  registerAccountStoreHandlers();
+  await createWindow(ENV);
+
+  if (features.autoUpdater) {
+    await setupAutoUpdater();
+  }
+};
+
+app.whenReady().then(onReady).catch(console.error);

+ 31 - 0
packages/apps-electron/src/electron/window.ts

@@ -0,0 +1,31 @@
+// Copyright 2017-2020 @polkadot/apps authors & contributors
+// This software may be modified and distributed under the terms
+// of the Apache-2.0 license. See the LICENSE file for details.
+
+import { BrowserWindow, screen } from 'electron';
+import path from 'path';
+
+export function createWindow (environment: string): Promise<unknown> {
+  const { height, width } = screen.getPrimaryDisplay().workAreaSize;
+
+  const win = new BrowserWindow({
+    height,
+    webPreferences: {
+      contextIsolation: true,
+      enableRemoteModule: false,
+      nodeIntegration: false,
+      preload: path.join(__dirname, 'preload.js')
+    },
+    width
+  });
+
+  if (environment === 'development') {
+    win.webContents.openDevTools();
+
+    return win.loadURL('http://0.0.0.0:3000/');
+  }
+
+  const mainFilePath = path.resolve(__dirname, 'index.html');
+
+  return win.loadFile(mainFilePath);
+}

+ 1 - 1
packages/apps-electron/webpack.main.config.js

@@ -14,7 +14,7 @@ function createWebpack () {
   return [
     {
       entry: {
-        electron: './src/electron.ts',
+        electron: './src/electron',
         preload: './src/preload.ts'
       },
       mode: ENV,

+ 8 - 0
yarn.lock

@@ -3193,6 +3193,7 @@ __metadata:
     "@types/react-dom": ^16.9.8
     babel-loader: ^8.1.0
     copy-webpack-plugin: ^6.0.2
+    electron-log: ^4.2.2
     electron-updater: ^4.3.3
     html-webpack-plugin: ^4.3.0
     react: ^16.13.1
@@ -9468,6 +9469,13 @@ __metadata:
   languageName: node
   linkType: hard
 
+"electron-log@npm:^4.2.2":
+  version: 4.2.2
+  resolution: "electron-log@npm:4.2.2"
+  checksum: 3/ff801eb7aa62ed1246b2eddb6edc92f90a20b90f7169191687914d3adc3c387c222c369b1ebacfdfa9aef993f893dbad89fa0430a99c56c8303afee92e2a8532
+  languageName: node
+  linkType: hard
+
 "electron-notarize@npm:^0.2.0":
   version: 0.2.1
   resolution: "electron-notarize@npm:0.2.1"