Skip to content

Commit

Permalink
Npm: download the required binaries during installation (#188)
Browse files Browse the repository at this point in the history
* Track .npm/bin/index.js

* Download the binaries inside postinstall

* fix: give proper permissions to the downloaded executables

* fix: use better error message in case downloading fails

* Use platform detection to determine extension of the downloaded binary

* Add the extension to eval lefthook

* Both output console error and wrap custom message

* It is matter of taste, but let it be install script, not postinstall

Co-authored-by: Andrey Novikov <[email protected]>
  • Loading branch information
aminya and Envek committed Jun 2, 2021
1 parent df55fd9 commit aa26a08
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ dist/
.rubygems/pkg/
.rubygems/libexec/
.npm/bin/
!.npm/bin/*.js
package.json
!.npm/package.json
node_modules/
yarn.lock
package-lock.json
7 changes: 4 additions & 3 deletions .npm/bin/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env node

var spawn = require('child_process').spawn;
const { getExePath } = require('../get-exe');
const path = require("path")
const extension = ["win32", "cygwin"].includes(process.platform) ? ".exe" : ""
const exePath = path.join(__dirname, `lefthook${extension}`)

var command_args = process.argv.slice(2);

var child = spawn(
getExePath(),
exePath,
command_args,
{ stdio: "inherit" });

Expand Down
36 changes: 0 additions & 36 deletions .npm/get-exe.js

This file was deleted.

81 changes: 81 additions & 0 deletions .npm/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const { spawnSync } = require("child_process")

const iswin = ["win32", "cygwin"].includes(process.platform)

async function install() {
if (process.env.CI) {
return
}
const exePath = await downloadBinary()
if (!iswin) {
const { chmodSync } = require("fs")
chmodSync(exePath, "755")
}
// run install
spawnSync(exePath, ["install", "-f"], {
cwd: process.env.INIT_CWD || process.cwd,
stdio: "inherit",
})
}

function getDownloadURL() {
// Detect OS
// https://nodejs.org/api/process.html#process_process_platform
let goOS = process.platform
let extension = ""
if (iswin) {
goOS = "windows"
extension = ".exe"
}

// Convert the goOS to the os name in the download URL
let downloadOS = goOS === "darwin" ? "macOS" : goOS
downloadOS = `${downloadOS.charAt(0).toUpperCase()}${downloadOS.slice(1)}`

// Detect architecture
// https://nodejs.org/api/process.html#process_process_arch
let arch = process.arch
switch (process.arch) {
case "x64": {
arch = "x86_64"
break
}
case "x32":
case "ia32": {
arch = "i386"
break
}
}
const version = require("./package.json").version

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() {
// 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 },
})
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:
install().catch((e) => {
throw e
})
5 changes: 4 additions & 1 deletion .npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"ia32"
],
"scripts": {
"postinstall": "node postinstall.js"
"install": "node install.js"
},
"dependencies": {
"node-downloader-helper": "^1.0.18"
}
}
10 changes: 0 additions & 10 deletions .npm/postinstall.js

This file was deleted.

0 comments on commit aa26a08

Please sign in to comment.