Show help and version even if phantom isn't present. Fixes #71

This commit is contained in:
fardog
2014-12-28 00:26:37 -08:00
parent 26d0908b27
commit 7bcbe6f469

View File

@@ -10,7 +10,11 @@ var PHANTOM_VERSION = "^1.9.0"
var info = chalk.blue.bold var info = chalk.blue.bold
, note = chalk.green.bold , note = chalk.green.bold
var cli = function(options) { module.exports = function() {
return new cli()
}()
function cli(options) {
this.options = { this.options = {
alias: { alias: {
help: 'h' help: 'h'
@@ -52,9 +56,11 @@ cli.prototype.parse = function(argv, next) {
if (options.version) { if (options.version) {
var pkg = require('../package.json') var pkg = require('../package.json')
this.message = "" + pkg.version this.message = "" + pkg.version
next(null, this.message)
} }
else if (options.help) { else if (options.help) {
this.message = this.helpMessage.join('\n') this.message = this.helpMessage.join('\n')
next(null, this.message)
} }
else { else {
options.files = options._ options.files = options._
@@ -71,8 +77,8 @@ cli.prototype.parse = function(argv, next) {
} }
} }
}.bind(this)) }.bind(this))
}
// set svg/png flags appropriately
if (options.svg && !options.png) { if (options.svg && !options.png) {
options.png = false options.png = false
} }
@@ -80,29 +86,50 @@ cli.prototype.parse = function(argv, next) {
options.png = true options.png = true
} }
// If phantom hasn't been specified, see if we can find it this.checkPhantom = createCheckPhantom(options.phantomPath)
if (!options.phantomPath) {
try {
var phantom = require('phantomjs')
options.phantomPath = phantom.path
} catch (e) {
try {
options.phantomPath = which.sync('phantomjs')
} catch (e) {
if (!options.phantomPath) {
var err = [
"Cannot find phantomjs in your PATH. If phantomjs is installed"
, "you may need to specify its path manually with the '-e' option."
, "If it is not installed, you should view the README for further"
, "details."
]
this.errors.push(new Error(err.join('\n'))) this.checkPhantom(function(err, path) {
if(err) {
this.errors.push(err)
}
options.phantomPath = path
next( next(
this.errors.length > 0 ? this.errors : null this.errors.length > 0 ? this.errors : null
, this.message , this.message
, options) , options
)
}.bind(this))
}
}
function createCheckPhantom(_phantomPath) {
var phantomPath = _phantomPath
, phantomVersion
return function checkPhantom(_next) {
var next = _next || function() {}
, err
if (typeof phantomPath === 'undefined') {
try {
var phantom = require('phantomjs')
phantomPath = phantom.path
} catch (e) {
try {
phantomPath = which.sync('phantomjs')
} catch (e) {
if (!phantomPath) {
phantomPath = null
err = new Error(
[
"Cannot find phantomjs in your PATH. If phantomjs is installed"
, "you may need to specify its path manually with the '-e' option."
, "Run this executable with '--help' or view the README for more"
, "details."
].join('\n')
)
next(err)
return return
} }
} }
@@ -110,26 +137,21 @@ cli.prototype.parse = function(argv, next) {
} }
// If we have phantompath, see if its version satisfies our requirements // If we have phantompath, see if its version satisfies our requirements
exec(options.phantomPath + ' --version', function(err, stdout, stderr) { exec(phantomPath + ' --version', function(err, stdout, stderr) {
if (err) { if (err) {
this.errors.push( next(new Error("Could not find phantomjs at the specified path."))
new Error("Could not find phantomjs at the specified path.")
)
} }
else if (!semver.satisfies(stdout, PHANTOM_VERSION)) { else if (!semver.satisfies(stdout, PHANTOM_VERSION)) {
this.message = note( next(new Error(
'mermaid requires phantomjs ' 'mermaid requires phantomjs '
+ PHANTOM_VERSION + PHANTOM_VERSION
+ ' to be installed, found version ' + ' to be installed, found version '
+ stdout + stdout
) ))
}
else {
next(null, phantomPath)
}
})
} }
next(this.errors.length > 0 ? this.errors : null, this.message, options)
}.bind(this))
} }
module.exports = function() {
return new cli()
}()