Skip to content

Commit

Permalink
Download the binaries inside postinstall
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed May 15, 2021
1 parent bf7b1a4 commit ecc4670
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 45 deletions.
6 changes: 3 additions & 3 deletions .npm/bin/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node

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

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.

3 changes: 3 additions & 0 deletions .npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
],
"scripts": {
"postinstall": "node postinstall.js"
},
"dependencies": {
"node-downloader-helper": "^1.0.18"
}
}
71 changes: 65 additions & 6 deletions .npm/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
if (!process.env.CI) {
const { spawnSync } = require('child_process');
const { getExePath } = require('./get-exe');
const { spawnSync } = require("child_process")

async function install() {
if (process.env.CI) {
return
}
const exePath = await downloadBinary()
// run install
spawnSync(getExePath(), ['install', '-f'], {
spawnSync(exePath, ["install", "-f"], {
cwd: process.env.INIT_CWD || process.cwd,
stdio: 'inherit',
});
stdio: "inherit",
})
}

function getDownloadURL() {
// 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"
}

// 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 = path.extname(downloadURL).slice(1)
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"))
await dl.start()
return path.join(binDir, fileName)
}

// start:
install().catch((e) => {
throw e
})

0 comments on commit ecc4670

Please sign in to comment.