Skip to content

Commit 9f904da

Browse files
committed
Merge pull request #75 from fardog/enhancement-71
Show help and version even if phantom isn't present. Fixes #71
2 parents 439cca9 + 7bcbe6f commit 9f904da

File tree

1 file changed

+75
-53
lines changed

1 file changed

+75
-53
lines changed

lib/cli.js

Lines changed: 75 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ var PHANTOM_VERSION = "^1.9.0"
1010
var info = chalk.blue.bold
1111
, note = chalk.green.bold
1212

13-
var cli = function(options) {
13+
module.exports = function() {
14+
return new cli()
15+
}()
16+
17+
function cli(options) {
1418
this.options = {
1519
alias: {
1620
help: 'h'
@@ -52,9 +56,11 @@ cli.prototype.parse = function(argv, next) {
5256
if (options.version) {
5357
var pkg = require('../package.json')
5458
this.message = "" + pkg.version
59+
next(null, this.message)
5560
}
5661
else if (options.help) {
5762
this.message = this.helpMessage.join('\n')
63+
next(null, this.message)
5864
}
5965
else {
6066
options.files = options._
@@ -71,65 +77,81 @@ cli.prototype.parse = function(argv, next) {
7177
}
7278
}
7379
}.bind(this))
74-
}
7580

76-
if (options.svg && !options.png) {
77-
options.png = false
78-
}
79-
else {
80-
options.png = true
81+
// set svg/png flags appropriately
82+
if (options.svg && !options.png) {
83+
options.png = false
84+
}
85+
else {
86+
options.png = true
87+
}
88+
89+
this.checkPhantom = createCheckPhantom(options.phantomPath)
90+
91+
this.checkPhantom(function(err, path) {
92+
if(err) {
93+
this.errors.push(err)
94+
}
95+
options.phantomPath = path
96+
next(
97+
this.errors.length > 0 ? this.errors : null
98+
, this.message
99+
, options
100+
)
101+
}.bind(this))
81102
}
103+
}
104+
105+
function createCheckPhantom(_phantomPath) {
106+
var phantomPath = _phantomPath
107+
, phantomVersion
82108

83-
// If phantom hasn't been specified, see if we can find it
84-
if (!options.phantomPath) {
85-
try {
86-
var phantom = require('phantomjs')
87-
options.phantomPath = phantom.path
88-
} catch (e) {
109+
return function checkPhantom(_next) {
110+
var next = _next || function() {}
111+
, err
112+
113+
if (typeof phantomPath === 'undefined') {
89114
try {
90-
options.phantomPath = which.sync('phantomjs')
115+
var phantom = require('phantomjs')
116+
phantomPath = phantom.path
91117
} catch (e) {
92-
if (!options.phantomPath) {
93-
var err = [
94-
"Cannot find phantomjs in your PATH. If phantomjs is installed"
95-
, "you may need to specify its path manually with the '-e' option."
96-
, "If it is not installed, you should view the README for further"
97-
, "details."
98-
]
99-
100-
this.errors.push(new Error(err.join('\n')))
101-
next(
102-
this.errors.length > 0 ? this.errors : null
103-
, this.message
104-
, options)
105-
106-
return
118+
try {
119+
phantomPath = which.sync('phantomjs')
120+
} catch (e) {
121+
if (!phantomPath) {
122+
phantomPath = null
123+
err = new Error(
124+
[
125+
"Cannot find phantomjs in your PATH. If phantomjs is installed"
126+
, "you may need to specify its path manually with the '-e' option."
127+
, "Run this executable with '--help' or view the README for more"
128+
, "details."
129+
].join('\n')
130+
)
131+
132+
next(err)
133+
return
134+
}
107135
}
108136
}
109137
}
110-
}
111-
112-
// If we have phantompath, see if its version satisfies our requirements
113-
exec(options.phantomPath + ' --version', function(err, stdout, stderr) {
114-
if (err) {
115-
this.errors.push(
116-
new Error("Could not find phantomjs at the specified path.")
117-
)
118-
}
119-
else if (!semver.satisfies(stdout, PHANTOM_VERSION)) {
120-
this.message = note(
121-
'mermaid requires phantomjs '
122-
+ PHANTOM_VERSION
123-
+ ' to be installed, found version '
124-
+ stdout
125-
)
126-
}
127-
next(this.errors.length > 0 ? this.errors : null, this.message, options)
128-
}.bind(this))
129138

139+
// If we have phantompath, see if its version satisfies our requirements
140+
exec(phantomPath + ' --version', function(err, stdout, stderr) {
141+
if (err) {
142+
next(new Error("Could not find phantomjs at the specified path."))
143+
}
144+
else if (!semver.satisfies(stdout, PHANTOM_VERSION)) {
145+
next(new Error(
146+
'mermaid requires phantomjs '
147+
+ PHANTOM_VERSION
148+
+ ' to be installed, found version '
149+
+ stdout
150+
))
151+
}
152+
else {
153+
next(null, phantomPath)
154+
}
155+
})
156+
}
130157
}
131-
132-
133-
module.exports = function() {
134-
return new cli()
135-
}()

0 commit comments

Comments
 (0)