• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2
3# This is used by the Node.js installer, which expects the cygwin/mingw
4# shell script to already be present in the npm dependency folder.
5
6(set -o igncr) 2>/dev/null && set -o igncr; # cygwin encoding fix
7
8basedir=`dirname "$0"`
9
10case `uname` in
11  *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
12esac
13
14if [ `uname` = 'Linux' ] && type wslpath &>/dev/null ; then
15  IS_WSL="true"
16fi
17
18function no_node_dir {
19  # if this didn't work, then everything else below will fail
20  echo "Could not determine Node.js install directory" >&2
21  exit 1
22}
23
24NODE_EXE="$basedir/node.exe"
25if ! [ -x "$NODE_EXE" ]; then
26  NODE_EXE="$basedir/node"
27fi
28if ! [ -x "$NODE_EXE" ]; then
29  NODE_EXE=node
30fi
31
32# this path is passed to node.exe, so it needs to match whatever
33# kind of paths Node.js thinks it's using, typically win32 paths.
34CLI_BASEDIR="$("$NODE_EXE" -p 'require("path").dirname(process.execPath)' 2> /dev/null)"
35if [ $? -ne 0 ]; then
36  # this fails under WSL 1 so add an additional message. we also suppress stderr above
37  # because the actual error raised is not helpful. in WSL 1 node.exe cannot handle
38  # output redirection properly. See https://github.com/microsoft/WSL/issues/2370
39  if [ "$IS_WSL" == "true" ]; then
40    echo "WSL 1 is not supported. Please upgrade to WSL 2 or above." >&2
41  fi
42  no_node_dir
43fi
44NPM_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-cli.js"
45NPX_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npx-cli.js"
46NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
47if [ $? -ne 0 ]; then
48  no_node_dir
49fi
50NPM_PREFIX_NPX_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npx-cli.js"
51
52# a path that will fail -f test on any posix bash
53NPX_WSL_PATH="/.."
54
55# WSL can run Windows binaries, so we have to give it the win32 path
56# however, WSL bash tests against posix paths, so we need to construct that
57# to know if npm is installed globally.
58if [ "$IS_WSL" == "true" ]; then
59  NPX_WSL_PATH=`wslpath "$NPM_PREFIX_NPX_CLI_JS"`
60fi
61if [ -f "$NPM_PREFIX_NPX_CLI_JS" ] || [ -f "$NPX_WSL_PATH" ]; then
62  NPX_CLI_JS="$NPM_PREFIX_NPX_CLI_JS"
63fi
64
65"$NODE_EXE" "$NPX_CLI_JS" "$@"
66