bin-wrapper
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -10,2 +10,3 @@ 'use strict'; | ||
var path = require('path'); | ||
var ProgressBar = require('progress'); | ||
var spawn = require('child_process').spawn; | ||
@@ -27,3 +28,3 @@ var which = require('which'); | ||
this.dest = this.opts.path; | ||
this.path = path.join(this.dest, this.bin); | ||
this.path = this._path() || path.join(this.dest, this.bin); | ||
this.url = this.opts.url; | ||
@@ -58,16 +59,19 @@ this.src = this.opts.src; | ||
if (fs.existsSync(this.path)) { | ||
if (this._path()) { | ||
return self._test(cmd, cb); | ||
} | ||
if (isbin(this.bin)) { | ||
self.path = which.sync(self.bin); | ||
return self._test(cmd, cb); | ||
} | ||
download(this.url, this.dest, { mode: '0755', proxy: this.proxy }).on('close', function () { | ||
return self._test(cmd, cb); | ||
}); | ||
this._download(this.url, this.dest, { mode: '0755', proxy: this.proxy }) | ||
.on('close', function () { | ||
return self._test(cmd, cb); | ||
}); | ||
}; | ||
/** | ||
* Download source and build a binary | ||
* | ||
* @param {Function} cb | ||
* @api public | ||
*/ | ||
BinWrapper.prototype.build = function (cb) { | ||
@@ -77,3 +81,3 @@ var self = this; | ||
var tmp = path.join(tmpDir, this.name); | ||
var get = download(this.src, tmp, { extract: true, strip: '1', proxy: this.proxy }); | ||
var get = this._download(this.src, tmp, { extract: true, strip: '1', proxy: this.proxy }); | ||
@@ -88,3 +92,3 @@ if (!cb || !mout.lang.isFunction(cb)) { | ||
return get.on('close', function () { | ||
get.on('close', function () { | ||
exec(self.buildScript, { cwd: tmp }, function (err) { | ||
@@ -101,2 +105,57 @@ if (err) { | ||
/** | ||
* Download a string or an array of files | ||
* | ||
* @param {String|Array} src | ||
* @param {String} dest | ||
* @param {Object} opts | ||
* @api private | ||
*/ | ||
BinWrapper.prototype._download = function (src, dest, opts) { | ||
var get = download(src, dest, opts); | ||
get.on('response', function (res) { | ||
var len = parseInt(res.headers['content-length'], 10); | ||
var bar = new ProgressBar(' ' + path.basename(src) + ': downloading [:bar] :percent :etas', { | ||
complete: '=', | ||
incomplete: ' ', | ||
width: 20, | ||
total: len | ||
}); | ||
res.on('data', function (data) { | ||
bar.tick(data.length); | ||
}); | ||
res.on('end', function () { | ||
console.log('\n'); | ||
}); | ||
}); | ||
return get; | ||
}; | ||
/** | ||
* If a binary exists, get its path | ||
* | ||
* @api private | ||
*/ | ||
BinWrapper.prototype._path = function () { | ||
var self = this; | ||
if (fs.existsSync(path.join(this.dest, this.bin))) { | ||
return path.join(self.dest, self.bin); | ||
} | ||
if (isbin(this.bin)) { | ||
if (!which.sync(self.bin).indexOf('node_modules/.bin')) { | ||
return which.sync(self.bin); | ||
} | ||
} | ||
return false; | ||
}; | ||
/** | ||
* Test if a binary is working by checking its exit code | ||
@@ -119,7 +178,5 @@ * | ||
bin.on('exit', function (code) { | ||
working = code === 0 ? true : false; | ||
working = code === 0; | ||
return cb(working); | ||
}); | ||
return bin; | ||
}; | ||
@@ -126,0 +183,0 @@ |
{ | ||
"name": "bin-wrapper", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Binary wrapper for Node.js that makes your programs seamlessly available as local dependencies", | ||
@@ -31,2 +31,3 @@ "keywords": [ | ||
"mout": "~0.6.0", | ||
"progress": "~1.1.0", | ||
"which": "~1.0.5" | ||
@@ -33,0 +34,0 @@ }, |
# bin-wrapper [![Build Status](https://secure.travis-ci.org/kevva/bin-wrapper.png?branch=master)](http://travis-ci.org/kevva/bin-wrapper) | ||
Binary wrapper for Node.js that makes your programs seamlessly available as local dependencies | ||
## Getting started | ||
@@ -18,4 +20,6 @@ | ||
bin: 'gifsicle', | ||
path: __dirname + '/vendor', | ||
path: 'vendor', | ||
url: 'http://url/to/gifsicle', | ||
src: 'http://www.lcdf.org/gifsicle/gifsicle-1.71.tar.gz', | ||
buildScript: './configure --bindir="vendor" && make install', | ||
platform: { | ||
@@ -31,9 +35,11 @@ win32: { | ||
} | ||
var bin = new Bin(opts); | ||
var bin = new Bin(opts) | ||
bin.check('--version', function (works) { | ||
if (works) { | ||
console.log('Binary downloaded and passed the test!') | ||
if (!works) { | ||
console.log('Pre-build test failed, compiling from source'); | ||
return bin.build(); | ||
} | ||
console.log('Binary passed the test'); | ||
}); | ||
@@ -56,4 +62,9 @@ ``` | ||
### .build(cb) | ||
Download the source archive defined in the `src` property and build it using the | ||
build script defined in the `buildScript` property. | ||
## License | ||
[MIT License](http://en.wikipedia.org/wiki/MIT_License) (c) [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
7473
181
68
5
+ Addedprogress@~1.1.0
+ Addedprogress@1.1.8(transitive)