bug 1346994 - Updated nodejs not found message r=standard8
authorRajesh Kathiriya <rajesh.kathiriya507@gmail.com>
Wed, 17 May 2017 23:07:21 +0530
changeset 360829 a709c2aec00c22ab4555f650f2990c465006b1dd
parent 360828 e9e3bea0eddd364642cfae82c6fd185f8df542dd
child 360830 695701369fd61014c95517f864a676be5cb4e020
push id31902
push userryanvm@gmail.com
push dateFri, 26 May 2017 19:43:26 +0000
treeherdermozilla-central@ba1a33add29d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersstandard8
bugs1346994
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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.