diff --git a/install.js b/install.js index 8b9244bbe..c45f04bab 100644 --- a/install.js +++ b/install.js @@ -26,7 +26,7 @@ var checkPhantomjsVersion = util.checkPhantomjsVersion var getTargetPlatform = util.getTargetPlatform var getTargetArch = util.getTargetArch var getDownloadSpec = util.getDownloadSpec -var maybeLinkLibModule = util.maybeLinkLibModule +var findValidPhantomJsBinary = util.findValidPhantomJsBinary var verifyChecksum = util.verifyChecksum var writeLocationFile = util.writeLocationFile @@ -322,9 +322,12 @@ function copyIntoPlace(extractedPath, targetPath) { */ function tryPhantomjsInLib() { return kew.fcall(function () { - return maybeLinkLibModule(path.resolve(__dirname, './lib/location.js')) - }).then(function (success) { - if (success) exit(0) + return findValidPhantomJsBinary(path.resolve(__dirname, './lib/location.js')) + }).then(function (binaryLocation) { + if (binaryLocation) { + console.log('PhantomJS is previously installed at', binaryLocation) + exit(0) + } }).fail(function () { // silently swallow any errors }) @@ -356,9 +359,14 @@ function tryPhantomjsOnPath() { if (/NPM_INSTALL_MARKER/.test(contents)) { console.log('Looks like an `npm install -g`') - return maybeLinkLibModule(path.resolve(fs.realpathSync(phantomPath), '../../lib/location')) - .then(function (success) { - if (success) exit(0) + var phantomLibPath = path.resolve(fs.realpathSync(phantomPath), '../../lib/location') + return findValidPhantomJsBinary(phantomLibPath) + .then(function (binaryLocation) { + if (binaryLocation) { + writeLocationFile(binaryLocation) + console.log('PhantomJS linked at', phantomLibPath) + exit(0) + } console.log('Could not link global install, skipping...') }) } else { diff --git a/lib/util.js b/lib/util.js index 568ef1769..3cf515a6d 100644 --- a/lib/util.js +++ b/lib/util.js @@ -16,10 +16,10 @@ var libPath = __dirname /** * Given a lib/location file of a PhantomJS previously installed with NPM, - * try to link the binary to this lib/location. - * @return {Promise} True on success + * is there a valid PhantomJS binary at this lib/location. + * @return {Promise} resolved location of phantomjs binary on success */ -function maybeLinkLibModule(libPath) { +function findValidPhantomJsBinary(libPath) { return kew.fcall(function () { var libModule = require(libPath) if (libModule.location && @@ -29,9 +29,7 @@ function maybeLinkLibModule(libPath) { if (fs.statSync(resolvedLocation)) { return checkPhantomjsVersion(resolvedLocation).then(function (matches) { if (matches) { - writeLocationFile(resolvedLocation) - console.log('PhantomJS linked at', resolvedLocation) - return kew.resolve(true) + return kew.resolve(resolvedLocation) } }) } @@ -157,7 +155,7 @@ module.exports = { getDownloadSpec: getDownloadSpec, getTargetPlatform: getTargetPlatform, getTargetArch: getTargetArch, - maybeLinkLibModule: maybeLinkLibModule, + findValidPhantomJsBinary: findValidPhantomJsBinary, verifyChecksum: verifyChecksum, writeLocationFile: writeLocationFile } diff --git a/test/tests.js b/test/tests.js index 0f229d6ad..a0fd95d01 100644 --- a/test/tests.js +++ b/test/tests.js @@ -67,17 +67,17 @@ exports.testCleanPath = function (test) { } exports.testBogusReinstallLocation = function (test) { - util.maybeLinkLibModule('./blargh') - .then(function (success) { - test.ok(!success, 'Expected link to fail') + util.findValidPhantomJsBinary('./blargh') + .then(function (binaryLocation) { + test.ok(!binaryLocation, 'Expected link to fail') test.done() }) } exports.testSuccessfulReinstallLocation = function (test) { - util.maybeLinkLibModule(path.resolve(__dirname, '../lib/location')) - .then(function (success) { - test.ok(success, 'Expected link to succeed') + util.findValidPhantomJsBinary(path.resolve(__dirname, '../lib/location')) + .then(function (binaryLocation) { + test.ok(binaryLocation, 'Expected link to succeed') test.done() }) }