cleanPath = function (path) {
return path
.replace(/:[^:]*node_modules[^:]*/g, '')
.replace(/(^|:)\.\/bin(\:|$)/g, ':')
.replace(/^:+/, '')
.replace(/:+$/, '')
}...
exit(1)
}
})
// NPM adds bin directories to the path, which will cause `which` to find the
// bin for this package not the actual phantomjs bin. Also help out people who
// put ./bin on their path
process.env.PATH = helper.cleanPath(originalPath)
var libPath = path.join(__dirname, 'lib')
var pkgPath = path.join(libPath, 'phantom')
var phantomPath = null
// If the user manually installed PhantomJS, we want
// to use the existing version.
...exec = function () {
var args = Array.prototype.slice.call(arguments)
return spawn(exports.path, args)
}...
```
Or `exec()` method is also provided for convenience:
```javascript
var phantomjs = require('phantomjs-prebuilt')
var program = phantomjs.exec('phantomjs-script.js', 'arg1', '
;arg2')
program.stdout.pipe(process.stdout)
program.stderr.pipe(process.stderr)
program.on('exit', code => {
// do something on end
})
```
...run = function () {
var args = arguments
return new Promise(function (resolve, reject) {
try {
var program = exports.exec.apply(null, args)
var isFirst = true
var stderr = ''
program.stdout.on('data', function () {
// This detects PhantomJS instance get ready.
if (!isFirst) return
isFirst = false
resolve(program)
})
program.stderr.on('data', function (data) {
stderr = stderr + data.toString('utf8')
})
program.on('error', function (err) {
if (!isFirst) return
isFirst = false
reject(err)
})
program.on('exit', function (code) {
if (!isFirst) return
isFirst = false
if (code == 0) {
// PhantomJS doesn't use exit codes correctly :(
if (stderr.indexOf('Error:') == 0) {
reject(new Error(stderr))
} else {
resolve(program)
}
} else {
reject(new Error('Exit code: ' + code))
}
})
} catch (err) {
reject(err)
}
})
}...
`run()` method detects when PhantomJS gets ready. It's handy to use with WebDriver (Selenium).
```javascript
var phantomjs = require('phantomjs-prebuilt')
var webdriverio = require('webdriverio')
var wdOpts = { desiredCapabilities: { browserName: 'phantomjs' } }
phantomjs.run('--webdriver=4444').then(program => {
webdriverio.remote(wdOpts).init()
.url('https://developer.mozilla.org/en-US/')
.getTitle().then(title => {
console.log(title) // 'Mozilla Developer Network'
program.kill() // quits PhantomJS
})
})
...function checkPhantomjsVersion(phantomPath) {
console.log('Found PhantomJS at', phantomPath, '...verifying')
return kew.nfcall(cp.execFile, phantomPath, ['--version']).then(function (stdout) {
var version = stdout.trim()
if (helper.version == version) {
return true
} else {
console.log('PhantomJS detected, but wrong version', stdout.trim(), '@', phantomPath + '.')
return false
}
}).fail(function (err) {
console.error('Error verifying phantomjs, continuing', err)
return false
})
}n/a
function findValidPhantomJsBinary(libPath) {
return kew.fcall(function () {
var libModule = require(libPath)
if (libModule.location &&
getTargetPlatform() == libModule.platform &&
getTargetArch() == libModule.arch) {
var resolvedLocation = path.resolve(path.dirname(libPath), libModule.location)
if (fs.statSync(resolvedLocation)) {
return checkPhantomjsVersion(resolvedLocation).then(function (matches) {
if (matches) {
return kew.resolve(resolvedLocation)
}
})
}
}
return false
}).fail(function () {
return false
})
}n/a
function getDownloadSpec() {
var cdnUrl = process.env.npm_config_phantomjs_cdnurl ||
process.env.PHANTOMJS_CDNURL ||
DEFAULT_CDN
var downloadUrl = cdnUrl + '/phantomjs-' + helper.version + '-'
var checksum = ''
var platform = getTargetPlatform()
var arch = getTargetArch()
if (platform === 'linux' && arch === 'x64') {
downloadUrl += 'linux-x86_64.tar.bz2'
checksum = '86dd9a4bf4aee45f1a84c9f61cf1947c1d6dce9b9e8d2a907105da7852460d2f'
} else if (platform === 'linux' && arch == 'ia32') {
downloadUrl += 'linux-i686.tar.bz2'
checksum = '80e03cfeb22cc4dfe4e73b68ab81c9fdd7c78968cfd5358e6af33960464f15e3'
} else if (platform === 'darwin') {
downloadUrl += 'macosx.zip'
checksum = '538cf488219ab27e309eafc629e2bcee9976990fe90b1ec334f541779150f8c1'
} else if (platform === 'win32') {
downloadUrl += 'windows.zip'
checksum = 'd9fb05623d6b26d3654d008eab3adafd1f6350433dfd16138c46161f42c7dcc8'
} else {
return null
}
return {url: downloadUrl, checksum: checksum}
}n/a
function getTargetArch() {
return process.env.PHANTOMJS_ARCH || process.arch
}n/a
function getTargetPlatform() {
return process.env.PHANTOMJS_PLATFORM || process.platform
}n/a
function verifyChecksum(fileName, checksum) {
return kew.resolve(hasha.fromFile(fileName, {algorithm: 'sha256'})).then(function (hash) {
var result = checksum == hash
if (result) {
console.log('Verified checksum of previously downloaded file')
} else {
console.log('Checksum did not match')
}
return result
}).fail(function (err) {
console.error('Failed to verify checksum: ', err)
return false
})
}n/a
function writeLocationFile(location) {
console.log('Writing location.js file')
if (getTargetPlatform() === 'win32') {
location = location.replace(/\\/g, '\\\\')
}
var platform = getTargetPlatform()
var arch = getTargetArch()
var contents = 'module.exports.location = "' + location + '"\n'
if (/^[a-zA-Z0-9]*$/.test(platform) && /^[a-zA-Z0-9]*$/.test(arch)) {
contents +=
'module.exports.platform = "' + getTargetPlatform() + '"\n' +
'module.exports.arch = "' + getTargetArch() + '"\n'
}
fs.writeFileSync(path.join(libPath, 'location.js'), contents)
}n/a