Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose download instance #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
29 changes: 28 additions & 1 deletion 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 @@ -65,7 +66,7 @@ test('get the binary path', function (t) {
.dest('tmp')
.use('foo');

t.assert(bin.path() === 'tmp/foo', bin.path());
t.assert(bin.path() === path.join('tmp', 'foo'), bin.path());
});

test('verify that a binary is working', function (t) {
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);
});
});
});