Skip to content

A full-featured download manager.

feat: check is running in dmg then move to App

Dr_rOotff3b1b2f1523
diff --git a/src/main/Application.js b/src/main/Application.js
index 7147bea4..885ce559 100644
--- a/src/main/Application.js
+++ b/src/main/Application.js
@@ -2,6 +2,7 @@ import { EventEmitter } from 'events'
import { app, shell, dialog, ipcMain } from 'electron'
import is from 'electron-is'
import logger from './core/Logger'
+import ExceptionHandler from './core/ExceptionHandler'
import ConfigManager from './core/ConfigManager'
import Engine from './core/Engine'
import UpdateManager from './core/UpdateManager'
@@ -15,8 +16,10 @@ export default class Application extends EventEmitter {
constructor () {
super()
+ this.exceptionHandler = new ExceptionHandler()
+
this.locale = app.getLocale()
- logger.log('this.locale', this.locale)
+ logger.log('[Motrix] Locale: ', this.locale)
this.configManager = new ConfigManager()
diff --git a/src/main/core/ExceptionHandler.js b/src/main/core/ExceptionHandler.js
new file mode 100644
index 00000000..5924a2ae
--- /dev/null
+++ b/src/main/core/ExceptionHandler.js
@@ -0,0 +1,41 @@
+import { app, dialog } from 'electron'
+import is from 'electron-is'
+import logger from './Logger'
+import {
+ isRunningInDmg,
+ moveAppToApplicationsFolder
+} from '../utils/index'
+
+const defaults = {
+ showDialog: !is.dev()
+}
+export default class ExceptionHandler {
+ constructor (options) {
+ this.options = {
+ ...defaults,
+ ...options
+ }
+
+ this.setup()
+ }
+
+ setup () {
+ if (is.dev()) {
+ return
+ }
+ const { showDialog } = this.options
+ process.on('uncaughtException', (err) => {
+ const { message, stack } = err
+ logger.error(`[Motrix] uncaughtException: ${message}`)
+ logger.error(stack)
+ if (message.includes('spawn') && message.includes('ENOENT') && isRunningInDmg()) {
+ moveAppToApplicationsFolder()
+ return
+ }
+
+ if (showDialog && app.isReady()) {
+ dialog.showErrorBox('系统错误', message)
+ }
+ })
+ }
+}
diff --git a/src/main/index.js b/src/main/index.js
index 3298cda4..b2bb93ae 100644
--- a/src/main/index.js
+++ b/src/main/index.js
@@ -2,10 +2,6 @@ import { app } from 'electron'
import is from 'electron-is'
import logger from './core/Logger'
-import {
- isRunningInDmg,
- moveAppToApplicationsFolder
-} from './utils/index'
import Application from './Application'
if (process.env.NODE_ENV !== 'development') {
@@ -53,26 +49,22 @@ function _init () {
}
function init () {
- if (isRunningInDmg()) {
- moveAppToApplicationsFolder()
- return
- }
- _init()
-}
-
-// Mac App Store Sandboxed App Not support requestSingleInstanceLock
-if (is.mas()) {
- init()
-} else {
- const gotSingleLock = app.requestSingleInstanceLock()
-
- if (!gotSingleLock) {
- app.quit()
+ // Mac App Store Sandboxed App Not support requestSingleInstanceLock
+ if (is.mas()) {
+ _init()
} else {
- app.on('second-instance', (event, commandLine, workingDirectory) => {
- global.application.showPage('index')
- })
+ const gotSingleLock = app.requestSingleInstanceLock()
- init()
+ if (!gotSingleLock) {
+ app.quit()
+ } else {
+ app.on('second-instance', (event, commandLine, workingDirectory) => {
+ global.application.showPage('index')
+ })
+
+ _init()
+ }
}
}
+
+init()
diff --git a/src/main/utils/index.js b/src/main/utils/index.js
index 03565319..f28376ae 100644
--- a/src/main/utils/index.js
+++ b/src/main/utils/index.js
@@ -36,7 +36,7 @@ export function transformConfig (config) {
}
export function isRunningInDmg () {
- if (!is.macOS()) {
+ if (!is.macOS() || is.dev()) {
return false
}
const appPath = app.getAppPath()