Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 28, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 75 additions & 53 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ var PHANTOM_VERSION = "^1.9.0"
var info = chalk.blue.bold
, note = chalk.green.bold

var cli = function(options) {
module.exports = function() {
return new cli()
}()

function cli(options) {
this.options = {
alias: {
help: 'h'
Expand Down Expand Up @@ -52,9 +56,11 @@ cli.prototype.parse = function(argv, next) {
if (options.version) {
var pkg = require('../package.json')
this.message = "" + pkg.version
next(null, this.message)
}
else if (options.help) {
this.message = this.helpMessage.join('\n')
next(null, this.message)
}
else {
options.files = options._
Expand All @@ -71,65 +77,81 @@ cli.prototype.parse = function(argv, next) {
}
}
}.bind(this))
}

if (options.svg && !options.png) {
options.png = false
}
else {
options.png = true
// set svg/png flags appropriately
if (options.svg && !options.png) {
options.png = false
}
else {
options.png = true
}

this.checkPhantom = createCheckPhantom(options.phantomPath)

this.checkPhantom(function(err, path) {
if(err) {
this.errors.push(err)
}
options.phantomPath = path
next(
this.errors.length > 0 ? this.errors : null
, this.message
, options
)
}.bind(this))
}
}

function createCheckPhantom(_phantomPath) {
var phantomPath = _phantomPath
, phantomVersion

// If phantom hasn't been specified, see if we can find it
if (!options.phantomPath) {
try {
var phantom = require('phantomjs')
options.phantomPath = phantom.path
} catch (e) {
return function checkPhantom(_next) {
var next = _next || function() {}
, err

if (typeof phantomPath === 'undefined') {
try {
options.phantomPath = which.sync('phantomjs')
var phantom = require('phantomjs')
phantomPath = phantom.path
} 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')))
next(
this.errors.length > 0 ? this.errors : null
, this.message
, options)

return
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
}
}
}
}
}

// If we have phantompath, see if its version satisfies our requirements
exec(options.phantomPath + ' --version', function(err, stdout, stderr) {
if (err) {
this.errors.push(
new Error("Could not find phantomjs at the specified path.")
)
}
else if (!semver.satisfies(stdout, PHANTOM_VERSION)) {
this.message = note(
'mermaid requires phantomjs '
+ PHANTOM_VERSION
+ ' to be installed, found version '
+ stdout
)
}
next(this.errors.length > 0 ? this.errors : null, this.message, options)
}.bind(this))

// If we have phantompath, see if its version satisfies our requirements
exec(phantomPath + ' --version', function(err, stdout, stderr) {
if (err) {
next(new Error("Could not find phantomjs at the specified path."))
}
else if (!semver.satisfies(stdout, PHANTOM_VERSION)) {
next(new Error(
'mermaid requires phantomjs '
+ PHANTOM_VERSION
+ ' to be installed, found version '
+ stdout
))
}
else {
next(null, phantomPath)
}
})
}
}


module.exports = function() {
return new cli()
}()