From 94d3d4f85a245f474091a5e241e249ecbc2c12a1 Mon Sep 17 00:00:00 2001
From: Alexey Pavlov <alexpux@gmail.com>
Date: Thu, 12 Apr 2018 10:20:30 +0200
Subject: [PATCH 12/23] Use shell wrapper script for npm on MSYS2 mingw-w64

---
 tools/install.py | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/tools/install.py b/tools/install.py
index be2bab54..20dfee30 100755
--- a/tools/install.py
+++ b/tools/install.py
@@ -95,7 +95,17 @@ def npm_files(action):
   if action == uninstall:
     action([link_path], 'bin/npm')
   elif action == install:
-    try_symlink('../lib/node_modules/npm/bin/npm-cli.js', link_path)
+  # Use MSYS2 shell script wrapper.
+    if ((os.name == 'nt' and 'GCC' in sys.version) or
+        (os.name == 'posix' and 'NT' in platform.system())):
+      # Copy the wrapper shell script
+      try_copy('deps/npm/bin/npm', link_path)
+      # deps/npm/bin/npm is a shell script so the shebang must run sh, not node
+      s = open(link_path, 'r').read()
+      s = re.sub(r'#!.*\n', '#!' + '/usr/bin/env sh' + '\n', s)
+      open(link_path, 'w').write(s)
+    else:
+      try_symlink('../lib/node_modules/npm/bin/npm-cli.js', link_path)
   else:
     assert(0) # unhandled action type
 
@@ -104,7 +114,17 @@ def npm_files(action):
   if action == uninstall:
     action([link_path], 'bin/npx')
   elif action == install:
-    try_symlink('../lib/node_modules/npm/bin/npx-cli.js', link_path)
+  # Use MSYS2 shell script wrapper.
+    if ((os.name == 'nt' and 'GCC' in sys.version) or
+        (os.name == 'posix' and 'NT' in platform.system())):
+      # Copy the wrapper shell script
+      try_copy('deps/npm/bin/npx', link_path)
+      # deps/npm/bin/npm is a shell script so the shebang must run sh, not node
+      s = open(link_path, 'r').read()
+      s = re.sub(r'#!.*\n', '#!' + '/usr/bin/env sh' + '\n', s)
+      open(link_path, 'w').write(s)
+    else:
+      try_symlink('../lib/node_modules/npm/bin/npx-cli.js', link_path)
   else:
     assert(0) # unhandled action type
 
-- 
2.17.0.windows.1

