From 7b721fa675de0a9a0a9656a9ada62c53f61a805a Mon Sep 17 00:00:00 2001 From: Thomas Sileghem Date: Tue, 15 Dec 2015 19:32:06 +0000 Subject: [PATCH] feat: expose download instance --- .travis.yml | 1 + index.js | 17 +++++++++++++++++ readme.md | 14 +++++++++++--- test/test.js | 27 +++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index dedfc07..07fee39 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,3 +4,4 @@ node_js: - 'iojs' - '0.12' - '0.10' +before_script: npm -g install npm@latest diff --git a/index.js b/index.js index e2a01e2..630a481 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,7 @@ function BinWrapper(opts) { return new BinWrapper(opts); } + this.configureDownloadFn = []; this.opts = opts || {}; if (this.opts.strip <= 0) { @@ -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 * @@ -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; diff --git a/readme.md b/readme.md index d2c489c..421f01b 100644 --- a/readme.md +++ b/readme.md @@ -104,12 +104,12 @@ 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 @@ -117,7 +117,7 @@ using the URL provided in `.src()`. 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) @@ -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 diff --git a/test/test.js b/test/test.js index 1b9f4d7..55c851b 100644 --- a/test/test.js +++ b/test/test.js @@ -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'); @@ -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); + }); + }); +});