Skip to content
This repository was archived by the owner on Dec 19, 2025. It is now read-only.

Commit 68b47c2

Browse files
committed
Reduce complexity of check.js
1 parent a83cbfd commit 68b47c2

File tree

3 files changed

+156
-134
lines changed

3 files changed

+156
-134
lines changed

lib/check.js

Lines changed: 2 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,6 @@
11
// 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')
1364

1375
/**
1386
* Checks whether a new version is available. Depending on whether a token is given, the

lib/query/graphql.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Require modules
2+
const github_graphql = require('github-graphql-client')
3+
const semver = require('semver')
4+
const query = require('../util/query')
5+
6+
/**
7+
* Executes a GraphQL query on the API to fetch either the latest release or tag.
8+
* The returned version is compared to the given version.
9+
* When the fetched version is greater than the given one, the newer version
10+
* is returned.
11+
*
12+
* @param {CheckOptions} options The options for the version check.
13+
* @param {CallbackFunction} callback The callback function.
14+
*/
15+
module.exports = (options, callback) => {
16+
// build the query
17+
const theQuery = options.fetchTags ? query.tags(options.repo, options.owner) : query.releases(options.repo, options.owner)
18+
19+
// do the api call
20+
github_graphql({
21+
token: options.token,
22+
query: theQuery
23+
}, (err, res) => {
24+
if (err) {
25+
callback(err, null)
26+
} else {
27+
// Retrieve newer version name
28+
const newer = options.fetchTags ? res.data.repository.refs.nodes[0] : res.data.repository.releases.nodes[0]
29+
30+
// Compare versions
31+
if (semver.gt((options.fetchTags ? newer.name : newer.tag.name), options.currentVersion)) {
32+
callback(null, newer)
33+
} else {
34+
callback(null, null)
35+
}
36+
}
37+
})
38+
}

lib/query/rest.js

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

0 commit comments

Comments
 (0)