diff --git a/.npm/bin/index.js b/.npm/bin/index.js index 2ada9ce7..2e64e83e 100755 --- a/.npm/bin/index.js +++ b/.npm/bin/index.js @@ -1,14 +1,14 @@ #!/usr/bin/env node var spawn = require('child_process').spawn; -var path = require('path'); +const { getExePath } = require('../get-exe'); var command_args = process.argv.slice(2); var child = spawn( - path.join(__dirname, 'lefthook'), + getExePath(), command_args, - { stdio: [process.stdin, process.stdout, process.stderr] }); + { stdio: "inherit" }); child.on('close', function (code) { if (code !== 0) { diff --git a/.npm/bin/lefthook b/.npm/bin/lefthook deleted file mode 100755 index 278e5494..00000000 --- a/.npm/bin/lefthook +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/sh - -dir="$(cd "$(dirname "$0")" >/dev/null 2>&1 || exit ; pwd -P)" - -OS=$(uname -s) -ARCH=$(uname -m) -EXT= - -# Detect OS -case $OS in - Linux) - GOOS=linux - ;; - Darwin) - GOOS=darwin - ;; - Windows*|CYGWIN*|MSYS_NT*|MINGW*) - GOOS=windows - EXT=.exe - ;; - *) - echo "Unsupported OS: ${OS}" - exit 1 - ;; -esac - -# Detect architecture -case $ARCH in - x86_64) - GOARCH=amd64 - ;; - x86) - GOARCH=386 - ;; - arm64) - GOOS=arm64 - ;; - *) - echo "Unsupported CPU architecture: ${ARCH}" - exit 1 - ;; -esac - -executable="$dir/lefthook_${GOOS}_${GOARCH}/lefthook${EXT}" - -if [ -x "$executable" ]; then - exec "$executable" "$@" -else - echo "Lefthook wasn't build for ${OS} ${ARCH}" - exit 1 -fi diff --git a/.npm/get-exe.js b/.npm/get-exe.js new file mode 100644 index 00000000..217578ba --- /dev/null +++ b/.npm/get-exe.js @@ -0,0 +1,36 @@ +const path = require("path") + +function getExePath() { + // Detect OS + // https://nodejs.org/api/process.html#process_process_platform + let goOS = process.platform; + let extension = ''; + if (['win32', 'cygwin'].includes(process.platform)) { + goOS = 'windows'; + extension = '.exe'; + } + + // Detect architecture + // https://nodejs.org/api/process.html#process_process_arch + let goArch = process.arch; + switch (process.arch) { + case 'x64': { + goArch = 'amd64'; + break; + } + case 'x32': + case 'ia32': { + goArch = '386'; + break; + } + } + + const dir = path.join(__dirname, 'bin'); + const executable = path.join( + dir, + `lefthook_${goOS}_${goArch}`, + `lefthook${extension}` + ); + return executable; +} +exports.getExePath = getExePath; diff --git a/.npm/postinstall.js b/.npm/postinstall.js index 93fb2fd1..68708d9d 100755 --- a/.npm/postinstall.js +++ b/.npm/postinstall.js @@ -1,13 +1,10 @@ -const { spawn } = require("child_process"); -const { join } = require("path"); +if (!process.env.CI) { + const { spawnSync } = require('child_process'); + const { getExePath } = require('./get-exe'); -const isCI = process.env.CI; - -if (!isCI) { - binpath = join(__dirname, 'bin', 'lefthook'); - - result = spawn(binpath, ["install", "-f"], { - cwd: process.env.INIT_CWD, - stdio: [process.stdin, process.stdout, process.stderr] + // run install + spawnSync(getExePath(), ['install', '-f'], { + cwd: process.env.INIT_CWD || process.cwd, + stdio: 'inherit', }); }