From e519fc30e3491b06edc5b3204c4f79ec57887175 Mon Sep 17 00:00:00 2001 From: Davlatjon Shavkatov Date: Wed, 31 Jul 2024 01:10:25 +0500 Subject: [PATCH 1/2] chore(`npm`): use `bin.sh` for execute This should solve few issues related to bun and it has a `bunx`/`npx` support compared to https://github.com/evilmartians/lefthook/pull/705 This approach does not work on `pnpm` and does not reduce startup time. For `npx` it is faster but not much, for `bunx` it is marginally faster --- packaging/npm/lefthook/bin.sh | 50 +++++++++++++++++++++++++++ packaging/npm/lefthook/bin/index.js | 17 --------- packaging/npm/lefthook/get-exe.js | 20 ----------- packaging/npm/lefthook/package.json | 6 +--- packaging/npm/lefthook/postinstall.js | 21 ----------- 5 files changed, 51 insertions(+), 63 deletions(-) create mode 100644 packaging/npm/lefthook/bin.sh delete mode 100755 packaging/npm/lefthook/bin/index.js delete mode 100644 packaging/npm/lefthook/get-exe.js delete mode 100644 packaging/npm/lefthook/postinstall.js diff --git a/packaging/npm/lefthook/bin.sh b/packaging/npm/lefthook/bin.sh new file mode 100644 index 00000000..f0f9b823 --- /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 + BIOME_BIN=$(find "${CACHE_DIR}" -iname "${BINARY_NAME}" | grep -s "${BINARY_NAME_SCOPE}" || echo "") +else + BIOME_BIN=$(find . -iname "${BINARY_NAME}" | grep -s "${BINARY_NAME_SCOPE}" || echo "") +fi + +# Check node_modules +if [ -z "${BIOME_BIN}" ]; then + echo "\`node_modules\` was not installed" + exit 1 +fi + +# Trim variables after success checks +BIOME_BIN=$(realpath -q "${BIOME_BIN}") + +# Make it executable +if test -f "${BIOME_BIN}"; then + chmod +x "${BIOME_BIN}" +fi + +# Replace binary in `bin` field for later use +if test -f "${CURRENT_DIR}/package.json"; then + sed -i.bak "s|bin.sh|${BIOME_BIN}|g" "${CURRENT_DIR}/package.json" + rm -rf "package.json.bak" +elif echo "${CURRENT_DIR}" | grep -q ".bin"; then + ln -sf "${BIOME_BIN}" "$0" +fi + +# Run currently until next run +"${BIOME_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, - ); -} From 60fc11ee9050d973930e8b70c8961c8e8223a044 Mon Sep 17 00:00:00 2001 From: Davlatjon Shavkatov Date: Wed, 31 Jul 2024 01:13:31 +0500 Subject: [PATCH 2/2] fix: typo fix for variable name --- packaging/npm/lefthook/bin.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packaging/npm/lefthook/bin.sh b/packaging/npm/lefthook/bin.sh index f0f9b823..7f204f95 100644 --- a/packaging/npm/lefthook/bin.sh +++ b/packaging/npm/lefthook/bin.sh @@ -19,32 +19,32 @@ fi # Search and find binary if $IS_X_CALL; then - BIOME_BIN=$(find "${CACHE_DIR}" -iname "${BINARY_NAME}" | grep -s "${BINARY_NAME_SCOPE}" || echo "") + LEFTHOOK_BIN=$(find "${CACHE_DIR}" -iname "${BINARY_NAME}" | grep -s "${BINARY_NAME_SCOPE}" || echo "") else - BIOME_BIN=$(find . -iname "${BINARY_NAME}" | grep -s "${BINARY_NAME_SCOPE}" || echo "") + LEFTHOOK_BIN=$(find . -iname "${BINARY_NAME}" | grep -s "${BINARY_NAME_SCOPE}" || echo "") fi # Check node_modules -if [ -z "${BIOME_BIN}" ]; then +if [ -z "${LEFTHOOK_BIN}" ]; then echo "\`node_modules\` was not installed" exit 1 fi # Trim variables after success checks -BIOME_BIN=$(realpath -q "${BIOME_BIN}") +LEFTHOOK_BIN=$(realpath -q "${LEFTHOOK_BIN}") # Make it executable -if test -f "${BIOME_BIN}"; then - chmod +x "${BIOME_BIN}" +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|${BIOME_BIN}|g" "${CURRENT_DIR}/package.json" + 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 "${BIOME_BIN}" "$0" + ln -sf "${LEFTHOOK_BIN}" "$0" fi # Run currently until next run -"${BIOME_BIN}" "$@" +"${LEFTHOOK_BIN}" "$@"