Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(gitHook): add multiple platform support #5279

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 98 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,101 @@
#!/bin/sh

(set -o igncr) 2>/dev/null && set -o igncr; # cygwin encoding fix

# absolute path working directory
basecwd=${PWD}
# base script directory
basedir=`dirname "$0"`
# absolute path script directory
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# determine current platform shell is Cygwin bash
case `uname` in
*CYGWIN*)
basedir=`cygpath -w "$basedir"`
# make cygwin bin as priority
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:$PATH"
# add ../node_modules/.bin to PATH enviroment
if [ -d "${basedir}/../node_modules/.bin" ]; then
export PATH="$PATH:${basedir}/../node_modules/.bin"
fi
;;
esac
# determine current platform shell is WSL bash
IS_WSL="false"
if [ -x "$(command -v wslpath)" ]; then
if [ `uname` = "Linux" ] && type wslpath &>/dev/null ; then
IS_WSL="true"
fi
fi

. "$(dirname "$0")/_/husky.sh"

npx lint-staged
# declare functions
no_node_dir() {
# if this didn't work, then everything else below will fail
echo "Could not determine Node.js install directory" >&2
exit 1
}

# determine node path
## determine when this package installed in global
NODE_EXE="$basedir/node.exe"
if ! [ -x "$NODE_EXE" ]; then
NODE_EXE="$basedir/node"
fi
## fallback to default node
if ! [ -x "$NODE_EXE" ]; then
NODE_EXE=node
fi

# determine npx path
## this path is passed to node.exe, so it needs to match whatever
## kind of paths Node.js thinks it's using, typically win32 paths.
CLI_BASEDIR="$("$NODE_EXE" -p 'require("path").dirname(process.execPath)' 2> /dev/null)"
if [ $? -ne 0 ]; then
# this fails under WSL 1 so add an additional message. we also suppress stderr above
# because the actual error raised is not helpful. in WSL 1 node.exe cannot handle
# output redirection properly. See https://github.com/microsoft/WSL/issues/2370
if [ "$IS_WSL" == "true" ]; then
echo "WSL 1 is not supported. Please upgrade to WSL 2 or above." >&2
fi
no_node_dir
fi
NPM_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-cli.js"
## check is linux symlink
## $NPM_CLI_JS on windows found but not in linux
## global node_modules located in <node bin>/../lib
if ! [ -f "$NPM_CLI_JS" ]; then
NPM_CLI_JS="$CLI_BASEDIR/../lib/node_modules/npm/bin/npm-cli.js"
fi
NPX_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npx-cli.js"
if ! [ -f "$NPX_CLI_JS" ]; then
NPX_CLI_JS="$CLI_BASEDIR/../lib/node_modules/npm/bin/npx-cli.js"
fi
## get node home
NODE_HOME=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
## throw when not found (node not installed correctly)
if [ $? -ne 0 ]; then
no_node_dir
fi

# fix WSL
NODE_HOME_NPX_CLI_JS="$NODE_HOME/node_modules/npm/bin/npx-cli.js"

## a path that will fail -f test on any posix bash
NPX_WSL_PATH="/.."

## WSL can run Windows binaries, so we have to give it the win32 path
## however, WSL bash tests against posix paths, so we need to construct that
## to know if npm is installed globally.
if [ "$IS_WSL" == "true" ]; then
NPX_WSL_PATH=`wslpath "$NPM_PREFIX_NPX_CLI_JS"`
fi
## overriding $NPX_CLI_JS on WSL
if [ -f "$NPM_PREFIX_NPX_CLI_JS" ] || [ -f "$NPX_WSL_PATH" ]; then
NPX_CLI_JS="$NPM_PREFIX_NPX_CLI_JS"
fi

# execute lint-staged
"$NODE_EXE" "$NPX_CLI_JS" lint-staged
Loading