@evilmartians/lefthook-installer 1.10.6 → 1.10.7
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.
- package/install.js +86 -0
- package/package.json +2 -1
package/install.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const http = require('https')
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
const path = require("path")
|
|
4
|
+
const chp = require("child_process")
|
|
5
|
+
|
|
6
|
+
const iswin = ["win32", "cygwin"].includes(process.platform)
|
|
7
|
+
|
|
8
|
+
async function install() {
|
|
9
|
+
if (process.env.CI) {
|
|
10
|
+
return
|
|
11
|
+
}
|
|
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)
|
|
18
|
+
if (!iswin) {
|
|
19
|
+
fs.chmodSync(exePath, "755")
|
|
20
|
+
}
|
|
21
|
+
// run install
|
|
22
|
+
chp.spawnSync(exePath, ['install', '-f'], {
|
|
23
|
+
cwd: process.env.INIT_CWD || process.cwd(),
|
|
24
|
+
stdio: 'inherit',
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function getDownloadURL() {
|
|
29
|
+
// Detect OS
|
|
30
|
+
// https://nodejs.org/api/process.html#process_process_platform
|
|
31
|
+
let goOS = process.platform
|
|
32
|
+
let extension = ""
|
|
33
|
+
if (iswin) {
|
|
34
|
+
goOS = "windows"
|
|
35
|
+
extension = ".exe"
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Convert the goOS to the os name in the download URL
|
|
39
|
+
let downloadOS = goOS === "darwin" ? "macOS" : goOS
|
|
40
|
+
downloadOS = `${downloadOS.charAt(0).toUpperCase()}${downloadOS.slice(1)}`
|
|
41
|
+
|
|
42
|
+
// Detect architecture
|
|
43
|
+
// https://nodejs.org/api/process.html#process_process_arch
|
|
44
|
+
let arch = process.arch
|
|
45
|
+
switch (process.arch) {
|
|
46
|
+
case "x64": {
|
|
47
|
+
arch = "x86_64"
|
|
48
|
+
break
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const version = require("./package.json").version
|
|
52
|
+
|
|
53
|
+
return `https://github.com/evilmartians/lefthook/releases/download/v${version}/lefthook_${version}_${downloadOS}_${arch}${extension}`
|
|
54
|
+
}
|
|
55
|
+
|
|
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)
|
|
68
|
+
|
|
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
|
+
})
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// start:
|
|
84
|
+
install().catch((e) => {
|
|
85
|
+
throw e
|
|
86
|
+
})
|