diff --git a/packaging/npm/lefthook/bin.sh b/packaging/npm/lefthook/bin.sh new file mode 100644 index 00000000..7f204f95 --- /dev/null +++ b/packaging/npm/lefthook/bin.sh @@ -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}" "$@" diff --git a/packaging/npm/lefthook/bin/index.js b/packaging/npm/lefthook/bin/index.js deleted file mode 100755 index 2e64e83e..00000000 --- a/packaging/npm/lefthook/bin/index.js +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env node - -var spawn = require('child_process').spawn; -const { getExePath } = require('../get-exe'); - -var command_args = process.argv.slice(2); - -var child = spawn( - getExePath(), - command_args, - { stdio: "inherit" }); - -child.on('close', function (code) { - if (code !== 0) { - process.exit(1); - } -}); diff --git a/packaging/npm/lefthook/get-exe.js b/packaging/npm/lefthook/get-exe.js deleted file mode 100644 index ddf8f234..00000000 --- a/packaging/npm/lefthook/get-exe.js +++ /dev/null @@ -1,20 +0,0 @@ -const path = require("path"); - -function getExePath() { - // Detect OS - // https://nodejs.org/api/process.html#process_process_platform - let os = process.platform; - let extension = ""; - if (["win32", "cygwin"].includes(process.platform)) { - os = "windows"; - extension = ".exe"; - } - - // Detect architecture - // https://nodejs.org/api/process.html#process_process_arch - let arch = process.arch; - - return require.resolve(`lefthook-${os}-${arch}/bin/lefthook${extension}`); -} - -exports.getExePath = getExePath; diff --git a/packaging/npm/lefthook/package.json b/packaging/npm/lefthook/package.json index e844ff60..67893b8d 100644 --- a/packaging/npm/lefthook/package.json +++ b/packaging/npm/lefthook/package.json @@ -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", @@ -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" } } diff --git a/packaging/npm/lefthook/postinstall.js b/packaging/npm/lefthook/postinstall.js deleted file mode 100644 index 23e01eb8..00000000 --- a/packaging/npm/lefthook/postinstall.js +++ /dev/null @@ -1,21 +0,0 @@ -const { spawnSync } = require("child_process"); -const { getExePath } = require("./get-exe"); - -function install() { - if (process.env.CI) { - return; - } - - spawnSync(getExePath(), ["install", "-f"], { - cwd: process.env.INIT_CWD || process.cwd(), - stdio: "inherit", - }); -} - -try { - install(); -} catch (e) { - console.warn( - "'lefthook install' command failed. Try running it manually.\n" + e, - ); -}