Skip to content

Commit

Permalink
refactor(gitHook): add multiple platform support
Browse files Browse the repository at this point in the history
update git hook `pre-commit` for multiple platforms
- add cygwin support (tested on windows 10 latest cygwin version)
- add WSL support (tested on windows 10 WSL ubuntu)
- add windows cmd support (tested commit on windows 10 cmd)
  • Loading branch information
dimaslanjaka committed Aug 25, 2023
2 parents f3226cf + 1fe6cc2 commit 34eaa35
Showing 1 changed file with 98 additions and 1 deletion.
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

0 comments on commit 34eaa35

Please sign in to comment.