-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhammercontent.js
64 lines (54 loc) · 1.75 KB
/
hammercontent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { dialog, app } = require('electron')
const http = require('follow-redirects').https
const extract = require('extract-zip')
const fs = require('fs')
const path = require('path')
const DOWNLOAD_URL = 'https://github.com/qurs/hammerpp-manager/releases/download/content/hammer++.zip'
const unzipContent = (zipDir) => {
return new Promise((resolve, reject) => {
extract(zipDir, { dir: path.join(process.cwd(), 'hammer++') }).then(() => {
fs.rm(zipDir, {force: true}, resolve)
}).catch(reject)
})
}
const downloadContent = () => {
return new Promise((resolve, reject) => {
const _path = path.join(process.cwd(), 'hammer++.zip')
const urlData = new URL(DOWNLOAD_URL)
if (!urlData) return reject()
if (fs.existsSync(_path)) return reject()
http.get({
hostname: urlData.hostname,
path: urlData.pathname,
headers: {'User-Agent': 'Mozilla/5.0 (Windows NT 10; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0'},
}, res => {
res.on('data', chunk => {
fs.appendFileSync(_path, chunk)
})
res.on('end', () => {
if (res.statusCode != 200) {
reject()
}
else {
resolve(_path)
}
})
})
})
}
exports.check = () => {
return new Promise((resolve, reject) => {
if (fs.existsSync( path.join(process.cwd(), 'hammer++') )) return resolve(false)
downloadContent().then(_path => {
unzipContent(_path).then(() => {
app.relaunch()
app.exit(0)
}).catch(err => {
dialog.showErrorBox('Ошибка', 'При попыке распаковать контент Hammer++ произошла ошибка! ' + err)
})
}).catch(err => {
dialog.showErrorBox('Ошибка', 'При попыке скачать контент Hammer++ произошла ошибка! ' + err)
})
resolve(true)
})
}