|
1 | | -# require npm modules |
2 | | -request = require 'request' |
3 | | -semcmp = require 'semver-compare' |
4 | | -chalk = require 'chalk' |
5 | | -sprintf = require('sprintf-js').sprintf |
6 | | -Promise = require 'promise' |
7 | | - |
8 | | -baseApiUrl = 'https://api.github.com/repos/%s/releases' |
9 | | - |
10 | | -module.exports = (options, callback) -> |
11 | | - if callback |
12 | | - check options, callback |
13 | | - else |
14 | | - return new Promise (resolve, reject) -> |
15 | | - check options, (release, error) -> |
16 | | - reject error if error |
17 | | - if release then resolve release |
18 | | - else resolve null |
19 | | - |
20 | | -check = (options, callback) -> |
21 | | - |
22 | | - # get options |
23 | | - repo = options.repo |
24 | | - currentVersion = options.currentVersion |
25 | | - |
26 | | - # check if required options are defined |
27 | | - callback(null, 'no repository specified') if repo is undefined |
28 | | - callback(null, 'no current version given') if currentVersion is undefined |
29 | | - |
30 | | - apiUrl = sprintf baseApiUrl, repo |
31 | | - |
32 | | - # request options |
33 | | - reqOpts = |
34 | | - url: apiUrl |
35 | | - headers: |
36 | | - 'User-Agent': 'github-version-checker' |
37 | | - |
38 | | - request reqOpts, (error, response, body) -> |
39 | | - callback(null, error) if error |
40 | | - |
41 | | - # parse the response body into an object |
42 | | - releases = JSON.parse body |
43 | | - |
44 | | - found = false |
45 | | - |
46 | | - # loop through releases |
47 | | - # if a remote version is higher than the installed one, the callback |
48 | | - # will be called with the release object |
49 | | - for release in releases |
50 | | - tag = release.tag_name.replace(/[^0-9$.,]/g, '') |
51 | | - if semcmp(currentVersion, tag) is -1 |
52 | | - found = true |
53 | | - callback release, null |
54 | | - break |
55 | | - |
56 | | - callback(null, null) if not found |
57 | | - |
58 | | - return null |
| 1 | +# require npm modules |
| 2 | +got = require 'got' |
| 3 | +semcmp = require 'semver-compare' |
| 4 | +chalk = require 'chalk' |
| 5 | +sprintf = require('sprintf-js').sprintf |
| 6 | + |
| 7 | +baseApiUrl = 'https://api.github.com/repos/%s/releases' |
| 8 | + |
| 9 | +module.exports = (options, callback) -> |
| 10 | + if callback |
| 11 | + check options, callback |
| 12 | + else |
| 13 | + return new Promise (resolve, reject) -> |
| 14 | + check options, (update, error) -> |
| 15 | + reject error if error |
| 16 | + if update then resolve update else resolve null |
| 17 | + |
| 18 | +check = (options, callback) -> |
| 19 | + # get options |
| 20 | + repo = options.repo |
| 21 | + currentVersion = options.currentVersion |
| 22 | + includePreReleases = options.includePreReleases || false |
| 23 | + |
| 24 | + # check if required options are defined |
| 25 | + callback(null, 'no repository specified') if repo is undefined |
| 26 | + callback(null, 'no current version given') if currentVersion is undefined |
| 27 | + |
| 28 | + # do the api call |
| 29 | + apiUrl = sprintf baseApiUrl, repo |
| 30 | + if includePreReleases then apiUrl += '/latest' |
| 31 | + got(apiUrl).then((response) -> |
| 32 | + # parse the response body into an object |
| 33 | + releases = JSON.parse response.body |
| 34 | + |
| 35 | + found = false |
| 36 | + |
| 37 | + if includePreReleases |
| 38 | + # loop through releases |
| 39 | + # if a remote version is higher than the installed one, the callback |
| 40 | + # will be called with the release object |
| 41 | + for release in releases |
| 42 | + tag = release.tag_name.replace(/[^0-9$.,]/g, '') |
| 43 | + if semcmp(currentVersion, tag) is -1 |
| 44 | + found = true |
| 45 | + callback release, null |
| 46 | + break |
| 47 | + else |
| 48 | + latest = releases[0] |
| 49 | + tag = latest.tag_name.replace(/[^0-9$.,]/g, '') |
| 50 | + if semcmp(currentVersion, tag) is -1 |
| 51 | + found = true |
| 52 | + callback latest, null |
| 53 | + |
| 54 | + callback(null, null) if not found |
| 55 | + ).catch((error) -> |
| 56 | + callback null, error |
| 57 | + ) |
| 58 | + |
| 59 | + return null |
0 commit comments