UNPKG

1.54 kBJavaScriptView Raw
1// Copyright 2013 The Obvious Corporation.
2
3/**
4 * @fileoverview Helpers made available via require('phantomjs') once package is
5 * installed.
6 */
7
8var fs = require('fs')
9var path = require('path')
10
11
12/**
13 * Where the phantom binary can be found.
14 * @type {string}
15 */
16try {
17 var location = require('./location')
18 exports.path = path.resolve(__dirname, location.location)
19 exports.platform = location.platform
20 exports.arch = location.arch
21} catch(e) {
22 // Must be running inside install script.
23 exports.path = null
24}
25
26
27/**
28 * The version of phantomjs installed by this package.
29 * @type {number}
30 */
31exports.version = '2.1.1'
32
33
34/**
35 * Returns a clean path that helps avoid `which` finding bin files installed
36 * by NPM for this repo.
37 * @param {string} path
38 * @return {string}
39 */
40exports.cleanPath = function (path) {
41 return path
42 .replace(/:[^:]*node_modules[^:]*/g, '')
43 .replace(/(^|:)\.\/bin(\:|$)/g, ':')
44 .replace(/^:+/, '')
45 .replace(/:+$/, '')
46}
47
48
49// Make sure the binary is executable. For some reason doing this inside
50// install does not work correctly, likely due to some NPM step.
51if (exports.path) {
52 try {
53 // avoid touching the binary if it's already got the correct permissions
54 var st = fs.statSync(exports.path);
55 var mode = st.mode | parseInt("0555", 8);
56 if (mode !== st.mode) {
57 fs.chmodSync(exports.path, mode);
58 }
59 } catch (e) {
60 // Just ignore error if we don't have permission.
61 // We did our best. Likely because phantomjs was already installed.
62 }
63}