1- # require npm modules
2- got = require ' got '
1+ # Require modules
2+ graphql = require ' github-graphql-client '
33chalk = require ' chalk'
44semver = require ' semver'
5+ query = require ' ./query'
56
6- baseApiUrl = ' https://api.github.com/repos/REPO/releases'
7+ ###
8+ The exported checking function.
79
10+ @param options {object} The options for the version check.
11+ @param callback {function|undefined} An optional callback to pass the result to.
12+ Can be omitted to return a Promise.
13+ @return undefined or a Promise.
14+ ###
815module .exports = (options , callback ) ->
916 if callback
1017 check options, callback
@@ -14,42 +21,49 @@ module.exports = (options, callback) ->
1421 reject error if error
1522 if update then resolve update else resolve null
1623
24+ ###
25+ The actual checking function. Executes a GraphQL query on the API to fetch
26+ either the latest release or tag. The returned version is compared to the given
27+ version. When to fetched version is greated than the given one, the newer version
28+ is returned.
29+
30+ @param options {object} The options for the version check.
31+ @param callback {function|undefined} An optional callback to pass the result to.
32+ Can be omitted to return a Promise.
33+ ###
1734check = (options , callback ) ->
1835 # get options
36+ token = options .token
1937 repo = options .repo
38+ owner = options .owner
2039 currentVersion = options .currentVersion
21- includePreReleases = options .includePreReleases || false
40+ fetchTags = options .fetchTags || false
2241
2342 # check if required options are defined
43+ callback (null , ' no token specified' ) if token is undefined
2444 callback (null , ' no repository specified' ) if repo is undefined
45+ callback (null , ' no owner specified' ) if owner is undefined
2546 callback (null , ' no current version given' ) if currentVersion is undefined
2647
48+ # build the query
49+ query = if fetchTags then query .tags repo, owner else query .releases repo, owner
50+
2751 # do the api call
28- apiUrl = baseApiUrl .replace ' REPO' , repo
29- if not includePreReleases then apiUrl += ' /latest'
30- got (apiUrl).then ((response ) ->
31- # parse the response body into an object
32- releases = JSON .parse response .body
33-
34- found = false
35-
36- if includePreReleases
37- # loop through releases
38- # if a remote version is higher than the installed one, the callback
39- # will be called with the release object
40- for release in releases
41- tag = release .tag_name
42- if semver .compare (currentVersion, tag) is - 1
43- found = true
44- callback release, null
45- break
52+ graphql (token : token, query : query), (err , res ) ->
53+ if err
54+ callback null , err
4655 else
47- tag = releases .tag_name
48- if semver .compare (currentVersion, tag) is - 1
49- found = true
50- callback releases, null
56+ # Retrieve newer version name
57+ newer =
58+ if fetchTags
59+ res .data .repository .refs .nodes [0 ]
60+ else
61+ res .data .repository .releases .nodes [0 ]
5162
52- callback (null , null ) if not found
53- ).catch (error) -> callback null , error
63+ # Compare versions
64+ if semver .gt (if fetchTags then newer .name else newer .tag .name ), currentVersion
65+ callback newer, null
66+ else
67+ callback null , null
5468
55- return null
69+ return
0 commit comments