1- var Promise = require ( 'bluebird' ) ;
21var request = require ( 'request' ) ;
32var progress = require ( 'progress' ) ;
43var fs = require ( 'fs' ) ;
@@ -21,7 +20,7 @@ module.exports = {
2120 checkCache : function ( cachepath , files ) {
2221 var missing ;
2322
24- // if the version is >=0.12.3, then we don't know which files we want from the archives, so just check that the
23+ // if the version is >=0.12.3, then we don't know which files we want from the archives, so just check that the
2524 // folder exists and has at least 3 files in it.
2625 if ( files . length === 1 && files [ 0 ] === '*' ) {
2726 return fs . existsSync ( cachepath ) && fs . readdirSync ( cachepath ) . length >= 2 ;
@@ -44,7 +43,6 @@ module.exports = {
4443 } ,
4544 downloadAndUnpack : function ( cachepath , url ) {
4645 var extention = path . extname ( url ) ,
47- done = Promise . defer ( ) ,
4846 self = this ,
4947 rq = request ( url ) ,
5048 len ,
@@ -54,85 +52,83 @@ module.exports = {
5452 return statusCode + ': ' + require ( 'http' ) . STATUS_CODES [ statusCode ] ;
5553 }
5654
57- rq . proxy = true ;
58- rq . on ( 'error' , function ( err ) {
59- bar && bar . terminate ( ) ;
60- done . reject ( err ) ;
61- } ) ;
62- rq . on ( 'response' , function ( res ) {
63- len = parseInt ( res . headers [ 'content-length' ] , 10 ) ;
64- if ( res . statusCode !== 200 ) {
65- done . reject ( {
66- statusCode : res . statusCode ,
67- msg : 'Recieved status code ' + format ( res . statusCode )
68- } ) ;
69- } else if ( len ) {
70- if ( ! bar ) {
71- bar = new progress ( ' downloading [:bar] :percent :etas' , {
72- complete : '=' ,
73- incomplete : '-' ,
74- width : 20 ,
75- total : len
55+ return new Promise ( function ( resolve , reject ) {
56+ rq . proxy = true ;
57+ rq . on ( 'error' , function ( err ) {
58+ bar && bar . terminate ( ) ;
59+ reject ( err ) ;
60+ } ) ;
61+ rq . on ( 'response' , function ( res ) {
62+ len = parseInt ( res . headers [ 'content-length' ] , 10 ) ;
63+ if ( res . statusCode !== 200 ) {
64+ reject ( {
65+ statusCode : res . statusCode ,
66+ msg : 'Recieved status code ' + format ( res . statusCode )
7667 } ) ;
77- } else {
78- bar . total += len ;
68+ } else if ( len ) {
69+ if ( ! bar ) {
70+ bar = new progress ( ' downloading [:bar] :percent :etas' , {
71+ complete : '=' ,
72+ incomplete : '-' ,
73+ width : 20 ,
74+ total : len
75+ } ) ;
76+ } else {
77+ bar . total += len ;
78+ }
7979 }
80- }
81- } ) ;
82- rq . on ( 'data' , function ( chunk ) {
83- len && bar && bar . tick ( chunk . length ) ;
84- } ) ;
80+ } ) ;
81+ rq . on ( 'data' , function ( chunk ) {
82+ len && bar && bar . tick ( chunk . length ) ;
83+ } ) ;
8584
86- if ( extention === '.zip' ) {
87- stream = temp . createWriteStream ( ) ;
85+ if ( extention === '.zip' ) {
86+ stream = temp . createWriteStream ( ) ;
8887
89- stream . on ( 'close ' , function ( ) {
90- if ( done . promise . isRejected ( ) ) return ;
91- self . extractZip ( stream . path , cachepath ) . then ( self . stripRootFolder ) . then ( function ( files ) {
92- done . resolve ( files ) ;
88+ stream . on ( 'finish ' , function ( ) {
89+ self . extractZip ( stream . path , cachepath ) . then ( self . stripRootFolder ) . then ( function ( files ) {
90+ resolve ( files ) ;
91+ } ) ;
9392 } ) ;
94- } ) ;
9593
96- rq . pipe ( stream ) ;
97- }
94+ rq . pipe ( stream ) ;
95+ }
9896
99- if ( extention === '.gz' ) {
100- rq . on ( 'response' , function ( res ) {
101- if ( res . statusCode !== 200 ) return ;
102- self . extractTar ( res , cachepath ) . then ( self . stripRootFolder ) . then ( function ( files ) {
103- done . resolve ( files ) ;
97+ else if ( extention === '.gz' ) {
98+ rq . on ( 'response' , function ( res ) {
99+ if ( res . statusCode !== 200 ) return ;
100+ self . extractTar ( res , cachepath ) . then ( self . stripRootFolder ) . then ( function ( files ) {
101+ resolve ( files ) ;
102+ } ) ;
104103 } ) ;
105- } ) ;
106- }
107-
108- return done . promise ;
104+ }
105+ } ) ;
109106 } ,
110107 extractTar : function ( tarstream , destination ) {
111- var done = Promise . defer ( ) ,
112- gunzip = zlib . createGunzip ( ) ,
108+ var gunzip = zlib . createGunzip ( ) ,
113109 files = [ ] ;
114110
115- tarstream
116- . pipe ( gunzip )
117- . on ( 'error' , function ( err ) {
118- done . reject ( err ) ;
119- } )
120- . pipe ( tar . extract ( destination , {
121- umask : ( isWin ? false : 0 ) ,
122- map : function ( header ) {
123- files . push ( { path : path . basename ( header . name ) } ) ;
124- return header ;
125- }
126- } ) )
127- . on ( 'finish' , function ( ) {
128- done . resolve ( { files :files , destination :destination } ) ;
129- } ) ;
111+ return new Promise ( function ( resolve , reject ) {
112+ tarstream
113+ . pipe ( gunzip )
114+ . on ( 'error' , function ( err ) {
115+ reject ( err ) ;
116+ } )
117+ . pipe ( tar . extract ( destination , {
118+ umask : ( isWin ? false : 0 ) ,
119+ map : function ( header ) {
120+ files . push ( { path : path . basename ( header . name ) } ) ;
121+ return header ;
122+ }
123+ } ) )
124+ . on ( 'finish' , function ( ) {
125+ resolve ( { files :files , destination :destination } ) ;
126+ } ) ;
130127
131- return done . promise ;
128+ } ) ;
132129 } ,
133130 extractZip : function ( zipfile , destination ) {
134- var files = [ ] ,
135- done = Promise . defer ( ) ;
131+ var files = [ ] ;
136132
137133 var onEntry = function ( entry ) {
138134 files . push ( {
@@ -141,24 +137,24 @@ module.exports = {
141137 } ) ;
142138 } ;
143139
144- extract ( zipfile , { dir : destination , onEntry : onEntry } , function ( err ) {
145- if ( err ) {
146- return done . reject ( err ) ;
147- }
140+ return new Promise ( function ( resolve , reject ) {
141+ extract ( zipfile , { dir : destination , onEntry : onEntry } , function ( err ) {
142+ if ( err ) {
143+ return reject ( err ) ;
144+ }
145+
146+ // Setup chmodSync to fix permissions
147+ files . forEach ( function ( file ) {
148+ fs . chmodSync ( path . join ( destination , file . path ) , file . mode ) ;
149+ } ) ;
148150
149- // Setup chmodSync to fix permissions
150- files . forEach ( function ( file ) {
151- fs . chmodSync ( path . join ( destination , file . path ) , file . mode ) ;
151+ resolve ( { files : files , destination : destination } ) ;
152152 } ) ;
153153
154- done . resolve ( { files : files , destination : destination } ) ;
155154 } ) ;
156-
157- return done . promise ;
158155 } ,
159156 stripRootFolder : function ( extracted ) {
160- var done = Promise . defer ( ) ,
161- files = extracted . files ,
157+ var files = extracted . files ,
162158 destination = extracted . destination ,
163159 rootFiles = fs . readdirSync ( destination ) ,
164160 fromDir = path . join ( destination , rootFiles . length === 1 ? rootFiles [ 0 ] : '' ) ;
@@ -174,17 +170,20 @@ module.exports = {
174170 i -- ;
175171 }
176172 }
177- // move stripped folder to destination
178- ncp ( fromDir , destination , function ( err ) {
179- if ( err ) done . reject ( ) ;
180- else rimraf ( fromDir , function ( ) {
181- done . resolve ( files ) ;
173+
174+ return new Promise ( function ( resolve , reject ) {
175+ // move stripped folder to destination
176+ ncp ( fromDir , destination , function ( err ) {
177+ if ( err ) {
178+ return reject ( ) ;
179+ }
180+ else rimraf ( fromDir , function ( ) {
181+ resolve ( files ) ;
182+ } ) ;
182183 } ) ;
183184 } ) ;
184185 } else {
185- done . resolve ( files ) ;
186+ return Promise . resolve ( files ) ;
186187 }
187-
188- return done . promise ;
189188 }
190189} ;
0 commit comments