From 1faee66a778924d1f038e6ba789daac3706b15db Mon Sep 17 00:00:00 2001
From: gulderov <gulderov@ya.ru>
Date: Mon, 11 Sep 2017 15:31:28 +0300
Subject: [PATCH] Auto-detect running editor on Linux for error overlay (#3077)

* Auto-detect running editor on Linux for error overlay

Basic support of auto detecting running editor for #2636.
Tested on Ubuntu 16.04.
It detects few editors. JetBrains products should start by
wrapper like /usr/local/bin/webstorm. Otherwise it takes a
lot of time to open editor.

* Comments fixed.

* List all processes owned by you

* Comment rewording
---
 packages/react-dev-utils/launchEditor.js | 33 ++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/packages/react-dev-utils/launchEditor.js b/packages/react-dev-utils/launchEditor.js
index 95ae1cddf..a6f7226ef 100644
--- a/packages/react-dev-utils/launchEditor.js
+++ b/packages/react-dev-utils/launchEditor.js
@@ -56,6 +56,20 @@ const COMMON_EDITORS_OSX = {
     '/Applications/WebStorm.app/Contents/MacOS/webstorm',
 };
 
+const COMMON_EDITORS_LINUX = {
+  atom: 'atom',
+  Brackets: 'brackets',
+  code: 'code',
+  emacs: 'emacs',
+  'idea.sh': 'idea',
+  'phpstorm.sh': 'phpstorm',
+  'pycharm.sh': 'pycharm',
+  'rubymine.sh': 'rubymine',
+  sublime_text: 'sublime_text',
+  vim: 'vim',
+  'webstorm.sh': 'webstorm',
+};
+
 const COMMON_EDITORS_WIN = [
   'Brackets.exe',
   'Code.exe',
@@ -144,8 +158,9 @@ function guessEditor() {
     return shellQuote.parse(process.env.REACT_EDITOR);
   }
 
-  // Using `ps x` on OSX or `Get-Process` on Windows we can find out which editor is currently running.
-  // Potentially we could use similar technique for Linux
+  // We can find out which editor is currently running by:
+  // `ps x` on macOS and Linux
+  // `Get-Process` on Windows
   try {
     if (process.platform === 'darwin') {
       const output = child_process.execSync('ps x').toString();
@@ -176,6 +191,20 @@ function guessEditor() {
           return [fullProcessPath];
         }
       }
+    } else if (process.platform === 'linux') {
+      // --no-heading No header line
+      // x List all processes owned by you
+      // -o comm Need only names column
+      const output = child_process
+        .execSync('ps x --no-heading -o comm --sort=comm')
+        .toString();
+      const processNames = Object.keys(COMMON_EDITORS_LINUX);
+      for (let i = 0; i < processNames.length; i++) {
+        const processName = processNames[i];
+        if (output.indexOf(processName) !== -1) {
+          return [COMMON_EDITORS_LINUX[processName]];
+        }
+      }
     }
   } catch (error) {
     // Ignore...
-- 
GitLab