11'use strict' ;
22const fs = require ( 'fs' ) ;
33const path = require ( 'path' ) ;
4+ const pify = require ( 'pify' ) ;
45const importLazy = require ( 'import-lazy' ) ( require ) ;
56
67const binCheck = importLazy ( 'bin-check' ) ;
78const binVersionCheck = importLazy ( 'bin-version-check' ) ;
89const download = importLazy ( 'download' ) ;
910const osFilterObj = importLazy ( 'os-filter-obj' ) ;
1011
12+ const statAsync = pify ( fs . stat ) ;
13+
1114/**
1215 * Initialize a new `BinWrapper`
1316 *
@@ -106,39 +109,27 @@ module.exports = class BinWrapper {
106109 * Run
107110 *
108111 * @param {Array } cmd
109- * @param {Function } cb
110112 * @api public
111113 */
112- run ( cmd , cb ) {
113- if ( typeof cmd === 'function' && ! cb ) {
114- cb = cmd ;
115- cmd = [ '--version' ] ;
116- }
117-
118- this . findExisting ( err => {
119- if ( err ) {
120- cb ( err ) ;
121- return ;
122- }
123-
124- if ( this . options . skipCheck ) {
125- cb ( ) ;
126- return ;
127- }
114+ run ( cmd = [ '--version' ] ) {
115+ return this . findExisting ( )
116+ . then ( ( ) => {
117+ if ( this . options . skipCheck ) {
118+ return ;
119+ }
128120
129- this . runCheck ( cmd , cb ) ;
130- } ) ;
121+ return this . runCheck ( cmd ) ;
122+ } ) ;
131123 }
132124
133125 /**
134126 * Run binary check
135127 *
136128 * @param {Array } cmd
137- * @param {Function } cb
138129 * @api private
139130 */
140- runCheck ( cmd , cb ) {
141- binCheck ( this . path ( ) , cmd )
131+ runCheck ( cmd ) {
132+ return binCheck ( this . path ( ) , cmd )
142133 . then ( works => {
143134 if ( ! works ) {
144135 throw new Error ( `The \`${ this . path ( ) } \` binary doesn't seem to work correctly` ) ;
@@ -149,56 +140,44 @@ module.exports = class BinWrapper {
149140 }
150141
151142 return Promise . resolve ( ) ;
152- } )
153- . then ( ( ) => cb ( ) )
154- . catch ( err => cb ( err ) ) ;
143+ } ) ;
155144 }
156145
157146 /**
158147 * Find existing files
159148 *
160- * @param {Function } cb
161149 * @api private
162150 */
163- findExisting ( cb ) {
164- fs . stat ( this . path ( ) , err => {
165- if ( err && err . code === 'ENOENT' ) {
166- this . download ( cb ) ;
167- return ;
168- }
169-
170- if ( err ) {
171- cb ( err ) ;
172- return ;
173- }
174-
175- cb ( ) ;
176- } ) ;
151+ findExisting ( ) {
152+ return statAsync ( this . path ( ) )
153+ . catch ( err => {
154+ if ( err && err . code === 'ENOENT' ) {
155+ return this . download ( ) ;
156+ }
157+
158+ return Promise . reject ( err ) ;
159+ } ) ;
177160 }
178161
179162 /**
180163 * Download files
181164 *
182- * @param {Function } cb
183165 * @api private
184166 */
185- download ( cb ) {
167+ download ( ) {
186168 const files = osFilterObj ( this . src ( ) || [ ] ) ;
187169 const urls = [ ] ;
188170
189171 if ( files . length === 0 ) {
190- cb ( new Error ( 'No binary found matching your system. It\'s probably not supported.' ) ) ;
191- return ;
172+ return Promise . reject ( new Error ( 'No binary found matching your system. It\'s probably not supported.' ) ) ;
192173 }
193174
194175 files . forEach ( file => urls . push ( file . url ) ) ;
195176
196- Promise . all ( urls . map ( url => download ( url , this . dest ( ) , {
177+ return Promise . all ( urls . map ( url => download ( url , this . dest ( ) , {
197178 extract : true ,
198179 mode : '755' ,
199180 strip : this . options . strip
200- } ) ) ) . then ( ( ) => {
201- cb ( ) ;
202- } ) ;
181+ } ) ) ) ;
203182 }
204183} ;
0 commit comments