@@ -11,212 +11,198 @@ const osFilterObj = importLazy('os-filter-obj');
1111/**
1212 * Initialize a new `BinWrapper`
1313 *
14- * @param {Object } opts
14+ * @param {Object } options
1515 * @api public
1616 */
17-
18- function BinWrapper ( opts ) {
19- if ( ! ( this instanceof BinWrapper ) ) {
20- return new BinWrapper ( opts ) ;
21- }
22-
23- this . opts = opts || { } ;
24-
25- if ( this . opts . strip <= 0 ) {
26- this . opts . strip = 0 ;
27- } else if ( ! this . opts . strip ) {
28- this . opts . strip = 1 ;
17+ module . exports = class BinWrapper {
18+ constructor ( options = { } ) {
19+ this . options = options ;
20+
21+ if ( this . options . strip <= 0 ) {
22+ this . options . strip = 0 ;
23+ } else if ( ! this . options . strip ) {
24+ this . options . strip = 1 ;
25+ }
2926 }
30- }
3127
32- module . exports = BinWrapper ;
28+ /**
29+ * Get or set files to download
30+ *
31+ * @param {String } src
32+ * @param {String } os
33+ * @param {String } arch
34+ * @api public
35+ */
36+ src ( src , os , arch ) {
37+ if ( arguments . length === 0 ) {
38+ return this . _src ;
39+ }
3340
34- /**
35- * Get or set files to download
36- *
37- * @param {String } src
38- * @param {String } os
39- * @param {String } arch
40- * @api public
41- */
41+ this . _src = this . _src || [ ] ;
42+ this . _src . push ( {
43+ url : src ,
44+ os,
45+ arch
46+ } ) ;
4247
43- BinWrapper . prototype . src = function ( src , os , arch ) {
44- if ( arguments . length === 0 ) {
45- return this . _src ;
48+ return this ;
4649 }
4750
48- this . _src = this . _src || [ ] ;
49- this . _src . push ( {
50- url : src ,
51- os,
52- arch
53- } ) ;
54-
55- return this ;
56- } ;
57-
58- /**
59- * Get or set the destination
60- *
61- * @param {String } dest
62- * @api public
63- */
51+ /**
52+ * Get or set the destination
53+ *
54+ * @param {String } dest
55+ * @api public
56+ */
57+ dest ( dest ) {
58+ if ( arguments . length === 0 ) {
59+ return this . _dest ;
60+ }
6461
65- BinWrapper . prototype . dest = function ( dest ) {
66- if ( arguments . length === 0 ) {
67- return this . _dest ;
62+ this . _dest = dest ;
63+ return this ;
6864 }
6965
70- this . _dest = dest ;
71- return this ;
72- } ;
73-
74- /**
75- * Get or set the binary
76- *
77- * @param { String } bin
78- * @api public
79- */
66+ /**
67+ * Get or set the binary
68+ *
69+ * @param { String } bin
70+ * @api public
71+ */
72+ use ( bin ) {
73+ if ( arguments . length === 0 ) {
74+ return this . _use ;
75+ }
8076
81- BinWrapper . prototype . use = function ( bin ) {
82- if ( arguments . length === 0 ) {
83- return this . _use ;
77+ this . _use = bin ;
78+ return this ;
8479 }
8580
86- this . _use = bin ;
87- return this ;
88- } ;
89-
90- /**
91- * Get or set a semver range to test the binary against
92- *
93- * @param { String } range
94- * @api public
95- */
81+ /**
82+ * Get or set a semver range to test the binary against
83+ *
84+ * @param { String } range
85+ * @api public
86+ */
87+ version ( range ) {
88+ if ( arguments . length === 0 ) {
89+ return this . _version ;
90+ }
9691
97- BinWrapper . prototype . version = function ( range ) {
98- if ( arguments . length === 0 ) {
99- return this . _version ;
92+ this . _version = range ;
93+ return this ;
10094 }
10195
102- this . _version = range ;
103- return this ;
104- } ;
105-
106- /**
107- * Get path to the binary
108- *
109- * @api public
110- */
111-
112- BinWrapper . prototype . path = function ( ) {
113- return path . join ( this . dest ( ) , this . use ( ) ) ;
114- } ;
115-
116- /**
117- * Run
118- *
119- * @param {Array } cmd
120- * @param {Function } cb
121- * @api public
122- */
123-
124- BinWrapper . prototype . run = function ( cmd , cb ) {
125- if ( typeof cmd === 'function' && ! cb ) {
126- cb = cmd ;
127- cmd = [ '--version' ] ;
96+ /**
97+ * Get path to the binary
98+ *
99+ * @api public
100+ */
101+ path ( ) {
102+ return path . join ( this . dest ( ) , this . use ( ) ) ;
128103 }
129104
130- this . findExisting ( err => {
131- if ( err ) {
132- cb ( err ) ;
133- return ;
134- }
135-
136- if ( this . opts . skipCheck ) {
137- cb ( ) ;
138- return ;
105+ /**
106+ * Run
107+ *
108+ * @param {Array } cmd
109+ * @param {Function } cb
110+ * @api public
111+ */
112+ run ( cmd , cb ) {
113+ if ( typeof cmd === 'function' && ! cb ) {
114+ cb = cmd ;
115+ cmd = [ '--version' ] ;
139116 }
140117
141- this . runCheck ( cmd , cb ) ;
142- } ) ;
143- } ;
144-
145- /**
146- * Run binary check
147- *
148- * @param {Array } cmd
149- * @param {Function } cb
150- * @api private
151- */
118+ this . findExisting ( err => {
119+ if ( err ) {
120+ cb ( err ) ;
121+ return ;
122+ }
152123
153- BinWrapper . prototype . runCheck = function ( cmd , cb ) {
154- binCheck ( ) ( this . path ( ) , cmd , ( err , works ) => {
155- if ( err ) {
156- cb ( err ) ;
157- return ;
158- }
124+ if ( this . options . skipCheck ) {
125+ cb ( ) ;
126+ return ;
127+ }
159128
160- if ( ! works ) {
161- cb ( new Error ( 'The `' + this . path ( ) + '` binary doesn\'t seem to work correctly' ) ) ;
162- return ;
163- }
129+ this . runCheck ( cmd , cb ) ;
130+ } ) ;
131+ }
164132
165- if ( this . version ( ) ) {
166- binVersionCheck ( ) ( this . path ( ) , this . version ( ) , cb ) ;
167- return ;
168- }
133+ /**
134+ * Run binary check
135+ *
136+ * @param {Array } cmd
137+ * @param {Function } cb
138+ * @api private
139+ */
140+ runCheck ( cmd , cb ) {
141+ binCheck ( ) ( this . path ( ) , cmd , ( err , works ) => {
142+ if ( err ) {
143+ cb ( err ) ;
144+ return ;
145+ }
146+
147+ if ( ! works ) {
148+ cb ( new Error ( `The \`${ this . path ( ) } \` binary doesn't seem to work correctly` ) ) ;
149+ return ;
150+ }
151+
152+ if ( this . version ( ) ) {
153+ binVersionCheck ( ) ( this . path ( ) , this . version ( ) , cb ) ;
154+ return ;
155+ }
169156
170- cb ( ) ;
171- } ) ;
172- } ;
157+ cb ( ) ;
158+ } ) ;
159+ }
173160
174- /**
175- * Find existing files
176- *
177- * @param {Function } cb
178- * @api private
179- */
161+ /**
162+ * Find existing files
163+ *
164+ * @param {Function } cb
165+ * @api private
166+ */
167+ findExisting ( cb ) {
168+ fs . stat ( this . path ( ) , err => {
169+ if ( err && err . code === 'ENOENT' ) {
170+ this . download ( cb ) ;
171+ return ;
172+ }
173+
174+ if ( err ) {
175+ cb ( err ) ;
176+ return ;
177+ }
180178
181- BinWrapper . prototype . findExisting = function ( cb ) {
182- fs . stat ( this . path ( ) , err => {
183- if ( err && err . code === 'ENOENT' ) {
184- this . download ( cb ) ;
185- return ;
186- }
179+ cb ( ) ;
180+ } ) ;
181+ }
187182
188- if ( err ) {
189- cb ( err ) ;
183+ /**
184+ * Download files
185+ *
186+ * @param {Function } cb
187+ * @api private
188+ */
189+ download ( cb ) {
190+ const files = osFilterObj ( ) ( this . src ( ) ) ;
191+ const download = new Download ( ) ( {
192+ extract : true ,
193+ mode : '755' ,
194+ strip : this . options . strip
195+ } ) ;
196+
197+ if ( files . length === 0 ) {
198+ cb ( new Error ( 'No binary found matching your system. It\'s probably not supported.' ) ) ;
190199 return ;
191200 }
192201
193- cb ( ) ;
194- } ) ;
195- } ;
196-
197- /**
198- * Download files
199- *
200- * @param {Function } cb
201- * @api private
202- */
202+ files . forEach ( file => download . get ( file . url ) ) ;
203203
204- BinWrapper . prototype . download = function ( cb ) {
205- const files = osFilterObj ( ) ( this . src ( ) ) ;
206- const download = new Download ( ) ( {
207- extract : true ,
208- mode : '755' ,
209- strip : this . opts . strip
210- } ) ;
211-
212- if ( files . length === 0 ) {
213- cb ( new Error ( 'No binary found matching your system. It\'s probably not supported.' ) ) ;
214- return ;
204+ download
205+ . dest ( this . dest ( ) )
206+ . run ( cb ) ;
215207 }
216-
217- files . forEach ( file => download . get ( file . url ) ) ;
218-
219- download
220- . dest ( this . dest ( ) )
221- . run ( cb ) ;
222208} ;
0 commit comments