From e19d646814a8d8b43782505e2019cdfd1997fa7d Mon Sep 17 00:00:00 2001 From: Davlatjon Shavkatov Date: Mon, 15 Apr 2024 13:48:53 +0500 Subject: [PATCH 1/7] perf(`npm/lefthook`): use `optionalDependencies` `bin` field --- .../npm/lefthook-darwin-arm64/package.json | 3 +++ packaging/npm/lefthook-darwin-x64/package.json | 3 +++ .../npm/lefthook-freebsd-arm64/package.json | 3 +++ packaging/npm/lefthook-freebsd-x64/package.json | 3 +++ packaging/npm/lefthook-linux-arm64/package.json | 3 +++ packaging/npm/lefthook-linux-x64/package.json | 3 +++ .../npm/lefthook-windows-arm64/package.json | 3 +++ packaging/npm/lefthook-windows-x64/package.json | 3 +++ packaging/npm/lefthook/bin/index.js | 17 ----------------- packaging/npm/lefthook/get-exe.js | 12 ++++++------ packaging/npm/lefthook/package.json | 4 ---- packaging/npm/lefthook/postinstall.js | 16 +++++++++------- 12 files changed, 39 insertions(+), 34 deletions(-) delete mode 100755 packaging/npm/lefthook/bin/index.js diff --git a/packaging/npm/lefthook-darwin-arm64/package.json b/packaging/npm/lefthook-darwin-arm64/package.json index 6b4fed01..09bebc31 100644 --- a/packaging/npm/lefthook-darwin-arm64/package.json +++ b/packaging/npm/lefthook-darwin-arm64/package.json @@ -2,6 +2,9 @@ "name": "lefthook-darwin-arm64", "version": "1.6.10", "description": "The macOS ARM 64-bit binary for lefthook, git hooks manager.", + "bin": { + "lefthook": "bin/lefthook" + }, "preferUnplugged": false, "repository": "https://github.com/evilmartians/lefthook", "license": "MIT", diff --git a/packaging/npm/lefthook-darwin-x64/package.json b/packaging/npm/lefthook-darwin-x64/package.json index 953557a1..843686aa 100644 --- a/packaging/npm/lefthook-darwin-x64/package.json +++ b/packaging/npm/lefthook-darwin-x64/package.json @@ -2,6 +2,9 @@ "name": "lefthook-darwin-x64", "version": "1.6.10", "description": "The macOS 64-bit binary for lefthook, git hooks manager.", + "bin": { + "lefthook": "bin/lefthook" + }, "preferUnplugged": false, "repository": "https://github.com/evilmartians/lefthook", "license": "MIT", diff --git a/packaging/npm/lefthook-freebsd-arm64/package.json b/packaging/npm/lefthook-freebsd-arm64/package.json index 0933fb77..35ba42b4 100644 --- a/packaging/npm/lefthook-freebsd-arm64/package.json +++ b/packaging/npm/lefthook-freebsd-arm64/package.json @@ -2,6 +2,9 @@ "name": "lefthook-freebsd-arm64", "version": "1.6.10", "description": "The FreeBSD ARM 64-bit binary for lefthook, git hooks manager.", + "bin": { + "lefthook": "bin/lefthook" + }, "preferUnplugged": false, "repository": "https://github.com/evilmartians/lefthook", "license": "MIT", diff --git a/packaging/npm/lefthook-freebsd-x64/package.json b/packaging/npm/lefthook-freebsd-x64/package.json index 24def5bc..1a6a6cdb 100644 --- a/packaging/npm/lefthook-freebsd-x64/package.json +++ b/packaging/npm/lefthook-freebsd-x64/package.json @@ -2,6 +2,9 @@ "name": "lefthook-freebsd-x64", "version": "1.6.10", "description": "The FreeBSD 64-bit binary for lefthook, git hooks manager.", + "bin": { + "lefthook": "bin/lefthook" + }, "preferUnplugged": false, "repository": "https://github.com/evilmartians/lefthook", "license": "MIT", diff --git a/packaging/npm/lefthook-linux-arm64/package.json b/packaging/npm/lefthook-linux-arm64/package.json index 77b2d154..d9215388 100644 --- a/packaging/npm/lefthook-linux-arm64/package.json +++ b/packaging/npm/lefthook-linux-arm64/package.json @@ -2,6 +2,9 @@ "name": "lefthook-linux-arm64", "version": "1.6.10", "description": "The Linux ARM 64-bit binary for lefthook, git hooks manager.", + "bin": { + "lefthook": "bin/lefthook" + }, "preferUnplugged": false, "repository": "https://github.com/evilmartians/lefthook", "license": "MIT", diff --git a/packaging/npm/lefthook-linux-x64/package.json b/packaging/npm/lefthook-linux-x64/package.json index a5af725e..ea67d312 100644 --- a/packaging/npm/lefthook-linux-x64/package.json +++ b/packaging/npm/lefthook-linux-x64/package.json @@ -2,6 +2,9 @@ "name": "lefthook-linux-x64", "version": "1.6.10", "description": "The Linux 64-bit binary for lefthook, git hooks manager.", + "bin": { + "lefthook": "bin/lefthook" + }, "preferUnplugged": false, "repository": "https://github.com/evilmartians/lefthook", "license": "MIT", diff --git a/packaging/npm/lefthook-windows-arm64/package.json b/packaging/npm/lefthook-windows-arm64/package.json index 07f4b40c..40d5f6c6 100644 --- a/packaging/npm/lefthook-windows-arm64/package.json +++ b/packaging/npm/lefthook-windows-arm64/package.json @@ -2,6 +2,9 @@ "name": "lefthook-windows-arm64", "version": "1.6.10", "description": "The Windows ARM 64-bit binary for lefthook, git hooks manager.", + "bin": { + "lefthook": "bin/lefthook" + }, "preferUnplugged": false, "repository": "https://github.com/evilmartians/lefthook", "license": "MIT", diff --git a/packaging/npm/lefthook-windows-x64/package.json b/packaging/npm/lefthook-windows-x64/package.json index 2ccfd235..f380c510 100644 --- a/packaging/npm/lefthook-windows-x64/package.json +++ b/packaging/npm/lefthook-windows-x64/package.json @@ -2,6 +2,9 @@ "name": "lefthook-windows-x64", "version": "1.6.10", "description": "The Windows 64-bit binary for lefthook, git hooks manager.", + "bin": { + "lefthook": "bin/lefthook" + }, "preferUnplugged": false, "repository": "https://github.com/evilmartians/lefthook", "license": "MIT", diff --git a/packaging/npm/lefthook/bin/index.js b/packaging/npm/lefthook/bin/index.js deleted file mode 100755 index 2e64e83e..00000000 --- a/packaging/npm/lefthook/bin/index.js +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env node - -var spawn = require('child_process').spawn; -const { getExePath } = require('../get-exe'); - -var command_args = process.argv.slice(2); - -var child = spawn( - getExePath(), - command_args, - { stdio: "inherit" }); - -child.on('close', function (code) { - if (code !== 0) { - process.exit(1); - } -}); diff --git a/packaging/npm/lefthook/get-exe.js b/packaging/npm/lefthook/get-exe.js index c67258ec..ddf8f234 100644 --- a/packaging/npm/lefthook/get-exe.js +++ b/packaging/npm/lefthook/get-exe.js @@ -1,20 +1,20 @@ -const path = require("path") +const path = require("path"); function getExePath() { // Detect OS // https://nodejs.org/api/process.html#process_process_platform let os = process.platform; - let extension = ''; - if (['win32', 'cygwin'].includes(process.platform)) { - os = 'windows'; - extension = '.exe'; + let extension = ""; + if (["win32", "cygwin"].includes(process.platform)) { + os = "windows"; + extension = ".exe"; } // Detect architecture // https://nodejs.org/api/process.html#process_process_arch let arch = process.arch; - return require.resolve(`lefthook-${os}-${arch}/bin/lefthook${extension}`) + return require.resolve(`lefthook-${os}-${arch}/bin/lefthook${extension}`); } exports.getExePath = getExePath; diff --git a/packaging/npm/lefthook/package.json b/packaging/npm/lefthook/package.json index f2f1c5fd..1465db28 100644 --- a/packaging/npm/lefthook/package.json +++ b/packaging/npm/lefthook/package.json @@ -2,11 +2,7 @@ "name": "lefthook", "version": "1.6.10", "description": "Simple git hooks manager", - "main": "index.js", "repository": "https://github.com/evilmartians/lefthook", - "bin": { - "lefthook": "bin/index.js" - }, "keywords": [ "git", "hook", diff --git a/packaging/npm/lefthook/postinstall.js b/packaging/npm/lefthook/postinstall.js index 200e7561..23e01eb8 100644 --- a/packaging/npm/lefthook/postinstall.js +++ b/packaging/npm/lefthook/postinstall.js @@ -1,19 +1,21 @@ -const { spawnSync } = require("child_process") -const { getExePath } = require("./get-exe") +const { spawnSync } = require("child_process"); +const { getExePath } = require("./get-exe"); function install() { if (process.env.CI) { - return + return; } spawnSync(getExePath(), ["install", "-f"], { cwd: process.env.INIT_CWD || process.cwd(), stdio: "inherit", - }) + }); } try { - install() -} catch(e) { - console.warn("'lefthook install' command failed. Try running it manually.\n" + e) + install(); +} catch (e) { + console.warn( + "'lefthook install' command failed. Try running it manually.\n" + e, + ); } From abe96c309d2b1255e0943d6c90f3e20329b6000b Mon Sep 17 00:00:00 2001 From: Davlatjon Shavkatov Date: Mon, 15 Apr 2024 13:49:53 +0500 Subject: [PATCH 2/7] perf(`npm/@evilmartians/lefthook`): update `binary` at postinstall --- packaging/npm-bundled/bin/index.js | 17 ------------ packaging/npm-bundled/get-exe.js | 39 ++++++++++++++-------------- packaging/npm-bundled/package.json | 3 --- packaging/npm-bundled/postinstall.js | 25 +++++++++++++++--- 4 files changed, 41 insertions(+), 43 deletions(-) delete mode 100644 packaging/npm-bundled/bin/index.js diff --git a/packaging/npm-bundled/bin/index.js b/packaging/npm-bundled/bin/index.js deleted file mode 100644 index 2e64e83e..00000000 --- a/packaging/npm-bundled/bin/index.js +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env node - -var spawn = require('child_process').spawn; -const { getExePath } = require('../get-exe'); - -var command_args = process.argv.slice(2); - -var child = spawn( - getExePath(), - command_args, - { stdio: "inherit" }); - -child.on('close', function (code) { - if (code !== 0) { - process.exit(1); - } -}); diff --git a/packaging/npm-bundled/get-exe.js b/packaging/npm-bundled/get-exe.js index a3a8de0a..9a47d8ea 100644 --- a/packaging/npm-bundled/get-exe.js +++ b/packaging/npm-bundled/get-exe.js @@ -1,38 +1,39 @@ -const path = require("path") +const path = require("path"); -function getExePath() { +function getRelativeExePath() { // 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'; + 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; - let suffix = ''; + let suffix = ""; switch (process.arch) { - case 'x64': { - goArch = 'amd64'; - suffix = '_v1'; // GOAMD64 + case "x64": { + goArch = "amd64"; + suffix = "_v1"; // GOAMD64 break; } - case 'x32': - case 'ia32': { - goArch = '386'; + case "x32": + case "ia32": { + goArch = "386"; break; } } - const dir = path.join(__dirname, 'bin'); - const executable = path.join( - dir, - `lefthook_${goOS}_${goArch}${suffix}`, - `lefthook${extension}` - ); + return [`lefthook_${goOS}_${goArch}${suffix}`, `lefthook${extension}`]; +} +function getExePath() { + const dir = path.join(__dirname, "bin"); + const [folder, file] = getRelativeExePath(); + const executable = path.join(dir, folder, file); return executable; } +exports.getRelativeExePath = getRelativeExePath; exports.getExePath = getExePath; diff --git a/packaging/npm-bundled/package.json b/packaging/npm-bundled/package.json index 61c10c70..e1994b67 100644 --- a/packaging/npm-bundled/package.json +++ b/packaging/npm-bundled/package.json @@ -3,9 +3,6 @@ "version": "1.6.10", "description": "Simple git hooks manager", "main": "index.js", - "bin": { - "lefthook": "./bin/index.js" - }, "repository": "https://github.com/evilmartians/lefthook", "keywords": [ "git", diff --git a/packaging/npm-bundled/postinstall.js b/packaging/npm-bundled/postinstall.js index 2300daea..1e7a5d50 100644 --- a/packaging/npm-bundled/postinstall.js +++ b/packaging/npm-bundled/postinstall.js @@ -1,10 +1,27 @@ if (!process.env.CI) { - const { spawnSync } = require('child_process'); - const { getExePath } = require('./get-exe'); + const { spawnSync } = require("child_process"); + const { getExePath, getRelativeExePath } = require("./get-exe"); + const { writeFileSync } = require("fs"); + const path = require("path"); + const pkg = require("./package.json"); // run install - spawnSync(getExePath(), ['install', '-f'], { + spawnSync(getExePath(), ["install", "-f"], { cwd: process.env.INIT_CWD || process.cwd(), - stdio: 'inherit', + stdio: "inherit", }); + + writeFileSync( + path.resolve(__dirname, "package.json"), + JSON.stringify( + { + ...pkg, + bin: { + dprint: "bin/" + getRelativeExePath().join("/"), + }, + }, + null, + 2, + ), + ); } From a398cd7ea39f698bdef73513d25af00cbb12c15d Mon Sep 17 00:00:00 2001 From: Davlatjon Shavkatov Date: Mon, 15 Apr 2024 13:50:38 +0500 Subject: [PATCH 3/7] perf(`@evilmartians/lefthook-installer`): use `postinstall` binary --- packaging/npm-installer/bin/index.js | 18 ------ packaging/npm-installer/install.js | 91 +++++++++++++++++----------- packaging/npm-installer/package.json | 3 - 3 files changed, 57 insertions(+), 55 deletions(-) delete mode 100755 packaging/npm-installer/bin/index.js diff --git a/packaging/npm-installer/bin/index.js b/packaging/npm-installer/bin/index.js deleted file mode 100755 index 8ec5ffff..00000000 --- a/packaging/npm-installer/bin/index.js +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env node - -var spawn = require('child_process').spawn; -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( - exePath, - command_args, - { stdio: "inherit" }); - -child.on('close', function (code) { - if (code !== 0) { - process.exit(1); - } -}); diff --git a/packaging/npm-installer/install.js b/packaging/npm-installer/install.js index ff823f55..c6f63a4b 100755 --- a/packaging/npm-installer/install.js +++ b/packaging/npm-installer/install.js @@ -1,76 +1,99 @@ -const { spawnSync } = require("child_process") +const { spawnSync } = require("child_process"); +const { writeFileSync } = require("fs"); +const path = require("path"); +const pkg = require("./package.json"); -const iswin = ["win32", "cygwin"].includes(process.platform) +const iswin = ["win32", "cygwin"].includes(process.platform); async function install() { if (process.env.CI) { - return + return; } - const exePath = await downloadBinary() + const exePath = await downloadBinary(); if (!iswin) { - const { chmodSync } = require("fs") - chmodSync(exePath, "755") + const { chmodSync } = require("fs"); + chmodSync(exePath, "755"); } + // set binary as direct run + setBinary(); // run install spawnSync(exePath, ["install", "-f"], { cwd: process.env.INIT_CWD || process.cwd(), stdio: "inherit", - }) + }); +} + +function setBinary() { + const extension = iswin ? ".exe" : ""; + const fileName = `lefthook${extension}`; + + writeFileSync( + path.resolve(__dirname, "package.json"), + JSON.stringify( + { + ...pkg, + bin: { + dprint: "bin/" + fileName, + }, + }, + null, + 2, + ), + ); } function getDownloadURL() { // Detect OS // https://nodejs.org/api/process.html#process_process_platform - let goOS = process.platform - let extension = "" + let goOS = process.platform; + let extension = ""; if (iswin) { - goOS = "windows" - extension = ".exe" + 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)}` + 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 + let arch = process.arch; switch (process.arch) { case "x64": { - arch = "x86_64" - break + arch = "x86_64"; + break; } } - const version = require("./package.json").version + const version = require("./package.json").version; - return `https://github.com/evilmartians/lefthook/releases/download/v${version}/lefthook_${version}_${downloadOS}_${arch}${extension}` + 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") +const { DownloaderHelper } = require("node-downloader-helper"); 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 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")) + }); + 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) + 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) + return path.join(binDir, fileName); } // start: install().catch((e) => { - throw e -}) + throw e; +}); diff --git a/packaging/npm-installer/package.json b/packaging/npm-installer/package.json index 58493b95..95224cb5 100644 --- a/packaging/npm-installer/package.json +++ b/packaging/npm-installer/package.json @@ -3,9 +3,6 @@ "version": "1.6.10", "description": "Simple git hooks manager", "main": "index.js", - "bin": { - "lefthook": "./bin/index.js" - }, "repository": "https://github.com/evilmartians/lefthook", "keywords": [ "git", From 13cb88e35deeca57e09bb30277e98dea27871f23 Mon Sep 17 00:00:00 2001 From: Davlatjon Shavkatov Date: Mon, 15 Apr 2024 13:54:34 +0500 Subject: [PATCH 4/7] fix(`@evilmartians/lefthook`): typo fix --- packaging/npm-bundled/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packaging/npm-bundled/package.json b/packaging/npm-bundled/package.json index e1994b67..be453bdd 100644 --- a/packaging/npm-bundled/package.json +++ b/packaging/npm-bundled/package.json @@ -2,7 +2,6 @@ "name": "@evilmartians/lefthook", "version": "1.6.10", "description": "Simple git hooks manager", - "main": "index.js", "repository": "https://github.com/evilmartians/lefthook", "keywords": [ "git", From 2787b4aef577be452361cb04ff923c4a1f37c554 Mon Sep 17 00:00:00 2001 From: Davlatjon Shavkatov Date: Mon, 15 Apr 2024 13:54:48 +0500 Subject: [PATCH 5/7] fix(`@evilmartians/lefthook-installer`): typo fix --- packaging/npm-installer/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packaging/npm-installer/package.json b/packaging/npm-installer/package.json index 95224cb5..7f5e2671 100644 --- a/packaging/npm-installer/package.json +++ b/packaging/npm-installer/package.json @@ -2,7 +2,6 @@ "name": "@evilmartians/lefthook-installer", "version": "1.6.10", "description": "Simple git hooks manager", - "main": "index.js", "repository": "https://github.com/evilmartians/lefthook", "keywords": [ "git", From 9eb8c534d6f38d3d20e2ea960afa14e9b2245742 Mon Sep 17 00:00:00 2001 From: Davlatjon Shavkatov Date: Mon, 15 Apr 2024 16:20:35 +0500 Subject: [PATCH 6/7] revert(`@npm-*`): restore old behavior and fix `pkg` `main` field --- packaging/npm-bundled/get-exe.js | 39 ++++++------ packaging/npm-bundled/package.json | 4 ++ packaging/npm-bundled/postinstall.js | 25 ++------ packaging/npm-installer/bin/index.js | 18 ++++++ packaging/npm-installer/install.js | 91 +++++++++++----------------- packaging/npm-installer/package.json | 4 ++ 6 files changed, 83 insertions(+), 98 deletions(-) create mode 100755 packaging/npm-installer/bin/index.js diff --git a/packaging/npm-bundled/get-exe.js b/packaging/npm-bundled/get-exe.js index 9a47d8ea..a3a8de0a 100644 --- a/packaging/npm-bundled/get-exe.js +++ b/packaging/npm-bundled/get-exe.js @@ -1,39 +1,38 @@ -const path = require("path"); +const path = require("path") -function getRelativeExePath() { +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"; + 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; - let suffix = ""; + let suffix = ''; switch (process.arch) { - case "x64": { - goArch = "amd64"; - suffix = "_v1"; // GOAMD64 + case 'x64': { + goArch = 'amd64'; + suffix = '_v1'; // GOAMD64 break; } - case "x32": - case "ia32": { - goArch = "386"; + case 'x32': + case 'ia32': { + goArch = '386'; break; } } - return [`lefthook_${goOS}_${goArch}${suffix}`, `lefthook${extension}`]; -} -function getExePath() { - const dir = path.join(__dirname, "bin"); - const [folder, file] = getRelativeExePath(); - const executable = path.join(dir, folder, file); + const dir = path.join(__dirname, 'bin'); + const executable = path.join( + dir, + `lefthook_${goOS}_${goArch}${suffix}`, + `lefthook${extension}` + ); return executable; } -exports.getRelativeExePath = getRelativeExePath; exports.getExePath = getExePath; diff --git a/packaging/npm-bundled/package.json b/packaging/npm-bundled/package.json index be453bdd..9c626244 100644 --- a/packaging/npm-bundled/package.json +++ b/packaging/npm-bundled/package.json @@ -2,6 +2,10 @@ "name": "@evilmartians/lefthook", "version": "1.6.10", "description": "Simple git hooks manager", + "main": "bin/index.js", + "bin": { + "lefthook": "./bin/index.js" + }, "repository": "https://github.com/evilmartians/lefthook", "keywords": [ "git", diff --git a/packaging/npm-bundled/postinstall.js b/packaging/npm-bundled/postinstall.js index 1e7a5d50..2300daea 100644 --- a/packaging/npm-bundled/postinstall.js +++ b/packaging/npm-bundled/postinstall.js @@ -1,27 +1,10 @@ if (!process.env.CI) { - const { spawnSync } = require("child_process"); - const { getExePath, getRelativeExePath } = require("./get-exe"); - const { writeFileSync } = require("fs"); - const path = require("path"); - const pkg = require("./package.json"); + const { spawnSync } = require('child_process'); + const { getExePath } = require('./get-exe'); // run install - spawnSync(getExePath(), ["install", "-f"], { + spawnSync(getExePath(), ['install', '-f'], { cwd: process.env.INIT_CWD || process.cwd(), - stdio: "inherit", + stdio: 'inherit', }); - - writeFileSync( - path.resolve(__dirname, "package.json"), - JSON.stringify( - { - ...pkg, - bin: { - dprint: "bin/" + getRelativeExePath().join("/"), - }, - }, - null, - 2, - ), - ); } diff --git a/packaging/npm-installer/bin/index.js b/packaging/npm-installer/bin/index.js new file mode 100755 index 00000000..8ec5ffff --- /dev/null +++ b/packaging/npm-installer/bin/index.js @@ -0,0 +1,18 @@ +#!/usr/bin/env node + +var spawn = require('child_process').spawn; +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( + exePath, + command_args, + { stdio: "inherit" }); + +child.on('close', function (code) { + if (code !== 0) { + process.exit(1); + } +}); diff --git a/packaging/npm-installer/install.js b/packaging/npm-installer/install.js index c6f63a4b..ff823f55 100755 --- a/packaging/npm-installer/install.js +++ b/packaging/npm-installer/install.js @@ -1,99 +1,76 @@ -const { spawnSync } = require("child_process"); -const { writeFileSync } = require("fs"); -const path = require("path"); -const pkg = require("./package.json"); +const { spawnSync } = require("child_process") -const iswin = ["win32", "cygwin"].includes(process.platform); +const iswin = ["win32", "cygwin"].includes(process.platform) async function install() { if (process.env.CI) { - return; + return } - const exePath = await downloadBinary(); + const exePath = await downloadBinary() if (!iswin) { - const { chmodSync } = require("fs"); - chmodSync(exePath, "755"); + const { chmodSync } = require("fs") + chmodSync(exePath, "755") } - // set binary as direct run - setBinary(); // run install spawnSync(exePath, ["install", "-f"], { cwd: process.env.INIT_CWD || process.cwd(), stdio: "inherit", - }); -} - -function setBinary() { - const extension = iswin ? ".exe" : ""; - const fileName = `lefthook${extension}`; - - writeFileSync( - path.resolve(__dirname, "package.json"), - JSON.stringify( - { - ...pkg, - bin: { - dprint: "bin/" + fileName, - }, - }, - null, - 2, - ), - ); + }) } function getDownloadURL() { // Detect OS // https://nodejs.org/api/process.html#process_process_platform - let goOS = process.platform; - let extension = ""; + let goOS = process.platform + let extension = "" if (iswin) { - goOS = "windows"; - extension = ".exe"; + 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)}`; + 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; + let arch = process.arch switch (process.arch) { case "x64": { - arch = "x86_64"; - break; + arch = "x86_64" + break } } - const version = require("./package.json").version; + const version = require("./package.json").version - return `https://github.com/evilmartians/lefthook/releases/download/v${version}/lefthook_${version}_${downloadOS}_${arch}${extension}`; + return `https://github.com/evilmartians/lefthook/releases/download/v${version}/lefthook_${version}_${downloadOS}_${arch}${extension}` } -const { DownloaderHelper } = require("node-downloader-helper"); +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 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")); + }) + 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); + 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); + return path.join(binDir, fileName) } // start: install().catch((e) => { - throw e; -}); + throw e +}) diff --git a/packaging/npm-installer/package.json b/packaging/npm-installer/package.json index 7f5e2671..ce3cc868 100644 --- a/packaging/npm-installer/package.json +++ b/packaging/npm-installer/package.json @@ -2,6 +2,10 @@ "name": "@evilmartians/lefthook-installer", "version": "1.6.10", "description": "Simple git hooks manager", + "main": "bin/index.js", + "bin": { + "lefthook": "./bin/index.js" + }, "repository": "https://github.com/evilmartians/lefthook", "keywords": [ "git", From aa1c52e2ed4a57985c478e173bb911430eb52bde Mon Sep 17 00:00:00 2001 From: Davlatjon Shavkatov Date: Mon, 15 Apr 2024 16:31:51 +0500 Subject: [PATCH 7/7] chore(`npm-bundled`): add missing `bin` removed on prev commit --- packaging/npm-bundled/bin/index.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 packaging/npm-bundled/bin/index.js diff --git a/packaging/npm-bundled/bin/index.js b/packaging/npm-bundled/bin/index.js new file mode 100644 index 00000000..2e64e83e --- /dev/null +++ b/packaging/npm-bundled/bin/index.js @@ -0,0 +1,17 @@ +#!/usr/bin/env node + +var spawn = require('child_process').spawn; +const { getExePath } = require('../get-exe'); + +var command_args = process.argv.slice(2); + +var child = spawn( + getExePath(), + command_args, + { stdio: "inherit" }); + +child.on('close', function (code) { + if (code !== 0) { + process.exit(1); + } +});