1
1
#! /usr/bin/env node
2
2
3
- const unzipper = require ( 'unzipper ' ) ;
3
+ const extract = require ( 'extract-zip ' ) ;
4
4
const url = require ( 'url' ) ;
5
5
const HttpsProxyAgent = require ( 'https-proxy-agent' ) ;
6
6
const https = require ( 'https' ) ;
@@ -40,7 +40,8 @@ if (os.platform() === 'win32') {
40
40
throw Error ( 'platform ' + os . platform ( ) + ' isn\'t supported' ) ;
41
41
}
42
42
43
- const endpoint = 'https://functionscdn.azureedge.net/public/' + version + '/Azure.Functions.Cli.' + platform + '.' + version + '.zip' ;
43
+ const fileName = 'Azure.Functions.Cli.' + platform + '.' + version + '.zip' ;
44
+ const endpoint = 'https://functionscdn.azureedge.net/public/' + version + '/' + fileName ;
44
45
console . log ( 'attempting to GET %j' , endpoint ) ;
45
46
const options = url . parse ( endpoint ) ;
46
47
// npm config preceed system environment
@@ -67,20 +68,29 @@ if (proxy) {
67
68
}
68
69
69
70
https . get ( options , response => {
71
+ const bar = new ProgressBar ( '[:bar] Downloading Azure Functions Core Tools' , {
72
+ total : Number ( response . headers [ 'content-length' ] ) ,
73
+ width : 18
74
+ } ) ;
70
75
71
- const bar = new ProgressBar ( '[:bar] Downloading Azure Functions Core Tools' , {
72
- total : Number ( response . headers [ 'content-length' ] ) ,
73
- width : 18
74
- } ) ;
75
-
76
- if ( response . statusCode === 200 ) {
77
- const installPath = getPath ( ) ;
78
- response . on ( 'data' , data => bar . tick ( data . length ) ) ;
79
- const unzipStream = unzipper . Extract ( { path : installPath } )
80
- . on ( 'close' , ( ) => {
76
+ if ( response . statusCode === 200 ) {
77
+ const installPath = getPath ( ) ;
78
+ const downloadPath = installPath + '/' + fileName ;
79
+ response . on ( 'data' , data => bar . tick ( data . length ) ) ;
80
+ if ( ! fs . existsSync ( installPath ) ) {
81
+ fs . mkdirSync ( installPath ) ;
82
+ }
83
+ const file = fs . createWriteStream ( downloadPath ) ;
84
+ response . pipe ( file ) ;
85
+ file . on ( 'finish' , function ( ) {
86
+ file . close ( ( ) => {
87
+ extract ( file . path , {
88
+ dir : installPath
89
+ } ) . then ( ( ) => {
81
90
try {
82
- fs . closeSync ( fs . openSync ( `${ installPath } /telemetryDefaultOn.sentinel` , 'w' ) )
83
- console . log ( telemetryInfo )
91
+ fs . closeSync ( fs . openSync ( `${ installPath } /telemetryDefaultOn.sentinel` , 'w' ) ) ;
92
+ console . log ( telemetryInfo ) ;
93
+ fs . unlinkSync ( downloadPath ) ;
84
94
}
85
95
catch ( err ) {
86
96
// That's alright.
@@ -90,14 +100,15 @@ https.get(options, response => {
90
100
fs . chmodSync ( `${ installPath } /gozip` , 0o755 ) ;
91
101
}
92
102
} ) ;
93
- response . pipe ( unzipStream ) ;
94
- } else {
95
- console . error ( chalk . red ( 'Error downloading zip file from ' + endpoint ) ) ;
96
- console . error ( chalk . red ( 'Expected: 200, Actual: ' + response . statusCode ) ) ;
97
- process . exit ( 1 ) ;
98
- }
99
- } )
100
- . on ( 'error' , err => {
101
- console . error ( chalk . red ( err ) ) ;
103
+ } ) ;
104
+ } ) ;
105
+ } else {
106
+ console . error ( chalk . red ( 'Error downloading zip file from ' + endpoint ) ) ;
107
+ console . error ( chalk . red ( 'Expected: 200, Actual: ' + response . statusCode ) ) ;
102
108
process . exit ( 1 ) ;
103
- } ) ;
109
+ }
110
+ } )
111
+ . on ( 'error' , err => {
112
+ console . error ( chalk . red ( err ) ) ;
113
+ process . exit ( 1 ) ;
114
+ } ) ;
0 commit comments