@@ -10,7 +10,11 @@ var PHANTOM_VERSION = "^1.9.0"
10
10
var info = chalk . blue . bold
11
11
, note = chalk . green . bold
12
12
13
- var cli = function ( options ) {
13
+ module . exports = function ( ) {
14
+ return new cli ( )
15
+ } ( )
16
+
17
+ function cli ( options ) {
14
18
this . options = {
15
19
alias : {
16
20
help : 'h'
@@ -52,9 +56,11 @@ cli.prototype.parse = function(argv, next) {
52
56
if ( options . version ) {
53
57
var pkg = require ( '../package.json' )
54
58
this . message = "" + pkg . version
59
+ next ( null , this . message )
55
60
}
56
61
else if ( options . help ) {
57
62
this . message = this . helpMessage . join ( '\n' )
63
+ next ( null , this . message )
58
64
}
59
65
else {
60
66
options . files = options . _
@@ -71,65 +77,81 @@ cli.prototype.parse = function(argv, next) {
71
77
}
72
78
}
73
79
} . bind ( this ) )
74
- }
75
80
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 ) )
81
102
}
103
+ }
104
+
105
+ function createCheckPhantom ( _phantomPath ) {
106
+ var phantomPath = _phantomPath
107
+ , phantomVersion
82
108
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' ) {
89
114
try {
90
- options . phantomPath = which . sync ( 'phantomjs' )
115
+ var phantom = require ( 'phantomjs' )
116
+ phantomPath = phantom . path
91
117
} 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
+ }
107
135
}
108
136
}
109
137
}
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 ) )
129
138
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
+ }
130
157
}
131
-
132
-
133
- module . exports = function ( ) {
134
- return new cli ( )
135
- } ( )
0 commit comments