Skip to content

Commit

Permalink
feat: expose download instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Sileghem committed Dec 15, 2015
1 parent 14f8d16 commit 7b721fa
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_js:
- 'iojs'
- '0.12'
- '0.10'
before_script: npm -g install npm@latest
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function BinWrapper(opts) {
return new BinWrapper(opts);
}

this.configureDownloadFn = [];
this.opts = opts || {};

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

/**
* configureDownload
*
* @param {Function} fn
* @api public
*/

BinWrapper.prototype.configureDownload = function (fn) {
this.configureDownloadFn.push(fn);
return this;
};

/**
* Run binary check
*
Expand Down Expand Up @@ -208,6 +221,10 @@ BinWrapper.prototype.download = function (cb) {
strip: this.opts.strip
});

this.configureDownloadFn.forEach(function (fn) {
fn(download);
});

if (!files.length) {
cb(new Error('No binary found matching your system. It\'s probably not supported.'));
return;
Expand Down
14 changes: 11 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,20 @@ Returns the full path to your binary.

Type: `string`

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

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

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

#### cmd

Type: `array`
Default: `['--version']`

Command to run the binary with. If it exits with code `0` it means that the
Command to run the binary with. If it exits with code `0` it means that the
binary is working.

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

Returns nothing but a possible error.

### .configureDownload(configFn)

#### configFn(download)

Type: `function`

The function will be called with the [download](https://github.com/kevva/download) instance used


## License

Expand Down
27 changes: 27 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var path = require('path');
var nock = require('nock');
var pathExists = require('path-exists');
var rimraf = require('rimraf');
var download = require('download');
var test = require('ava');
var BinWrapper = require('../');
var fixture = path.join.bind(path, __dirname, 'fixtures');
Expand Down Expand Up @@ -184,3 +185,29 @@ test('error if no binary is found and no source is provided', function (t) {
t.assert(err.message === 'No binary found matching your system. It\'s probably not supported.', err.message);
});
});

test('expose download', function (t) {
t.plan(5);

var scope = nock('http://foo.com')
.get('/gifsicle.tar.gz')
.replyWithFile(200, fixture('gifsicle-' + process.platform + '.tar.gz'));

var bin = new BinWrapper()
.src('http://foo.com/gifsicle.tar.gz')
.dest(path.join(__dirname, 'expose'))
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle')
.configureDownload(function (d) {
t.assert(d instanceof download);
});

bin.run(function (err) {
t.assert(!err, err);
t.assert(pathExists.sync(bin.path()));
t.assert(scope.isDone());

rimraf(bin.dest(), function (err) {
t.assert(!err, err);
});
});
});

0 comments on commit 7b721fa

Please sign in to comment.