11'use strict' ;
2+ const fetch = require ( 'cross-fetch' ) ;
23const Generator = require ( 'yeoman-generator' ) ;
4+ const queryString = require ( 'query-string' ) ;
35const yosay = require ( 'yosay' ) ;
46
57module . exports = class extends Generator {
@@ -49,6 +51,8 @@ module.exports = class extends Generator {
4951 this . templateData . tokenendpoint = answers . tokenendpoint ;
5052 this . templateData . userinformationendpoint = answers . userinformationendpoint ;
5153
54+ this . templateData . currentVersion = await this . getCurrentVersion ( ) ;
55+
5256 this . name = answers . name ;
5357 this . applicationName = 'AspNet.Security.OAuth.' + answers . name ;
5458 }
@@ -60,4 +64,55 @@ module.exports = class extends Generator {
6064 this . fs . copyTpl ( this . templatePath ( 'AuthenticationHandler.cs' ) , this . applicationName + '/' + this . name + 'AuthenticationHandler.cs' , this . templateData )
6165 this . fs . copyTpl ( this . templatePath ( 'AuthenticationOptions.cs' ) , this . applicationName + '/' + this . name + 'AuthenticationOptions.cs' , this . templateData )
6266 }
67+
68+ async getCurrentVersion ( ) {
69+
70+ let response = await fetch ( 'https://api.nuget.org/v3/index.json' ) ;
71+
72+ if ( ! response . ok ) {
73+ throw new Error ( `Failed to query NuGet service index. HTTP status code: ${ response . status } .` ) ;
74+ }
75+
76+ const serviceIndex = await response . json ( ) ;
77+ const baseAddress = serviceIndex . resources . find ( resource => resource [ '@type' ] === 'SearchQueryService/3.5.0' ) [ '@id' ] ;
78+
79+ if ( ! baseAddress ) {
80+ throw new Error ( 'Failed to determine the base address for the NuGet search query service.' ) ;
81+ }
82+
83+ const query = queryString . stringify ( {
84+ prerelease : false ,
85+ q : 'PackageId:AspNet.Security.OAuth.GitHub' ,
86+ semVerLevel : '2.0.0' ,
87+ take : 1
88+ } ) ;
89+
90+ const searchUrl = `${ baseAddress } ?${ query } ` ;
91+ response = await fetch ( searchUrl ) ;
92+
93+ if ( ! response . ok ) {
94+ throw new Error ( `Failed to search for NuGet package from '${ searchUrl } '. HTTP status code: ${ response . status } .` ) ;
95+ }
96+
97+ const searchResult = await response . json ( ) ;
98+
99+ let latestVersion = null ;
100+
101+ if ( searchResult . data && searchResult . data . length > 0 ) {
102+ latestVersion = searchResult . data [ 0 ] . version ;
103+ }
104+
105+ if ( ! latestVersion ) {
106+ throw new Error ( 'Failed to determine the latest version of the OAuth providers.' ) ;
107+ }
108+
109+ const dot = '.' ;
110+ const versionParts = latestVersion . split ( dot ) ;
111+
112+ // Increment the build number by one for the release that
113+ // would be the first one including this new provider.
114+ versionParts [ 2 ] = parseInt ( versionParts [ 2 ] , 10 ) + 1 ;
115+
116+ return versionParts . join ( dot ) ;
117+ }
63118} ;
0 commit comments