Socket
Socket
Sign inDemoInstall

bin-wrapper

Package Overview
Dependencies
Maintainers
2
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bin-wrapper - npm Package Compare versions

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)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc