Skip to content

Commit

Permalink
migrate puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
wreulicke committed Aug 26, 2017
1 parent 89179ff commit c18c8aa
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 347 deletions.
79 changes: 6 additions & 73 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
var fs = require('fs')
var exec = require('child_process').exec
var chalk = require('chalk')
var which = require('which')
var parseArgs = require('minimist')
var semver = require('semver')
var path = require('path')

var PHANTOM_VERSION = '^2.1.0'

var info = chalk.blue.bold

module.exports = (function () {
Expand All @@ -23,7 +18,6 @@ function Cli (options) {
outputSuffix: 'O',
svg: 's',
verbose: 'v',
phantomPath: 'e',
sequenceConfig: 'c',
ganttConfig: 'g',
css: 't',
Expand All @@ -46,7 +40,6 @@ function Cli (options) {
' -p --png If SVG was selected, and you also want PNG, set this flag',
' -o --outputDir Directory to save files, will be created automatically, defaults to `cwd`',
" -O --outputSuffix Suffix to output filenames in front of '.svg' or '.png', defaults to ''",
' -e --phantomPath Specify the path to the phantomjs executable',
' -t --css Specify the path to a CSS file to be included when processing output',
' -c --sequenceConfig Specify the path to the file with the configuration to be applied in the sequence diagram',
' -g --ganttConfig Specify the path to the file with the configuration to be applied in the gantt diagram',
Expand Down Expand Up @@ -78,7 +71,7 @@ Cli.prototype.parse = function (argv, next) {
}

// ensure that parameter-expecting options have parameters
;['outputDir', 'outputSuffix', 'phantomPath', 'sequenceConfig', 'ganttConfig', 'css'].forEach(function (i) {
;['outputDir', 'outputSuffix', 'sequenceConfig', 'ganttConfig', 'css'].forEach(function (i) {
if (typeof options[i] !== 'undefined') {
if (typeof options[i] !== 'string' || options[i].length < 1) {
this.errors.push(new Error(i + ' expects a value.'))
Expand Down Expand Up @@ -127,70 +120,10 @@ Cli.prototype.parse = function (argv, next) {
if (!options.width) {
options.width = 1200
}

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

return function checkPhantom (_next) {
var next = _next || function () { }
var 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
}
}
}
}

// 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)
}
})
next(
this.errors.length > 0 ? this.errors : null
, this.message
, options
)
}
}
11 changes: 2 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
var path = require('path')
var spawn = require('child_process').spawn

var mkdirp = require('mkdirp')

var phantomscript = path.join(__dirname, 'phantomscript.js')
var processor = require('./processor')

module.exports = { process: processMermaid }

Expand All @@ -13,7 +11,6 @@ function processMermaid (files, _options, _next) {
var outputSuffix = options.outputSuffix || ''
var next = _next || function () { }
var phantomArgs = [
phantomscript,
outputDir,
options.png,
options.svg,
Expand All @@ -33,11 +30,7 @@ function processMermaid (files, _options, _next) {
if (err) {
throw err
}
var phantom = spawn(options.phantomPath, phantomArgs)

phantom.on('exit', next)

phantom.stderr.pipe(process.stderr)
phantom.stdout.pipe(process.stdout)
processor(...(phantomArgs)).then(() => next()).catch(e => next(e))
})
}
231 changes: 0 additions & 231 deletions lib/phantomscript.js

This file was deleted.

Loading

0 comments on commit c18c8aa

Please sign in to comment.