From 54977146f375bca55ffd2963ae286bdca92c45b9 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 21 Apr 2017 09:40:23 +0200 Subject: [PATCH] fix: Try to find phantomjs binary in `.bin` Throw explicit error when the binary cannot be found Ref #120, #138, #141 --- index.js | 23 +++++++++++++++++++---- package.json | 1 + yarn.lock | 28 +++++++++++++++++++++------- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index af57ec9..4c21330 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,23 @@ var fs = require('fs') var path = require('path') var _ = require('lodash') +var phantomSource = require('phantomjs-prebuilt').path + +if (phantomSource === null) { + var which = require('npm-which')(process.cwd()) + + try { + phantomSource = which.sync('phantomjs') + } catch (e) {} +} + +if (phantomSource == null) { + var installScriptLocation = require.resolve('phantomjs-prebuilt/install') + var installScriptCmd = '`node ' + installScriptLocation + '`' + + throw new Error("PhantomJS binary not found, make sure `phantomjs-prebuilt`'s `postinstall` is run, or manually run " + installScriptCmd) +} + function serializeOption (value) { if (typeof value === 'function') { return value.toString() @@ -13,8 +30,6 @@ var phantomJSExePath = function () { // If the path we're given by phantomjs is to a .cmd, it is pointing to a global copy. // Using the cmd as the process to execute causes problems cleaning up the processes // so we walk from the cmd to the phantomjs.exe and use that instead. - var phantomSource = require('phantomjs-prebuilt').path - if (path.extname(phantomSource).toLowerCase() === '.cmd') { var phantomPackage = require('phantomjs-prebuilt/package.json') return path.join( @@ -102,8 +117,8 @@ PhantomJSBrowser.prototype = { name: 'PhantomJS', DEFAULT_CMD: { - linux: require('phantomjs-prebuilt').path, - darwin: require('phantomjs-prebuilt').path, + linux: phantomSource, + darwin: phantomSource, win32: phantomJSExePath() }, ENV_CMD: 'PHANTOMJS_BIN' diff --git a/package.json b/package.json index c10c867..dabaa3e 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "author": "Vojta Jina ", "dependencies": { "lodash": "^4.0.1", + "npm-which": "^3.0.1", "phantomjs-prebuilt": "^2.1.7" }, "peerDependencies": { diff --git a/yarn.lock b/yarn.lock index 9760e41..d352dd3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2045,6 +2045,20 @@ normalize-path@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-2.0.1.tgz#47886ac1662760d4261b7d979d241709d3ce3f7a" +npm-path@^2.0.2: + version "2.0.3" + resolved "https://registry.yarnpkg.com/npm-path/-/npm-path-2.0.3.tgz#15cff4e1c89a38da77f56f6055b24f975dfb2bbe" + dependencies: + which "^1.2.10" + +npm-which@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/npm-which/-/npm-which-3.0.1.tgz#9225f26ec3a285c209cae67c3b11a6b4ab7140aa" + dependencies: + commander "^2.9.0" + npm-path "^2.0.2" + which "^1.2.10" + npmlog@^4.0.1: version "4.0.2" resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-4.0.2.tgz#d03950e0e78ce1527ba26d2a7592e9348ac3e75f" @@ -2480,11 +2494,11 @@ safe-buffer@^5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.0.1.tgz#d263ca54696cd8a306b5ca6551e92de57918fbe7" -"semver@2 || 3 || 4", "semver@2 || 3 || 4 || 5", semver@^4.3.3, semver@~4.3.3: +"semver@2 || 3 || 4", semver@^4.3.3, semver@~4.3.3: version "4.3.6" resolved "https://registry.yarnpkg.com/semver/-/semver-4.3.6.tgz#300bc6e0e86374f7ba61068b5b1ecd57fc6532da" -semver@~5.3.0: +"semver@2 || 3 || 4 || 5", semver@~5.3.0: version "5.3.0" resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f" @@ -2831,16 +2845,16 @@ void-elements@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/void-elements/-/void-elements-2.0.1.tgz#c066afb582bb1cb4128d60ea92392e94d5e9dbec" -which@~1.0.5: - version "1.0.9" - resolved "https://registry.yarnpkg.com/which/-/which-1.0.9.tgz#460c1da0f810103d0321a9b633af9e575e64486f" - -which@~1.2.10: +which@^1.2.10, which@~1.2.10: version "1.2.12" resolved "https://registry.yarnpkg.com/which/-/which-1.2.12.tgz#de67b5e450269f194909ef23ece4ebe416fa1192" dependencies: isexe "^1.1.1" +which@~1.0.5: + version "1.0.9" + resolved "https://registry.yarnpkg.com/which/-/which-1.0.9.tgz#460c1da0f810103d0321a9b633af9e575e64486f" + wide-align@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/wide-align/-/wide-align-1.1.0.tgz#40edde802a71fea1f070da3e62dcda2e7add96ad"