From 50d6e2b25ad9c24a50dd56a353593bd48ec59837 Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Mon, 8 Jul 2024 13:31:45 +0300 Subject: [PATCH] fix: publishing (#765) * fix: publishing fix: refactor the NPM publishing and resolving * fix: use direct binaries when possible * fix: inherit stdio for npm-bundled --- internal/templates/hook.tmpl | 100 ++++++++++++++------------- packaging/npm-bundled/get-exe.js | 7 +- packaging/npm-bundled/package.json | 1 - packaging/npm-installer/install.js | 62 ++++++++++------- packaging/npm-installer/package.json | 4 -- packaging/pack.rb | 45 +++++++++--- packaging/rubygems/bin/lefthook | 10 ++- 7 files changed, 130 insertions(+), 99 deletions(-) diff --git a/internal/templates/hook.tmpl b/internal/templates/hook.tmpl index 0a6e1835..97237962 100644 --- a/internal/templates/hook.tmpl +++ b/internal/templates/hook.tmpl @@ -29,57 +29,61 @@ call_lefthook() {{end -}} else dir="$(git rev-parse --show-toplevel)" - if test -f "$dir/node_modules/lefthook/bin/index.js" + osArch=$(uname | tr '[:upper:]' '[:lower:]') + cpuArch=$(uname -m | sed 's/aarch64/arm64/;s/x86_64/x64/') + if test -f "$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook{{.Extension}}" + then + "$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook{{.Extension}}" "$@" + elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook{{.Extension}}" + then + "$dir/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook{{.Extension}}" "$@" + elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook{{.Extension}}" + then + "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook{{.Extension}}" "$@" + elif test -f "$dir/node_modules/lefthook/bin/index.js" then "$dir/node_modules/lefthook/bin/index.js" "$@" + {{ $extension := .Extension }} + {{- range .Roots -}} + elif test -f "$dir/{{.}}/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook{{.Extension}}" + then + "$dir/node_modules/lefthook-${osArch}-${cpuArch}/bin/lefthook{{.Extension}}" "$@" + elif test -f "$dir/{{.}}/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook{{$extension}}" + then + "$dir/{{.}}/node_modules/@evilmartians/lefthook/bin/lefthook-${osArch}-${cpuArch}/lefthook{{$extension}}" "$@" + elif test -f "$dir/{{.}}/node_modules/@evilmartians/lefthook-installer/bin/lefthook{{$extension}}" + then + "$dir/{{.}}/node_modules/@evilmartians/lefthook-installer/bin/lefthook{{$extension}}" "$@" + elif test -f "$dir/{{.}}/node_modules/lefthook/bin/index.js" + then + "$dir/{{.}}/node_modules/lefthook/bin/index.js" "$@" + {{end}} + elif bundle exec lefthook -h >/dev/null 2>&1 + then + bundle exec lefthook "$@" + elif yarn lefthook -h >/dev/null 2>&1 + then + yarn lefthook "$@" + elif pnpm lefthook -h >/dev/null 2>&1 + then + pnpm lefthook "$@" + elif swift package plugin lefthook >/dev/null 2>&1 + then + swift package --disable-sandbox plugin lefthook "$@" + elif command -v mint >/dev/null 2>&1 + then + mint run csjones/lefthook-plugin "$@" + elif command -v npx >/dev/null 2>&1 + then + npx lefthook "$@" else - osArch=$(uname | tr '[:upper:]' '[:lower:]') - cpuArch=$(uname -m | sed 's/aarch64/arm64/') - if test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" - then - "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" "$@" - elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" - then - "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" "$@" - {{ $extension := .Extension }} - {{- range .Roots -}} - elif test -f "$dir/{{.}}/node_modules/lefthook/bin/index.js" - then - "$dir/{{.}}/node_modules/lefthook/bin/index.js" "$@" - elif test -f "$dir/{{.}}/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{$extension}}" - then - "$dir/{{.}}/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{$extension}}" "$@" - elif test -f "$dir/{{.}}/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{$extension}}" - then - "$dir/{{.}}/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{$extension}}" "$@" - {{end}} - elif bundle exec lefthook -h >/dev/null 2>&1 - then - bundle exec lefthook "$@" - elif yarn lefthook -h >/dev/null 2>&1 - then - yarn lefthook "$@" - elif pnpm lefthook -h >/dev/null 2>&1 - then - pnpm lefthook "$@" - elif swift package plugin lefthook >/dev/null 2>&1 - then - swift package --disable-sandbox plugin lefthook "$@" - elif command -v mint >/dev/null 2>&1 - then - mint run csjones/lefthook-plugin "$@" - elif command -v npx >/dev/null 2>&1 - then - npx lefthook "$@" - else - echo "Can't find lefthook in PATH" - {{- if .AssertLefthookInstalled}} - echo "ERROR: Operation is aborted due to lefthook settings." - echo "Make sure lefthook is available in your environment and re-try." - echo "To skip these checks use --no-verify git argument or set LEFTHOOK=0 env variable." - exit 1 - {{- end}} - fi + echo "Can't find lefthook in PATH" + {{- if .AssertLefthookInstalled}} + echo "ERROR: Operation is aborted due to lefthook settings." + echo "Make sure lefthook is available in your environment and re-try." + echo "To skip these checks use --no-verify git argument or set LEFTHOOK=0 env variable." + exit 1 + {{- end}} fi fi } diff --git a/packaging/npm-bundled/get-exe.js b/packaging/npm-bundled/get-exe.js index a3a8de0a..192d335d 100644 --- a/packaging/npm-bundled/get-exe.js +++ b/packaging/npm-bundled/get-exe.js @@ -15,11 +15,6 @@ function getExePath() { let goArch = process.arch; let suffix = ''; switch (process.arch) { - case 'x64': { - goArch = 'amd64'; - suffix = '_v1'; // GOAMD64 - break; - } case 'x32': case 'ia32': { goArch = '386'; @@ -30,7 +25,7 @@ function getExePath() { const dir = path.join(__dirname, 'bin'); const executable = path.join( dir, - `lefthook_${goOS}_${goArch}${suffix}`, + `lefthook-${goOS}-${goArch}`, `lefthook${extension}` ); return executable; diff --git a/packaging/npm-bundled/package.json b/packaging/npm-bundled/package.json index 33e9d7b6..a2db4a68 100644 --- a/packaging/npm-bundled/package.json +++ b/packaging/npm-bundled/package.json @@ -30,7 +30,6 @@ "ia32" ], "scripts": { - "version": "git clean -fdX bin/ && (cd ../../dist/ && find . -maxdepth 2 -executable -type f -exec cp --parents \\{\\} ../packaging/npm-bundled/bin/ \\;) && cp -f ../../README.md ./", "postinstall": "node postinstall.js" } } diff --git a/packaging/npm-installer/install.js b/packaging/npm-installer/install.js index ff823f55..f0ed1946 100755 --- a/packaging/npm-installer/install.js +++ b/packaging/npm-installer/install.js @@ -1,4 +1,7 @@ -const { spawnSync } = require("child_process") +const http = require('https') +const fs = require('fs') +const path = require("path") +const chp = require("child_process") const iswin = ["win32", "cygwin"].includes(process.platform) @@ -6,15 +9,19 @@ async function install() { if (process.env.CI) { return } - const exePath = await downloadBinary() + const downloadURL = getDownloadURL() + const extension = iswin ? ".exe" : "" + const fileName = `lefthook${extension}` + const exePath = path.join(__dirname, "bin", fileName) + await downloadBinary(downloadURL, exePath) + console.log('downloaded to', exePath) if (!iswin) { - const { chmodSync } = require("fs") - chmodSync(exePath, "755") + fs.chmodSync(exePath, "755") } // run install - spawnSync(exePath, ["install", "-f"], { + chp.spawnSync(exePath, ['install', '-f'], { cwd: process.env.INIT_CWD || process.cwd(), - stdio: "inherit", + stdio: 'inherit', }) } @@ -46,28 +53,31 @@ function getDownloadURL() { return `https://github.com/evilmartians/lefthook/releases/download/v${version}/lefthook_${version}_${downloadOS}_${arch}${extension}` } -const { DownloaderHelper } = require("node-downloader-helper") -const path = require("path") +async function downloadBinary(url, dest) { + console.log('downloading', url) + const file = fs.createWriteStream(dest) + return new Promise((resolve, reject) => { + http.get(url, function(response) { + if (response.statusCode === 302 && response.headers.location) { + // If the response is a 302 redirect, follow the new location + downloadBinary(response.headers.location, dest) + .then(resolve) + .catch(reject) + } else { + response.pipe(file) -async function downloadBinary() { - // TODO zip the binaries to reduce the download size - const downloadURL = getDownloadURL() - const extension = iswin ? ".exe" : "" - const fileName = `lefthook${extension}` - const binDir = path.join(__dirname, "bin") - const dl = new DownloaderHelper(downloadURL, binDir, { - fileName, - retry: { maxRetries: 5, delay: 50 }, + file.on('finish', function() { + file.close(() => { + resolve(dest) + }) + }) + } + }).on('error', function(err) { + fs.unlink(file, () => { + reject(err) + }) + }) }) - dl.on("end", () => console.log("lefthook binary was downloaded")) - try { - await dl.start() - } catch(e) { - const message = `Failed to download ${fileName}: ${e.message} while fetching ${downloadURL}` - console.error(message) - throw new Error(message) - } - return path.join(binDir, fileName) } // start: diff --git a/packaging/npm-installer/package.json b/packaging/npm-installer/package.json index dae654d0..6afeae40 100644 --- a/packaging/npm-installer/package.json +++ b/packaging/npm-installer/package.json @@ -29,10 +29,6 @@ "arm64" ], "scripts": { - "version": "cp -f ../../README.md ./", "install": "node install.js" - }, - "dependencies": { - "node-downloader-helper": "^1.0.18" } } diff --git a/packaging/pack.rb b/packaging/pack.rb index e07c3235..1952bbdb 100755 --- a/packaging/pack.rb +++ b/packaging/pack.rb @@ -54,18 +54,47 @@ def put_binaries cd(__dir__) puts "Putting binaries to packages..." { - "#{DIST}/lefthook_linux_amd64_v1/lefthook" => "npm/lefthook-linux-x64/bin/lefthook", - "#{DIST}/lefthook_linux_arm64/lefthook" => "npm/lefthook-linux-arm64/bin/lefthook", - "#{DIST}/lefthook_freebsd_amd64_v1/lefthook" => "npm/lefthook-freebsd-x64/bin/lefthook", - "#{DIST}/lefthook_freebsd_arm64/lefthook" => "npm/lefthook-freebsd-arm64/bin/lefthook", - "#{DIST}/lefthook_windows_amd64_v1/lefthook.exe" => "npm/lefthook-windows-x64/bin/lefthook.exe", - "#{DIST}/lefthook_windows_arm64/lefthook.exe" => "npm/lefthook-windows-arm64/bin/lefthook.exe", - "#{DIST}/lefthook_darwin_amd64_v1/lefthook" => "npm/lefthook-darwin-x64/bin/lefthook", - "#{DIST}/lefthook_darwin_arm64/lefthook" => "npm/lefthook-darwin-arm64/bin/lefthook", + "#{DIST}/lefthook_linux_amd64_v1/lefthook" => "npm/lefthook-linux-x64/bin/lefthook", + "#{DIST}/lefthook_linux_arm64/lefthook" => "npm/lefthook-linux-arm64/bin/lefthook", + "#{DIST}/lefthook_freebsd_amd64_v1/lefthook" => "npm/lefthook-freebsd-x64/bin/lefthook", + "#{DIST}/lefthook_freebsd_arm64/lefthook" => "npm/lefthook-freebsd-arm64/bin/lefthook", + "#{DIST}/lefthook_windows_amd64_v1/lefthook.exe" => "npm/lefthook-windows-x64/bin/lefthook.exe", + "#{DIST}/lefthook_windows_arm64/lefthook.exe" => "npm/lefthook-windows-arm64/bin/lefthook.exe", + "#{DIST}/lefthook_darwin_amd64_v1/lefthook" => "npm/lefthook-darwin-x64/bin/lefthook", + "#{DIST}/lefthook_darwin_arm64/lefthook" => "npm/lefthook-darwin-arm64/bin/lefthook", }.each do |(source, dest)| mkdir_p(File.dirname(dest)) cp(source, dest, verbose: true) end + + { + "#{DIST}/lefthook_linux_amd64_v1/lefthook" => "npm-bundled/bin/lefthook-linux-x64/lefthook", + "#{DIST}/lefthook_linux_arm64/lefthook" => "npm-bundled/bin/lefthook-linux-arm64/lefthook", + "#{DIST}/lefthook_freebsd_amd64_v1/lefthook" => "npm-bundled/bin/lefthook-freebsd-x64/lefthook", + "#{DIST}/lefthook_freebsd_arm64/lefthook" => "npm-bundled/bin/lefthook-freebsd-arm64/lefthook", + "#{DIST}/lefthook_windows_amd64_v1/lefthook.exe" => "npm-bundled/bin/lefthook-windows-x64/lefthook.exe", + "#{DIST}/lefthook_windows_arm64/lefthook.exe" => "npm-bundled/bin/lefthook-windows-arm64/lefthook.exe", + "#{DIST}/lefthook_darwin_amd64_v1/lefthook" => "npm-bundled/bin/lefthook-darwin-x64/lefthook", + "#{DIST}/lefthook_darwin_arm64/lefthook" => "npm-bundled/bin/lefthook-darwin-arm64/lefthook", + }.each do |(source, dest)| + mkdir_p(File.dirname(dest)) + cp(source, dest, verbose: true) + end + + { + "#{DIST}/lefthook_linux_amd64_v1/lefthook" => "rubygems/libexec/lefthook-linux-x64/lefthook", + "#{DIST}/lefthook_linux_arm64/lefthook" => "rubygems/libexec/lefthook-linux-arm64/lefthook", + "#{DIST}/lefthook_freebsd_amd64_v1/lefthook" => "rubygems/libexec/lefthook-freebsd-x64/lefthook", + "#{DIST}/lefthook_freebsd_arm64/lefthook" => "rubygems/libexec/lefthook-freebsd-arm64/lefthook", + "#{DIST}/lefthook_windows_amd64_v1/lefthook.exe" => "rubygems/libexec/lefthook-windows-x64/lefthook.exe", + "#{DIST}/lefthook_windows_arm64/lefthook.exe" => "rubygems/libexec/lefthook-windows-arm64/lefthook.exe", + "#{DIST}/lefthook_darwin_amd64_v1/lefthook" => "rubygems/libexec/lefthook-darwin-x64/lefthook", + "#{DIST}/lefthook_darwin_arm64/lefthook" => "rubygems/libexec/lefthook-darwin-arm64/lefthook", + }.each do |(source, dest)| + mkdir_p(File.dirname(dest)) + cp(source, dest, verbose: true) + end + puts "done" end diff --git a/packaging/rubygems/bin/lefthook b/packaging/rubygems/bin/lefthook index 7638c420..77d6f808 100755 --- a/packaging/rubygems/bin/lefthook +++ b/packaging/rubygems/bin/lefthook @@ -7,8 +7,8 @@ arch = case platform.cpu.sub(/\Auniversal\./, '') when /\Aarm64/ then "arm64" # Apple reports arm64e on M1 macs when /aarch64/ then "arm64" - when "x86_64" then "amd64" - when "x64" then "amd64" # Windows with MINGW64 reports RUBY_PLATFORM as "x64-mingw32" + when "x86_64" then "x64" + when "x64" then "x64" # Windows with MINGW64 reports RUBY_PLATFORM as "x64-mingw32" else raise "Unknown architecture: #{platform.cpu}" end @@ -18,14 +18,12 @@ os = when "darwin" then "darwin" # MacOS when "windows" then "windows" when "mingw32" then "windows" # Windows with MINGW64 reports RUBY_PLATFORM as "x64-mingw32" - when "mingw" then "windows" + when "mingw" then "windows" when "freebsd" then "freebsd" else raise "Unknown OS: #{platform.os}" end -suffix = arch == "amd64" ? "_v1" : "" # GOAMD64 - -binary = "lefthook_#{os}_#{arch}#{suffix}/lefthook" +binary = "lefthook-#{os}-#{arch}/lefthook" binary = "#{binary}.exe" if os == "windows" args = $*.map { |x| x.include?(' ') ? "'" + x + "'" : x }