npx 2.0 KB

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