@@ -16,28 +16,28 @@ const pkg = require('../package.json');
1616 * @param callback {function|undefined} The callback function.
1717 */
1818const 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- } ) ;
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+ } ) ;
4141} ;
4242
4343/**
@@ -50,79 +50,79 @@ const graphql = (options, callback) => {
5050 * @param callback {function|undefined} The callback function.
5151*/
5252const rest = ( options , callback ) => {
53- const handler = res => {
54- const chunks = [ ] ;
55- res . on ( 'data' , chunk => {
56- return chunks . push ( chunk . toString ( ) ) ;
57- } ) ;
58- res . on ( 'end' , ( ) => {
59- // parse response string into an object
60- const response = chunks . join ( '' ) ;
61- let json = null ;
62- try {
63- json = JSON . parse ( response ) ;
64- } catch ( error ) {
65- return callback ( error , null ) ;
66- }
67-
68- // 404 error occurs, when no releases are found
69- if ( res . statusCode === 404 ) {
70- return callback ( null , null ) ;
71- }
72-
73- // status codes other than 200 are treated as an error
74- else if ( res . statusCode !== 200 ) {
75- return callback ( new Error ( json . message ) , null ) ;
76- }
77-
78- // Compare versions
79- let found = false ;
80- let version = null ;
81- for ( let i = 0 ; i < json . length ; i ++ ) {
82- version = json [ i ] ;
83- if ( semver . gt ( version [ options . fetchTags ? 'name' : 'tag_name' ] , options . currentVersion ) ) {
84- found = true ;
85- break ;
86- }
87- }
88-
89- if ( found ) {
90- const mapped = schemeMapper [ options . fetchTags ? 'tag' : 'release' ] ( version ) ;
91- callback ( null , mapped ) ;
92- } else {
93- callback ( null , null ) ;
94- }
95- } ) ;
96- } ;
97-
98- // build url
99- let apiUrl = `/repos/${ options . owner } /${ options . repo } ` ;
100- if ( options . fetchTags ) {
101- apiUrl += '/tags' ;
102- } else {
103- apiUrl += '/releases' ;
104-
105- if ( options . latestOnly ) {
106- apiUrl += '/latest' ;
107- }
108- }
109-
110- // define request options
111- const opts = {
112- hostname : 'api.github.com' ,
113- path : apiUrl ,
114- headers : {
115- 'Accept' : 'application/vnd.github.v3+json' ,
116- 'User-Agent' : `${ pkg . name } v${ pkg . version } `
117- }
118- } ;
119-
120- // execute request
121- let req = https . get ( opts , handler ) ;
122- req . on ( 'error' , err => {
123- callback ( err , null ) ;
124- } ) ;
125- req . end ( ) ;
53+ const handler = res => {
54+ const chunks = [ ] ;
55+ res . on ( 'data' , chunk => {
56+ return chunks . push ( chunk . toString ( ) ) ;
57+ } ) ;
58+ res . on ( 'end' , ( ) => {
59+ // parse response string into an object
60+ const response = chunks . join ( '' ) ;
61+ let json = null ;
62+ try {
63+ json = JSON . parse ( response ) ;
64+ } catch ( error ) {
65+ return callback ( error , null ) ;
66+ }
67+
68+ // 404 error occurs, when no releases are found
69+ if ( res . statusCode === 404 ) {
70+ return callback ( null , null ) ;
71+ }
72+
73+ // status codes other than 200 are treated as an error
74+ else if ( res . statusCode !== 200 ) {
75+ return callback ( new Error ( json . message ) , null ) ;
76+ }
77+
78+ // Compare versions
79+ let found = false ;
80+ let version = null ;
81+ for ( let i = 0 ; i < json . length ; i ++ ) {
82+ version = json [ i ] ;
83+ if ( semver . gt ( version [ options . fetchTags ? 'name' : 'tag_name' ] , options . currentVersion ) ) {
84+ found = true ;
85+ break ;
86+ }
87+ }
88+
89+ if ( found ) {
90+ const mapped = schemeMapper [ options . fetchTags ? 'tag' : 'release' ] ( version ) ;
91+ callback ( null , mapped ) ;
92+ } else {
93+ callback ( null , null ) ;
94+ }
95+ } ) ;
96+ } ;
97+
98+ // build url
99+ let apiUrl = `/repos/${ options . owner } /${ options . repo } ` ;
100+ if ( options . fetchTags ) {
101+ apiUrl += '/tags' ;
102+ } else {
103+ apiUrl += '/releases' ;
104+
105+ if ( options . latestOnly ) {
106+ apiUrl += '/latest' ;
107+ }
108+ }
109+
110+ // define request options
111+ const opts = {
112+ hostname : 'api.github.com' ,
113+ path : apiUrl ,
114+ headers : {
115+ 'Accept' : 'application/vnd.github.v3+json' ,
116+ 'User-Agent' : `${ pkg . name } v${ pkg . version } `
117+ }
118+ } ;
119+
120+ // execute request
121+ let req = https . get ( opts , handler ) ;
122+ req . on ( 'error' , err => {
123+ callback ( err , null ) ;
124+ } ) ;
125+ req . end ( ) ;
126126} ;
127127
128128/**
@@ -138,31 +138,31 @@ module.exports = (options, callback) => {
138138 throw new Error ( 'options object must not be null or undefined!' ) ;
139139 }
140140
141- // get options
142- options . token = options . token || process . env . GITHUB_API_TOKEN || undefined ;
143- if ( options . reduceTraffic ) {
144- console . warn ( 'The "reduceTraffic" option is deprecated! Consider updating "github-version-checker" to a newer version.' ) ;
145- }
146-
147- // check if required options are defined
148- if ( ! options . repo ) {
149- callback ( 'no repository specified' , null ) ;
150- return ;
151- }
152- if ( ! options . owner ) {
153- callback ( 'no owner specified' , null ) ;
154- return ;
155- }
156- if ( ! options . currentVersion ) {
157- callback ( 'no current version given' , null ) ;
158- return ;
159- }
160-
161- // decide what to do
162- // when we have a token supplied, we will call the GraphQL api
163- if ( options . token ) {
164- graphql ( options , callback ) ;
165- } else {
166- rest ( options , callback ) ;
167- }
141+ // get options
142+ options . token = options . token || process . env . GITHUB_API_TOKEN || undefined ;
143+ if ( options . reduceTraffic ) {
144+ console . warn ( 'The "reduceTraffic" option is deprecated! Consider updating "github-version-checker" to a newer version.' ) ;
145+ }
146+
147+ // check if required options are defined
148+ if ( ! options . repo ) {
149+ callback ( 'no repository specified' , null ) ;
150+ return ;
151+ }
152+ if ( ! options . owner ) {
153+ callback ( 'no owner specified' , null ) ;
154+ return ;
155+ }
156+ if ( ! options . currentVersion ) {
157+ callback ( 'no current version given' , null ) ;
158+ return ;
159+ }
160+
161+ // decide what to do
162+ // when we have a token supplied, we will call the GraphQL api
163+ if ( options . token ) {
164+ graphql ( options , callback ) ;
165+ } else {
166+ rest ( options , callback ) ;
167+ }
168168} ;
0 commit comments