@evilmartians/lefthook-installer 1.6.21 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/install.js +36 -26
  2. package/package.json +1 -5
package/install.js CHANGED
@@ -1,4 +1,7 @@
1
- const { spawnSync } = require("child_process")
1
+ const http = require('https')
2
+ const fs = require('fs')
3
+ const path = require("path")
4
+ const chp = require("child_process")
2
5
 
3
6
  const iswin = ["win32", "cygwin"].includes(process.platform)
4
7
 
@@ -6,15 +9,19 @@ async function install() {
6
9
  if (process.env.CI) {
7
10
  return
8
11
  }
9
- const exePath = await downloadBinary()
12
+ const downloadURL = getDownloadURL()
13
+ const extension = iswin ? ".exe" : ""
14
+ const fileName = `lefthook${extension}`
15
+ const exePath = path.join(__dirname, "bin", fileName)
16
+ await downloadBinary(downloadURL, exePath)
17
+ console.log('downloaded to', exePath)
10
18
  if (!iswin) {
11
- const { chmodSync } = require("fs")
12
- chmodSync(exePath, "755")
19
+ fs.chmodSync(exePath, "755")
13
20
  }
14
21
  // run install
15
- spawnSync(exePath, ["install", "-f"], {
22
+ chp.spawnSync(exePath, ['install', '-f'], {
16
23
  cwd: process.env.INIT_CWD || process.cwd(),
17
- stdio: "inherit",
24
+ stdio: 'inherit',
18
25
  })
19
26
  }
20
27
 
@@ -46,28 +53,31 @@ function getDownloadURL() {
46
53
  return `https://github.com/evilmartians/lefthook/releases/download/v${version}/lefthook_${version}_${downloadOS}_${arch}${extension}`
47
54
  }
48
55
 
49
- const { DownloaderHelper } = require("node-downloader-helper")
50
- const path = require("path")
56
+ async function downloadBinary(url, dest) {
57
+ console.log('downloading', url)
58
+ const file = fs.createWriteStream(dest)
59
+ return new Promise((resolve, reject) => {
60
+ http.get(url, function(response) {
61
+ if (response.statusCode === 302 && response.headers.location) {
62
+ // If the response is a 302 redirect, follow the new location
63
+ downloadBinary(response.headers.location, dest)
64
+ .then(resolve)
65
+ .catch(reject)
66
+ } else {
67
+ response.pipe(file)
51
68
 
52
- async function downloadBinary() {
53
- // TODO zip the binaries to reduce the download size
54
- const downloadURL = getDownloadURL()
55
- const extension = iswin ? ".exe" : ""
56
- const fileName = `lefthook${extension}`
57
- const binDir = path.join(__dirname, "bin")
58
- const dl = new DownloaderHelper(downloadURL, binDir, {
59
- fileName,
60
- retry: { maxRetries: 5, delay: 50 },
69
+ file.on('finish', function() {
70
+ file.close(() => {
71
+ resolve(dest)
72
+ })
73
+ })
74
+ }
75
+ }).on('error', function(err) {
76
+ fs.unlink(file, () => {
77
+ reject(err)
78
+ })
79
+ })
61
80
  })
62
- dl.on("end", () => console.log("lefthook binary was downloaded"))
63
- try {
64
- await dl.start()
65
- } catch(e) {
66
- const message = `Failed to download ${fileName}: ${e.message} while fetching ${downloadURL}`
67
- console.error(message)
68
- throw new Error(message)
69
- }
70
- return path.join(binDir, fileName)
71
81
  }
72
82
 
73
83
  // start:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evilmartians/lefthook-installer",
3
- "version": "1.6.21",
3
+ "version": "1.7.0",
4
4
  "description": "Simple git hooks manager",
5
5
  "main": "bin/index.js",
6
6
  "bin": {
@@ -29,10 +29,6 @@
29
29
  "arm64"
30
30
  ],
31
31
  "scripts": {
32
- "version": "cp -f ../../README.md ./",
33
32
  "install": "node install.js"
34
- },
35
- "dependencies": {
36
- "node-downloader-helper": "^1.0.18"
37
33
  }
38
34
  }