Browse Source

fix crash after second instance opened without file path

MaysWind 3 years ago
parent
commit
2193fede8d
2 changed files with 18 additions and 16 deletions
  1. 11 9
      main/cmd.js
  2. 7 7
      main/main.js

+ 11 - 9
main/cmd.js

@@ -26,7 +26,7 @@ const argv = yargs(process.argv.slice(1))
     })
     .argv;
 
-function parseFilePath(argv) {
+function parseArguments(argv) {
     if (!argv || argv.length < 2) {
         return undefined;
     }
@@ -39,16 +39,18 @@ function parseFilePath(argv) {
         }
     }
 
-    const ret = yargs(actualArgv)
-        .command({
-            command: '$0 <file>'
-        })
-        .argv;
-
-    return ret ? ret.file : undefined;
+    try {
+        return yargs(actualArgv)
+            .command({
+                command: '$0 [file]'
+            })
+            .argv;
+    } catch (ex) {
+        return undefined;
+    }
 }
 
 module.exports = {
     argv: argv,
-    parseFilePath: parseFilePath
+    parseArguments: parseArguments
 };

+ 7 - 7
main/main.js

@@ -133,18 +133,18 @@ app.on('second-instance', (event, argv, workingDirectory, additionalData) => {
 
         core.mainWindow.focus();
 
-        let filePath = null;
+        let secondInstanceArgv = null;
 
-        if (additionalData && additionalData.argv) {
-            filePath = additionalData.argv.file;
+        if (additionalData) {
+            secondInstanceArgv = additionalData.argv;
         }
 
-        if (!filePath) {
-            filePath = cmd.parseFilePath(argv);
+        if (!secondInstanceArgv) {
+            secondInstanceArgv = cmd.parseArguments(argv);
         }
 
-        if (filePath && file.isContainsSupportedFileArg(filePath)) {
-            ipcRender.notifyRenderProcessNewNewTaskFromFileAfterViewLoaded(filePath);
+        if (secondInstanceArgv && secondInstanceArgv.file && file.isContainsSupportedFileArg(secondInstanceArgv.file)) {
+            ipcRender.notifyRenderProcessNewNewTaskFromFileAfterViewLoaded(secondInstanceArgv.file);
             ipcRender.notifyRenderProcessNavigateToNewTask();
         }
     }