-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
90 lines (78 loc) · 2.78 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
const request = require('request');
module.exports = {
check (config) {
return new Promise((resolve, reject) => {
try {
this.validateConfigurations(config);
request(
this.getRequestOptions(config.repository),
(error, response, body) => {
if (error) {
throw error;
}
const latest = JSON.parse(body).tag_name.replace(/[^0-9.]/g, '');
resolve({
current: config.version,
latest: latest,
newest: latest == config.version,
download_link: `${config.repository.url}/releases/latest`
});
}
);
} catch (message) {
reject(message);
}
});
},
proceed (options) {
if (! options || ! options.config || ! options.proxy) {
return;
}
this.check(options.config)
.then(result => {
options.proxy.log({
name: options.name,
source: 'plugin',
type: result.latest ? 'success' : 'info',
message: result.latest
? `You have the latest version v${result.current}.`
: `Your current version is v${result.current} and the latest version is v${result.newest}. Click <a href="${result.download_link}">here</a> to download the latest version.`
});
})
.catch(error => {
options.proxy.log({
name: options.name,
source: 'plugin',
type: 'error',
message: error
});
});
},
getRequestOptions (repository) {
const endpoint = repository.url.replace(
/https?:\/\/github.com/,
'https://api.github.com/repos'
);
return {
method: 'GET',
uri: `${endpoint}/releases/latest`,
headers: {
'User-Agent': 'SW Exporter',
},
};
},
validateConfigurations (config) {
if (! config.version) {
throw 'Current version not found in the configuration file.';
}
if (! config.repository) {
throw 'Repository informations not found in the configuration file.';
}
if (config.repository.type != 'git') {
throw 'Repository type is not yet supported.';
}
if (! config.repository.url) {
throw 'Repository url not found in the configuration file.';
}
}
};