Skip to content

Commit

Permalink
fix: publishing (#765)
Browse files Browse the repository at this point in the history
* fix: publishing
fix: refactor the NPM publishing and resolving

* fix: use direct binaries when possible

* fix: inherit stdio for npm-bundled
  • Loading branch information
mrexox committed Jul 8, 2024
1 parent 387c9d9 commit 50d6e2b
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 99 deletions.
100 changes: 52 additions & 48 deletions internal/templates/hook.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 1 addition & 6 deletions packaging/npm-bundled/get-exe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion packaging/npm-bundled/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
62 changes: 36 additions & 26 deletions packaging/npm-installer/install.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
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)

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',
})
}

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions packaging/npm-installer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
"arm64"
],
"scripts": {
"version": "cp -f ../../README.md ./",
"install": "node install.js"
},
"dependencies": {
"node-downloader-helper": "^1.0.18"
}
}
45 changes: 37 additions & 8 deletions packaging/pack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 4 additions & 6 deletions packaging/rubygems/bin/lefthook
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 }
Expand Down

0 comments on commit 50d6e2b

Please sign in to comment.