@@ -25,19 +25,28 @@ function getOptions(urlString) {
2525 }
2626}
2727
28- function download ( url ) {
29- return new Promise ( ( c , e ) => {
30- var content = '' ;
31- var request = https . get ( getOptions ( url ) , function ( response ) {
28+ function download ( url , redirectCount ) {
29+ return new Promise ( ( c , e ) => {
30+ var content = '' ;
31+ https . get ( getOptions ( url ) , function ( response ) {
3232 response . on ( 'data' , function ( data ) {
3333 content += data . toString ( ) ;
3434 } ) . on ( 'end' , function ( ) {
35+ let count = redirectCount || 0 ;
36+ if ( count < 5 && response . statusCode >= 300 && response . statusCode <= 303 || response . statusCode === 307 ) {
37+ let location = response . headers [ 'location' ] ;
38+ if ( location ) {
39+ console . log ( "Redirected " + url + " to " + location ) ;
40+ download ( location , count + 1 ) . then ( c , e ) ;
41+ return ;
42+ }
43+ }
3544 c ( content ) ;
3645 } ) ;
3746 } ) . on ( 'error' , function ( err ) {
3847 e ( err . message ) ;
3948 } ) ;
40- } ) ;
49+ } ) ;
4150}
4251
4352function getCommitSha ( repoId , repoPath ) {
@@ -46,14 +55,15 @@ function getCommitSha(repoId, repoPath) {
4655 try {
4756 let lastCommit = JSON . parse ( content ) [ 0 ] ;
4857 return Promise . resolve ( {
49- commitSha : lastCommit . sha ,
50- commitDate : lastCommit . commit . author . date
58+ commitSha : lastCommit . sha ,
59+ commitDate : lastCommit . commit . author . date
5160 } ) ;
5261 } catch ( e ) {
62+ console . error ( "Failed extracting the SHA: " + content ) ;
5363 return Promise . resolve ( null ) ;
5464 }
5565 } , function ( ) {
56- console . err ( 'Failed loading ' + commitInfo ) ;
66+ console . error ( 'Failed loading ' + commitInfo ) ;
5767 return Promise . resolve ( null ) ;
5868 } ) ;
5969}
@@ -97,7 +107,7 @@ exports.update = function (repoId, repoPath, dest, modifyGrammar) {
97107}
98108
99109if ( path . basename ( process . argv [ 1 ] ) === 'update-grammar.js' ) {
100- for ( var i = 3 ; i < process . argv . length ; i += 2 ) {
101- exports . update ( process . argv [ 2 ] , process . argv [ i ] , process . argv [ i + 1 ] ) ;
110+ for ( var i = 3 ; i < process . argv . length ; i += 2 ) {
111+ exports . update ( process . argv [ 2 ] , process . argv [ i ] , process . argv [ i + 1 ] ) ;
102112 }
103113}
0 commit comments