|
1 | 1 | // Require modules |
2 | | -const https = require('https') |
3 | | -const github_graphql = require('github-graphql-client') |
4 | | -const semver = require('semver') |
5 | | -const query = require('./query') |
6 | | -const schemeMapper = require('./scheme-mapper') |
7 | | -const pkg = require('../package.json') |
8 | | - |
9 | | -/** |
10 | | - * Executes a GraphQL query on the API to fetch either the latest release or tag. |
11 | | - * The returned version is compared to the given version. |
12 | | - * When the fetched version is greater than the given one, the newer version |
13 | | - * is returned. |
14 | | - * |
15 | | - * @param {CheckOptions} options The options for the version check. |
16 | | - * @param {CallbackFunction} callback The callback function. |
17 | | - */ |
18 | | -const graphql = (options, callback) => { |
19 | | - // build the query |
20 | | - const theQuery = options.fetchTags ? query.tags(options.repo, options.owner) : query.releases(options.repo, options.owner) |
21 | | - |
22 | | - // do the api call |
23 | | - github_graphql({ |
24 | | - token: options.token, |
25 | | - query: theQuery |
26 | | - }, (err, res) => { |
27 | | - if (err) { |
28 | | - callback(err, null) |
29 | | - } else { |
30 | | - // Retrieve newer version name |
31 | | - const newer = options.fetchTags ? res.data.repository.refs.nodes[0] : res.data.repository.releases.nodes[0] |
32 | | - |
33 | | - // Compare versions |
34 | | - if (semver.gt((options.fetchTags ? newer.name : newer.tag.name), options.currentVersion)) { |
35 | | - callback(null, newer) |
36 | | - } else { |
37 | | - callback(null, null) |
38 | | - } |
39 | | - } |
40 | | - }) |
41 | | -} |
42 | | - |
43 | | -/** |
44 | | - * Executes an API call on the Github Rest API (v3) that should return the latest version. |
45 | | - * The returned version is compared to the given version. |
46 | | - * When the fetched version is greater than the given one, the newer version |
47 | | - * is returned. |
48 | | - * |
49 | | - * @param {CheckOptions} options The options for the version check. |
50 | | - * @param {CallbackFunction} callback The callback function. |
51 | | -*/ |
52 | | -const rest = (options, callback) => { |
53 | | - /** @type {RestHandlerFunction} */ |
54 | | - const handler = res => { |
55 | | - const chunks = [] |
56 | | - res.on('data', chunk => { |
57 | | - return chunks.push(chunk.toString()) |
58 | | - }) |
59 | | - res.on('end', () => { |
60 | | - // parse response string into an object |
61 | | - const response = chunks.join('') |
62 | | - let json = null |
63 | | - try { |
64 | | - json = JSON.parse(response) |
65 | | - } catch (error) { |
66 | | - return callback(error, null) |
67 | | - } |
68 | | - |
69 | | - // 404 error occurs, when no releases are found |
70 | | - if (res.statusCode === 404) { |
71 | | - return callback(null, null) |
72 | | - } |
73 | | - |
74 | | - // status codes other than 200 are treated as an error |
75 | | - else if (res.statusCode !== 200) { |
76 | | - return callback(new Error(json.message), null) |
77 | | - } |
78 | | - |
79 | | - // Compare versions |
80 | | - let found = false |
81 | | - let version = null |
82 | | - for (let i = 0; i < json.length; i++) { |
83 | | - version = json[i] |
84 | | - |
85 | | - if (options.excludePrereleases) { |
86 | | - if (version['prerelease'] !== undefined && version['prerelease']) { |
87 | | - continue |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - if (semver.gt(version[options.fetchTags ? 'name' : 'tag_name'], options.currentVersion)) { |
92 | | - found = true |
93 | | - break |
94 | | - } |
95 | | - } |
96 | | - |
97 | | - if (found) { |
98 | | - const mapped = schemeMapper[options.fetchTags ? 'tag' : 'release'](version) |
99 | | - callback(null, mapped) |
100 | | - } else { |
101 | | - callback(null, null) |
102 | | - } |
103 | | - }) |
104 | | - } |
105 | | - |
106 | | - // build url |
107 | | - let apiUrl = `/repos/${options.owner}/${options.repo}` |
108 | | - if (options.fetchTags) { |
109 | | - apiUrl += '/tags' |
110 | | - } else { |
111 | | - apiUrl += '/releases' |
112 | | - |
113 | | - if (options.latestOnly) { |
114 | | - apiUrl += '/latest' |
115 | | - } |
116 | | - } |
117 | | - |
118 | | - // define request options |
119 | | - /** @type {https.RequestOptions} */ |
120 | | - const opts = { |
121 | | - hostname: 'api.github.com', |
122 | | - path: apiUrl, |
123 | | - headers: { |
124 | | - 'Accept': 'application/vnd.github.v3+json', |
125 | | - 'User-Agent': `${pkg.name} v${pkg.version}` |
126 | | - } |
127 | | - } |
128 | | - |
129 | | - // execute request |
130 | | - let req = https.get(opts, handler) |
131 | | - req.on('error', err => { |
132 | | - callback(err, null) |
133 | | - }) |
134 | | - req.end() |
135 | | -} |
| 2 | +const graphql = require('./query/graphql') |
| 3 | +const rest = require('./query/rest') |
136 | 4 |
|
137 | 5 | /** |
138 | 6 | * Checks whether a new version is available. Depending on whether a token is given, the |
|
0 commit comments