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

chore(npm): use bin.sh for execute #798

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
50 changes: 50 additions & 0 deletions packaging/npm/lefthook/bin.sh
dalisoft marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh
set -eu

CURRENT_DIR=$(dirname "$0")
IS_X_CALL=false
CACHE_DIR="."

BINARY_NAME='lefthook'
BINARY_NAME_SCOPE='lefthook-'

# Test if calling via `npx` or `bunx`
if "$(echo pwd)" | grep -q ".bun/install/cache"; then
IS_X_CALL=true
CACHE_DIR=$(bun pm cache ls)
elif "$(echo pwd)" | grep -q ".npm/_npx"; then
IS_X_CALL=true
CACHE_DIR=$(dirname "$(pwd)")
fi

# Search and find binary
if $IS_X_CALL; then
LEFTHOOK_BIN=$(find "${CACHE_DIR}" -iname "${BINARY_NAME}" | grep -s "${BINARY_NAME_SCOPE}" || echo "")
else
LEFTHOOK_BIN=$(find . -iname "${BINARY_NAME}" | grep -s "${BINARY_NAME_SCOPE}" || echo "")
fi

# Check node_modules
if [ -z "${LEFTHOOK_BIN}" ]; then
echo "\`node_modules\` was not installed"
exit 1
fi

# Trim variables after success checks
LEFTHOOK_BIN=$(realpath -q "${LEFTHOOK_BIN}")

# Make it executable
if test -f "${LEFTHOOK_BIN}"; then
chmod +x "${LEFTHOOK_BIN}"
fi

# Replace binary in `bin` field for later use
if test -f "${CURRENT_DIR}/package.json"; then
sed -i.bak "s|bin.sh|${LEFTHOOK_BIN}|g" "${CURRENT_DIR}/package.json"
rm -rf "package.json.bak"
elif echo "${CURRENT_DIR}" | grep -q ".bin"; then
ln -sf "${LEFTHOOK_BIN}" "$0"
fi

# Run currently until next run
"${LEFTHOOK_BIN}" "$@"
17 changes: 0 additions & 17 deletions packaging/npm/lefthook/bin/index.js

This file was deleted.

20 changes: 0 additions & 20 deletions packaging/npm/lefthook/get-exe.js

This file was deleted.

6 changes: 1 addition & 5 deletions packaging/npm/lefthook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
"type": "git",
"url": "git+https://github.com/evilmartians/lefthook.git"
},
"main": "bin/index.js",
"bin": {
"lefthook": "bin/index.js"
"lefthook": "bin.sh"
},
"keywords": [
"git",
Expand All @@ -31,8 +30,5 @@
"lefthook-freebsd-x64": "1.7.11",
"lefthook-windows-arm64": "1.7.11",
"lefthook-windows-x64": "1.7.11"
},
"scripts": {
"postinstall": "node postinstall.js"
}
}
21 changes: 0 additions & 21 deletions packaging/npm/lefthook/postinstall.js

This file was deleted.

Loading