From 59a012e4e474dc9a58dc53d40f2e3bfdf9aaa98c Mon Sep 17 00:00:00 2001 From: Kevin Martensson Date: Tue, 30 Dec 2014 13:54:25 +0100 Subject: [PATCH] Use binary destination set in package.json Fixes #31. --- index.js | 27 +++++++++++++++++++++++++-- package.json | 1 + 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 164e7b4..84840ac 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ var binCheck = require('bin-check'); var binVersionCheck = require('bin-version-check'); var Download = require('download'); +var find = require('findup-sync'); var globby = require('globby'); var isPathGlobal = require('is-path-global'); var osFilterObj = require('os-filter-obj'); @@ -63,8 +64,11 @@ BinWrapper.prototype.src = function (src, os, arch) { BinWrapper.prototype.dest = function (dest) { if (!arguments.length && !this._dest) { - this._dest = tempfile(); - return this._dest; + if (this.pkg()) { + this._dest = this.pkg(); + } else { + this._dest = tempfile(); + } } if (!arguments.length) { @@ -291,6 +295,25 @@ BinWrapper.prototype.get = function (cb) { download.run(cb); }; +/** + * Get destination from package.json + * + * @api private + */ + +BinWrapper.prototype.pkg = function () { + delete require.cache[__filename]; + + var dir = path.dirname(module.parent.filename); + var pkg = require(find('package.json', { cwd: dir })); + + if (pkg.bin[path.basename(this.use())]) { + return path.resolve(path.dirname(pkg.bin[path.basename(this.use())])); + } + + return path.resolve(path.dirname(pkg.bin)); +}; + /** * Module exports */ diff --git a/package.json b/package.json index 8c04ebd..307c46e 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "bin-version-check": "^2.1.0", "download": "^3.3.0", "download-status": "^2.0.0", + "findup-sync": "^0.2.1", "globby": "^1.0.0", "is-path-global": "^1.0.0", "lnfs": "^1.0.0",