Skip to content

Commit 7b721fa

Browse files
author
Thomas Sileghem
committed
feat: expose download instance
1 parent 14f8d16 commit 7b721fa

4 files changed

Lines changed: 56 additions & 3 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_js:
44
- 'iojs'
55
- '0.12'
66
- '0.10'
7+
before_script: npm -g install npm@latest

index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function BinWrapper(opts) {
1919
return new BinWrapper(opts);
2020
}
2121

22+
this.configureDownloadFn = [];
2223
this.opts = opts || {};
2324

2425
if (this.opts.strip <= 0) {
@@ -141,6 +142,18 @@ BinWrapper.prototype.run = function (cmd, cb) {
141142
}.bind(this));
142143
};
143144

145+
/**
146+
* configureDownload
147+
*
148+
* @param {Function} fn
149+
* @api public
150+
*/
151+
152+
BinWrapper.prototype.configureDownload = function (fn) {
153+
this.configureDownloadFn.push(fn);
154+
return this;
155+
};
156+
144157
/**
145158
* Run binary check
146159
*
@@ -208,6 +221,10 @@ BinWrapper.prototype.download = function (cb) {
208221
strip: this.opts.strip
209222
});
210223

224+
this.configureDownloadFn.forEach(function (fn) {
225+
fn(download);
226+
});
227+
211228
if (!files.length) {
212229
cb(new Error('No binary found matching your system. It\'s probably not supported.'));
213230
return;

readme.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,20 @@ Returns the full path to your binary.
104104

105105
Type: `string`
106106

107-
Define a [semver range](https://github.com/isaacs/node-semver#ranges) to check
107+
Define a [semver range](https://github.com/isaacs/node-semver#ranges) to check
108108
the binary against.
109109

110110
### .run([cmd], callback)
111111

112-
Runs the search for the binary. If no binary is found it will download the file
112+
Runs the search for the binary. If no binary is found it will download the file
113113
using the URL provided in `.src()`.
114114

115115
#### cmd
116116

117117
Type: `array`
118118
Default: `['--version']`
119119

120-
Command to run the binary with. If it exits with code `0` it means that the
120+
Command to run the binary with. If it exits with code `0` it means that the
121121
binary is working.
122122

123123
#### callback(err)
@@ -126,6 +126,14 @@ Type: `function`
126126

127127
Returns nothing but a possible error.
128128

129+
### .configureDownload(configFn)
130+
131+
#### configFn(download)
132+
133+
Type: `function`
134+
135+
The function will be called with the [download](https://github.com/kevva/download) instance used
136+
129137

130138
## License
131139

test/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var path = require('path');
44
var nock = require('nock');
55
var pathExists = require('path-exists');
66
var rimraf = require('rimraf');
7+
var download = require('download');
78
var test = require('ava');
89
var BinWrapper = require('../');
910
var fixture = path.join.bind(path, __dirname, 'fixtures');
@@ -184,3 +185,29 @@ test('error if no binary is found and no source is provided', function (t) {
184185
t.assert(err.message === 'No binary found matching your system. It\'s probably not supported.', err.message);
185186
});
186187
});
188+
189+
test('expose download', function (t) {
190+
t.plan(5);
191+
192+
var scope = nock('http://foo.com')
193+
.get('/gifsicle.tar.gz')
194+
.replyWithFile(200, fixture('gifsicle-' + process.platform + '.tar.gz'));
195+
196+
var bin = new BinWrapper()
197+
.src('http://foo.com/gifsicle.tar.gz')
198+
.dest(path.join(__dirname, 'expose'))
199+
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle')
200+
.configureDownload(function (d) {
201+
t.assert(d instanceof download);
202+
});
203+
204+
bin.run(function (err) {
205+
t.assert(!err, err);
206+
t.assert(pathExists.sync(bin.path()));
207+
t.assert(scope.isDone());
208+
209+
rimraf(bin.dest(), function (err) {
210+
t.assert(!err, err);
211+
});
212+
});
213+
});

0 commit comments

Comments
 (0)