From b9c93fb977c68e42ebe7f7caee258864f6bec1b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=B6nthal?= Date: Fri, 15 Dec 2017 15:59:47 +0100 Subject: [PATCH] use system binary if possible --- index.js | 7 +++++++ package.json | 3 ++- readme.md | 1 + test.js | 10 ++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e2a01e2..bf16294 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ var binCheck = lazyReq('bin-check'); var binVersionCheck = lazyReq('bin-version-check'); var Download = lazyReq('download'); var osFilterObj = lazyReq('os-filter-obj'); +var which = require('which'); /** * Initialize a new `BinWrapper` @@ -109,6 +110,12 @@ BinWrapper.prototype.version = function (range) { */ BinWrapper.prototype.path = function () { + var systemBin = which.sync(this.use(), {nothrow: true}) + + if (systemBin) { + return systemBin; + } + return path.join(this.dest(), this.use()); }; diff --git a/package.json b/package.json index a253503..535cc44 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "download": "^4.0.0", "each-async": "^1.1.1", "lazy-req": "^1.0.0", - "os-filter-obj": "^1.0.0" + "os-filter-obj": "^1.0.0", + "which": "^1.3.0" }, "devDependencies": { "ava": "*", diff --git a/readme.md b/readme.md index ff03dda..ccb553d 100644 --- a/readme.md +++ b/readme.md @@ -35,6 +35,7 @@ Get the path to your binary with `bin.path()`: console.log(bin.path()); // => path/to/vendor/gifsicle ``` +> if you have the requested binary installed globally, this one will be used instead (if everything works) ## API diff --git a/test.js b/test.js index 865e19a..ef6e0b9 100644 --- a/test.js +++ b/test.js @@ -121,3 +121,13 @@ test('error if no binary is found and no source is provided', t => { t.throws(pify(bin.run.bind(bin))(), 'No binary found matching your system. It\'s probably not supported.'); }); + +test('can use a system binary if found in PATH', async t => { + const bin = new Fn() + .dest(tempfile()) + .use(process.platform === 'win32' ? 'node.exe' : 'node'); + + await pify(bin.run.bind(bin))(); + + t.true(await pathExists(bin.path())); +});