diff --git a/.gitignore b/.gitignore index 412d011f..b9b5ee10 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.npm/bin/index.js b/.npm/bin/index.js index 2e64e83e..8ec5ffff 100755 --- a/.npm/bin/index.js +++ b/.npm/bin/index.js @@ -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" }); diff --git a/.npm/get-exe.js b/.npm/get-exe.js deleted file mode 100644 index 217578ba..00000000 --- a/.npm/get-exe.js +++ /dev/null @@ -1,36 +0,0 @@ -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/install.js b/.npm/install.js new file mode 100755 index 00000000..a9cb9919 --- /dev/null +++ b/.npm/install.js @@ -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 +}) diff --git a/.npm/package.json b/.npm/package.json index 042b6cbe..f97b3709 100644 --- a/.npm/package.json +++ b/.npm/package.json @@ -29,6 +29,9 @@ "x32" ], "scripts": { - "postinstall": "node postinstall.js" + "install": "node install.js" + }, + "dependencies": { + "node-downloader-helper": "^1.0.18" } } diff --git a/.npm/postinstall.js b/.npm/postinstall.js deleted file mode 100755 index 68708d9d..00000000 --- a/.npm/postinstall.js +++ /dev/null @@ -1,10 +0,0 @@ -if (!process.env.CI) { - const { spawnSync } = require('child_process'); - const { getExePath } = require('./get-exe'); - - // run install - spawnSync(getExePath(), ['install', '-f'], { - cwd: process.env.INIT_CWD || process.cwd, - stdio: 'inherit', - }); -}