-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (126 loc) · 3.82 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const FileSystem = require('original-fs')
const _ = require('lodash')
const path = require('path')
const request = require('request')
class Updater {
constructor() {
this.setup = {
api: '',
appPath: '',
appPathFolder: '',
requestOption: {},
callback: false
}
}
static log(msg) {
console.log(msg)
}
init(setup) {
this.setup = _.extend(this.setup, setup)
this.setup.appPathFolder.slice(0, this.setup.appPath.indexOf("app.asar"))
}
end(error) {
if (typeof this.setup.callback != 'function') return false
this.setup.callback.call(this,
( error != 'undefined' ? errors[error] : false ),
this.update.last)
}
check(callback) {
if (callback) {
this.setup.callback
}
let packageInfo = require(this.setup.appPath + 'package.json')
let currentVersion = packageInfo.version
if (!currentVersion) {
this.log('The "version" property not specified inside the application package.json')
this.end(0)
return false
}
let that = this
request({
url: this.setup.api + '?version=' + version,
method: 'get',
timeout: 2 * 60
}, function (error, response, body) {
if (error) {
that.end(0)
return false
}
if (response.statusCode == 200) {
that.log(body)
let result = body
if (!result.last) {
throw false
}
// Update available
if (result.source) {
that.log('Update available: ' + result.last)
that.update = result
// Ask user for confirmation
that.end()
} else {
that.log('No updates available')
that.end(2)
return false
}
} else {
throw false
}
})
}
download(callback) {
if (callback) {
this.setup.callback = callback
}
let that = this
let url = this.update.source, fileName = 'update.asar'
let dist = path.join(this.setup.appPathFolder, fileName)
this.log('Downloading:' + url)
try {
request
.get(url)
.on('error', function (err) {
console.log(err)
})
.on('end', function () {
that.update.file = dist
that.log('Update downloaded: ' + dist)
that.apply()
})
.pipe(FileSystem.createWriteStream(dist))
} catch (err) {
this.log('Downloaded error.')
}
}
apply() {
let that = this
try {
FileSystem.unlink(this.setup.appPath.slice(0, -1), function (err) {
if (err) {
return console.error(err)
}
that.log("Asar deleted successfully.")
})
} catch (error) {
that.log('Delete error: ' + error)
// Failure
that.end(6)
}
try {
FileSystem.rename(this.update.file, this.setup.appPath.slice(0, -1), function (err) {
if (err) {
return console.error(err)
}
that.log("Update applied.")
})
that.log('End of update.')
// Success
that.end()
} catch (error) {
that.log('Rename error: ' + error)
// Failure
that.end(6)
}
}
}
module.exports = new Updater()