bin-wrapper
Advanced tools
Comparing version 0.3.4 to 0.4.0
126
index.js
'use strict'; | ||
var binCheck = require('bin-check'); | ||
var binVersionCheck = require('bin-version-check'); | ||
var find = require('find-file'); | ||
@@ -8,16 +9,8 @@ var path = require('path'); | ||
/** | ||
* Initialize a new `BinWrapper` with opts | ||
* Initialize a new `BinWrapper` | ||
* | ||
* @param {Object} opts | ||
* @api public | ||
*/ | ||
function BinWrapper(opts) { | ||
if (!(this instanceof BinWrapper)) { | ||
return new BinWrapper(); | ||
} | ||
opts = opts || {}; | ||
this.opts = opts; | ||
this.global = opts.global !== false; | ||
function BinWrapper() { | ||
this._src = []; | ||
@@ -27,3 +20,3 @@ } | ||
/** | ||
* Define a binary to download | ||
* Add a file to download | ||
* | ||
@@ -56,9 +49,9 @@ * @param {String} src | ||
/** | ||
* Define where to download the binary to | ||
* Define where to download the file to | ||
* | ||
* @param {String} str | ||
* @param {String} dest | ||
* @api public | ||
*/ | ||
BinWrapper.prototype.dest = function (str) { | ||
BinWrapper.prototype.dest = function (dest) { | ||
if (!arguments.length) { | ||
@@ -68,3 +61,3 @@ return this._dest; | ||
this._dest = str; | ||
this._dest = dest; | ||
return this; | ||
@@ -76,7 +69,7 @@ }; | ||
* | ||
* @param {String} str | ||
* @param {String} bin | ||
* @api public | ||
*/ | ||
BinWrapper.prototype.use = function (str) { | ||
BinWrapper.prototype.use = function (bin) { | ||
if (!arguments.length) { | ||
@@ -86,11 +79,39 @@ return this._use; | ||
this._use = bin; | ||
return this; | ||
}; | ||
/** | ||
* Get the bin path | ||
* | ||
* @api public | ||
*/ | ||
BinWrapper.prototype.path = function () { | ||
var opts = { path: this.dest(), global: this.global, exclude: 'node_modules/.bin' }; | ||
var bin = find(str, opts); | ||
var bin = find(this.use(), opts); | ||
var dir; | ||
if (bin && bin.length > 0) { | ||
this._use = bin[0]; | ||
dir = bin[0]; | ||
} else { | ||
this._use = path.join(this.dest(), str); | ||
dir = path.join(this.dest(), this.use()); | ||
} | ||
return dir; | ||
}; | ||
/** | ||
* Define a semver range to test the binary against | ||
* | ||
* @param {String} range | ||
* @api public | ||
*/ | ||
BinWrapper.prototype.version = function (range) { | ||
if (!arguments.length) { | ||
return this._version; | ||
} | ||
this._version = range; | ||
return this; | ||
@@ -100,3 +121,3 @@ }; | ||
/** | ||
* Run | ||
* Run bin-wrapper | ||
* | ||
@@ -109,21 +130,27 @@ * @param {Array} cmd | ||
BinWrapper.prototype.run = function (cmd, cb) { | ||
var download = require('download'); | ||
var Download = require('download'); | ||
var files = this._parse(this.src()); | ||
var self = this; | ||
this.parse(this.src()); | ||
this.test(cmd, function (err) { | ||
this._test(cmd, function (err) { | ||
if (err) { | ||
return download(self.src(), self.dest(), { mode: '0755' }) | ||
.on('error', function (err) { | ||
var download = new Download({ mode: 777 }); | ||
files.forEach(function (file) { | ||
download.get(file, self.dest()); | ||
}); | ||
return download.run(function (err) { | ||
if (err) { | ||
return cb(err); | ||
}) | ||
.on('close', function () { | ||
self.test(cmd, function (err) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
} | ||
cb(); | ||
}); | ||
self._test(cmd, function (err) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
cb(); | ||
}); | ||
}); | ||
} | ||
@@ -136,14 +163,14 @@ | ||
/** | ||
* Test | ||
* Test binary | ||
* | ||
* @param {Array} cmd | ||
* @param {Function} cb | ||
* @api public | ||
* @api private | ||
*/ | ||
BinWrapper.prototype.test = function (cmd, cb) { | ||
BinWrapper.prototype._test = function (cmd, cb) { | ||
var self = this; | ||
if (this.use()) { | ||
return binCheck(self.use(), cmd, function (err, works) { | ||
if (this.path()) { | ||
return binCheck(self.path(), cmd, function (err, works) { | ||
if (err) { | ||
@@ -154,5 +181,15 @@ return cb(err); | ||
if (!works) { | ||
return cb('command failed'); | ||
return cb(new Error('The `' + self.use() + '` binary doesn\'t seem to work correctly.')); | ||
} | ||
if (self.version()) { | ||
return binVersionCheck(self.path(), self.version(), function (err) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
cb(); | ||
}); | ||
} | ||
cb(); | ||
@@ -162,3 +199,3 @@ }); | ||
cb('no binary found'); | ||
cb(new Error('Couldn\'t find the `' + this.use() + '` binary. Make sure it\'s installed and in your $PATH.')); | ||
}; | ||
@@ -170,6 +207,6 @@ | ||
* @param {Object} obj | ||
* @api public | ||
* @api private | ||
*/ | ||
BinWrapper.prototype.parse = function (obj) { | ||
BinWrapper.prototype._parse = function (obj) { | ||
var arch = process.arch === 'x64' ? 'x64' : process.arch === 'arm' ? 'arm' : 'x86'; | ||
@@ -188,4 +225,3 @@ var ret = []; | ||
this._src = ret; | ||
return this; | ||
return ret; | ||
}; | ||
@@ -192,0 +228,0 @@ |
{ | ||
"name": "bin-wrapper", | ||
"version": "0.3.4", | ||
"version": "0.4.0", | ||
"description": "Binary wrapper that makes your programs seamlessly available as local dependencies", | ||
@@ -16,3 +16,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "mocha --reporter list --timeout 0" | ||
"test": "mocha --reporter list --timeout 75000" | ||
}, | ||
@@ -30,3 +30,4 @@ "files": [ | ||
"bin-check": "^0.1.0", | ||
"download": "^0.1.2", | ||
"bin-version-check": "^0.1.0", | ||
"download": "^0.2.0", | ||
"find-file": "^0.1.2" | ||
@@ -33,0 +34,0 @@ }, |
@@ -15,5 +15,4 @@ # bin-wrapper [![Build Status](https://travis-ci.org/kevva/bin-wrapper.svg?branch=master)](https://travis-ci.org/kevva/bin-wrapper) | ||
var BinWrapper = require('bin-wrapper'); | ||
var bin = new BinWrapper({ global: true }); | ||
bin | ||
var bin = new BinWrapper(); | ||
.src('https://raw.github.com/yeoman/node-jpegtran-bin/0.2.4/vendor/win/x64/jpegtran.exe', 'win32', 'x64') | ||
@@ -23,15 +22,17 @@ .src('https://raw.github.com/yeoman/node-jpegtran-bin/0.2.4/vendor/win/x64/libjpeg-62.dll', 'win32', 'x64') | ||
.use('jpegtran.exe') | ||
.run(['--version'], function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
.version('>=1.3.0'); | ||
console.log('jpegtran is working'); | ||
}); | ||
bin.run(['--version'], function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
console.log('jpegtran is working'); | ||
}); | ||
``` | ||
Get the path to your binary with `bin.use`: | ||
Get the path to your binary with `bin.path()`: | ||
```js | ||
console.log(bin.use()); // => path/to/vendor/jpegtran.exe | ||
console.log(bin.path()); // => path/to/vendor/jpegtran.exe | ||
``` | ||
@@ -41,18 +42,27 @@ | ||
### new BinWrapper(opts) | ||
### new BinWrapper() | ||
Creates a new `BinWrapper` instance. Use the `global` option to enable or disable global checking. | ||
Creates a new `BinWrapper` instance. | ||
### .src(str) | ||
### .src(url, os, arch) | ||
Accepts a URL pointing to a file to download. | ||
### .dest(str) | ||
### .dest(dest) | ||
Accepts a path which the files will be downloaded to. | ||
### .use(str) | ||
### .use(bin) | ||
Define which file to use as the binary. | ||
### .path() | ||
Get the full path to your binary. | ||
### .version(range) | ||
Define a [semver range](https://github.com/isaacs/node-semver#ranges) to check | ||
the binary against. | ||
### .run(cmd, cb) | ||
@@ -64,13 +74,4 @@ | ||
## Options | ||
### global | ||
Type: `Boolean` | ||
Default: `true` | ||
Whether to check for a binary globally or not. | ||
## License | ||
MIT © [Kevin Mårtensson](http://kevinmartensson.com) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6630
176
74
4
+ Addedbin-version-check@^0.1.0
+ Addedarchive-type@0.1.4(transitive)
+ Addedarray-uniq@0.1.1(transitive)
+ Addedbin-version@0.1.0(transitive)
+ Addedbin-version-check@0.1.0(transitive)
+ Addeddecompress@0.3.3(transitive)
+ Addeddecompress-tar@0.1.1(transitive)
+ Addeddecompress-targz@0.1.1(transitive)
+ Addeddecompress-unzip@0.1.2(transitive)
+ Addeddownload@0.2.4(transitive)
+ Addedeach-async@1.1.1(transitive)
+ Addedfind-versions@0.1.1(transitive)
+ Addedfs-extra@0.10.00.8.1(transitive)
+ Addedget-stdin@1.0.0(transitive)
+ Addedis-7zip@0.1.0(transitive)
+ Addedis-bzip2@0.1.0(transitive)
+ Addedis-gzip@0.1.0(transitive)
+ Addedis-rar@0.1.0(transitive)
+ Addedis-tar@0.1.1(transitive)
+ Addedis-zip@0.1.0(transitive)
+ Addedjsonfile@1.1.11.2.0(transitive)
+ Addedminimist@0.1.0(transitive)
+ Addedncp@0.4.20.5.1(transitive)
+ Addedonetime@1.1.0(transitive)
+ Addedread-chunk@0.1.0(transitive)
+ Addedrimraf@2.2.8(transitive)
+ Addedsemver@2.3.2(transitive)
+ Addedsemver-regex@0.1.1(transitive)
+ Addedset-immediate-shim@1.0.1(transitive)
+ Addedsimple-bufferstream@0.0.4(transitive)
+ Addedtemp-write@0.3.1(transitive)
+ Addedware@0.2.10.3.0(transitive)
- Removedabbrev@1.1.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddecompress@0.2.5(transitive)
- Removeddownload@0.1.19(transitive)
- Removedduplexer@0.1.2(transitive)
- Removedeach-async@0.1.3(transitive)
- Removedext-list@0.2.0(transitive)
- Removedext-name@1.0.1(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedget-urls@0.1.2(transitive)
- Removedglob@7.2.3(transitive)
- Removedgot@0.2.0(transitive)
- Removedinflight@1.0.6(transitive)
- Removedisarray@0.0.1(transitive)
- Removedminimatch@3.1.2(transitive)
- Removednopt@2.2.1(transitive)
- Removedobject-keys@0.4.0(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedreadable-stream@1.0.34(transitive)
- Removedrimraf@2.7.1(transitive)
- Removedstream-combiner@0.0.4(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedtempfile@0.1.3(transitive)
- Removedthrough2@0.4.2(transitive)
- Removedunderscore.string@2.3.3(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedxtend@2.1.2(transitive)
Updateddownload@^0.2.0