bug 1346994 - Updated nodejs not found message r?standard8 draft
authorRajesh Kathiriya <rajesh.kathiriya507@gmail.com>
Wed, 17 May 2017 23:07:21 +0530
changeset 585078 f1aa95ea9096
parent 579653 85e5d15c3169
child 630627 2b519ebfbd51
push id61003
push userbmo:rajesh.kathiriya507@gmail.com
push dateFri, 26 May 2017 14:34:49 +0000
reviewersstandard8
bugs1346994
milestone55.0a1
bug 1346994 - Updated nodejs not found message r?standard8 MozReview-Commit-ID: AKZyW9HPvmY
tools/lint/eslint/setup_helper.py
--- a/tools/lint/eslint/setup_helper.py
+++ b/tools/lint/eslint/setup_helper.py
@@ -10,18 +10,23 @@ import os
 import platform
 import re
 import subprocess
 import sys
 from distutils.version import LooseVersion
 sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "..", "python", "which"))
 import which
 
+NODE_MACHING_VERSION_NOT_FOUND_MESSAGE = """
+nodejs is out of date. You currently have node %s but v6.9.1 is required.
+Please update nodejs from https://nodejs.org and try again.
+""".strip()
+
 NODE_NOT_FOUND_MESSAGE = """
-nodejs v6.9.1 is either not installed or is installed to a non-standard path.
+nodejs is either not installed or is installed to a non-standard path.
 Please install nodejs from https://nodejs.org and try again.
 
 Valid installation paths:
 """.strip()
 
 NPM_NOT_FOUND_MESSAGE = """
 Node Package Manager (npm) is either not installed or installed to a
 non-standard path. Please install npm from https://nodejs.org (it comes as an
@@ -236,69 +241,82 @@ def get_possible_node_paths_win():
     return list({
         "%s\\nodejs" % os.environ.get("SystemDrive"),
         os.path.join(os.environ.get("ProgramFiles"), "nodejs"),
         os.path.join(os.environ.get("PROGRAMW6432"), "nodejs"),
         os.path.join(os.environ.get("PROGRAMFILES"), "nodejs")
     })
 
 
-def get_node_or_npm_path(filename, minversion=None):
+def which_path(filename):
     """
     Return the nodejs or npm path.
     """
+    path = None
 
     if platform.system() == "Windows":
         for ext in [".cmd", ".exe", ""]:
             try:
-                node_or_npm_path = which.which(filename + ext,
-                                               path=get_possible_node_paths_win())
-                if is_valid(node_or_npm_path, minversion):
-                    return node_or_npm_path
+                path = which.which(filename + ext, path=get_possible_node_paths_win())
             except which.WhichError:
                 pass
     else:
         try:
-            node_or_npm_path = which.which(filename)
-            if is_valid(node_or_npm_path, minversion):
-                return node_or_npm_path
+            path = which.which(filename)
         except which.WhichError:
             if filename == "node":
                 # Retry it with "nodejs" as Linux tends to prefer nodejs rather than node.
-                return get_node_or_npm_path("nodejs", minversion)
+                return which_path("nodejs")
+
+    return path
+
+
+def get_node_or_npm_path(filename, minversion=None):
+    node_or_npm_path = which_path(filename)
 
-    if filename in ('node', 'nodejs'):
-        print(NODE_NOT_FOUND_MESSAGE)
-    elif filename == "npm":
-        print(NPM_NOT_FOUND_MESSAGE)
+    if not node_or_npm_path:
+        if filename in ('node', 'nodejs'):
+            print(NODE_NOT_FOUND_MESSAGE)
+        elif filename == "npm":
+            print(NPM_NOT_FOUND_MESSAGE)
+
+        if platform.system() == "Windows":
+            app_paths = get_possible_node_paths_win()
 
-    if platform.system() == "Windows":
-        app_paths = get_possible_node_paths_win()
+            for p in app_paths:
+                print("  - %s" % p)
+        elif platform.system() == "Darwin":
+            print("  - /usr/local/bin/{}".format(filename))
+        elif platform.system() == "Linux":
+            print("  - /usr/bin/{}".format(filename))
+
+        return None
 
-        for p in app_paths:
-            print("  - %s" % p)
-    elif platform.system() == "Darwin":
-        print("  - /usr/local/bin/{}".format(filename))
-    elif platform.system() == "Linux":
-        print("  - /usr/bin/{}".format(filename))
+    if not minversion:
+        return node_or_npm_path
+
+    version_str = get_version(node_or_npm_path)
+
+    version = LooseVersion(version_str.lstrip('v'))
+
+    if version > minversion:
+        return node_or_npm_path
+
+    print(NODE_MACHING_VERSION_NOT_FOUND_MESSAGE % version_str.strip())
 
     return None
 
 
-def is_valid(path, minversion=None):
+def get_version(path):
     try:
         version_str = subprocess.check_output([path, "--version"],
                                               stderr=subprocess.STDOUT)
-        if minversion:
-            # nodejs prefixes its version strings with "v"
-            version = LooseVersion(version_str.lstrip('v'))
-            return version >= minversion
-        return True
+        return version_str
     except (subprocess.CalledProcessError, OSError):
-        return False
+        return None
 
 
 def set_project_root(root=None):
     """Sets the project root to the supplied path, or works out what the root
     is based on looking for 'mach'.
 
     Keyword arguments:
     root - (optional) The path to set the root to.