fixed css, sequenceConfig, and ganttConfig being sent as buffer to phantomjs

made verbose cli argument a boolean
This commit is contained in:
Ben Page 2016-09-28 13:56:23 -05:00
parent aa6e15e3ac
commit 0c62c08abd
4 changed files with 50 additions and 66 deletions

View File

@ -29,7 +29,7 @@ function cli(options) {
, css: 't' , css: 't'
, width: 'w' , width: 'w'
} }
, 'boolean': ['help', 'png', 'svg'] , 'boolean': ['help', 'png', 'svg', 'verbose']
, 'string': ['outputDir'] , 'string': ['outputDir']
} }
@ -94,25 +94,38 @@ cli.prototype.parse = function(argv, next) {
else { else {
options.png = true options.png = true
} }
if (options.sequenceConfig) { if (options.sequenceConfig) {
options.sequenceConfig = checkConfig(options.sequenceConfig) try {
fs.accessSync(options.sequenceConfig, fs.R_OK)
} catch (err) {
this.errors.push(err)
}
} else {
options.sequenceConfig = null
} }
if (options.ganttConfig) { if (options.ganttConfig) {
options.ganttConfig = checkConfig(options.ganttConfig) try {
fs.accessSync(options.ganttConfig, fs.R_OK)
} catch (err) {
this.errors.push(err)
}
} else {
options.ganttConfig = null
} }
if (options.css) { if (options.css) {
try { try {
options.css = fs.readFileSync(options.css, 'utf8') fs.accessSync(options.css, fs.R_OK)
} catch (err) { } catch (err) {
this.errors.push(err) this.errors.push(err)
} }
} else { } else {
options.css = fs.readFileSync(path.join(__dirname, '..', 'dist', 'mermaid.css')) options.css = path.join(__dirname, '..', 'dist', 'mermaid.css')
} }
// set svg/png flags appropriately // set svg/png flags appropriately
@ -136,17 +149,6 @@ cli.prototype.parse = function(argv, next) {
} }
} }
function checkConfig(configPath) {
try {
var text = fs.readFileSync(configPath, 'utf8');
JSON.parse(text)
return text
} catch (e) {
console.log(e);
return null;
}
}
function createCheckPhantom(_phantomPath) { function createCheckPhantom(_phantomPath) {
var phantomPath = _phantomPath var phantomPath = _phantomPath
, phantomVersion , phantomVersion

View File

@ -18,7 +18,7 @@ function processMermaid(files, _options, _next) {
, outputDir , outputDir
, options.png , options.png
, options.svg , options.svg
, options.css || '' , options.css
, options.sequenceConfig , options.sequenceConfig
, options.ganttConfig , options.ganttConfig
, options.verbose , options.verbose

View File

@ -28,7 +28,6 @@ var system = require('system')
, fs = require('fs') , fs = require('fs')
, webpage = require('webpage') , webpage = require('webpage')
var page = webpage.create() var page = webpage.create()
, files = system.args.slice(9, system.args.length) , files = system.args.slice(9, system.args.length)
, width = system.args[8] , width = system.args[8]
@ -40,21 +39,15 @@ var options = {
outputDir: system.args[1] outputDir: system.args[1]
, png: system.args[2] === 'true' ? true : false , png: system.args[2] === 'true' ? true : false
, svg: system.args[3] === 'true' ? true : false , svg: system.args[3] === 'true' ? true : false
, css: system.args[4] !== '' ? system.args[4] : '* { margin: 0; padding: 0; }' , css: fs.read(system.args[4])
, sequenceConfig: system.args[5] , sequenceConfig: system.args[5] !== 'null' ? JSON.parse(fs.read(system.args[5])) : '{}'
, ganttConfig: system.args[6] , ganttConfig: system.args[6] !== 'null' ? fs.read(system.args[6]) : '{}'
, verbose: system.args[7] === 'true' ? true : false , verbose: system.args[7] === 'true' ? true : false
, width: width , width: width
} }
, log = logger(options.verbose) , log = logger(options.verbose)
// If no css is suuplied make sure a fixed witdth is given to the gant renderer options.sequenceConfig.useMaxWidth = false;
if(system.args[3] !== ''){
if(typeof options.ganttConfig === 'undefined'){
options.ganttConfig = {};
}
options.ganttConfig.useWidth = 1200;
}
page.content = [ page.content = [
'<html>' '<html>'
@ -218,7 +211,7 @@ function resolveForeignObjects(element) {
function executeInPage(data) { function executeInPage(data) {
var xmlSerializer = new XMLSerializer() var xmlSerializer = new XMLSerializer()
, contents = data.contents , contents = data.contents
, sequenceConfig = data.sequenceConfig , sequenceConfig = JSON.stringify(data.sequenceConfig)
, ganttConfig = data.ganttConfig , ganttConfig = data.ganttConfig
, toRemove , toRemove
, el , el
@ -229,7 +222,7 @@ function executeInPage(data) {
, width , width
, height , height
, confWidth = data.confWidth , confWidth = data.confWidth
toRemove = document.getElementsByClassName('mermaid') toRemove = document.getElementsByClassName('mermaid')
if (toRemove && toRemove.length) { if (toRemove && toRemove.length) {
for (var i = 0, len = toRemove.length; i < len; i++) { for (var i = 0, len = toRemove.length; i < len; i++) {
@ -242,41 +235,24 @@ function executeInPage(data) {
elContent = document.createTextNode(contents) elContent = document.createTextNode(contents)
el.appendChild(elContent) el.appendChild(elContent)
//el.innerText = '<b>hello</b>\uD800' //contents; //el.innerText = '<b>hello</b>\uD800' //contents;
document.body.appendChild(el) document.body.appendChild(el)
mermaid.initialize({ var config = {
sequenceDiagram:{useMaxWidth:false}, sequenceDiagram: sequenceConfig,
flowchart:{useMaxWidth:false}, flowchart: {useMaxWidth: false},
logLevel:1 logLevel: 1
}); };
mermaid.initialize(config);
//console.log('after initialize',sequenceConfig); //console.log('after initialize',sequenceConfig);
if(typeof sequenceConfig !== undefined && sequenceConfig !== 'undefined'){ sc = document.createElement("script")
//sc = document.createElement("script") scContent = document.createTextNode('mermaid.ganttConfig = ' + ganttConfig + ';')
//scContent = document.createTextNode('mermaid.sequenceConfig = JSON.parse(' + JSON.stringify(sequenceConfig) + ');') sc.appendChild(scContent)
//sc.appendChild(scContent)
//document.body.appendChild(sc) document.body.appendChild(sc)
mermaid.initialize({
sequenceDiagram:JSON.parse(sequenceConfig)
});
}
//console.log('after initialize 2');
if(typeof ganttConfig !== undefined && ganttConfig !== 'undefined'){
sc = document.createElement("script")
scContent = document.createTextNode('mermaid.ganttConfig = JSON.parse(' + JSON.stringify(ganttConfig) + ');')
sc.appendChild(scContent)
document.body.appendChild(sc)
}else{
sc = document.createElement("script")
scContent = document.createTextNode('mermaid.ganttConfig = {useWidth:1200};')
sc.appendChild(scContent)
document.body.appendChild(sc)
}
mermaid.init(); mermaid.init();

View File

@ -24,12 +24,18 @@ var singleFile = {
, outputDir: path.join(process.cwd(),'test/tmp2/') , outputDir: path.join(process.cwd(),'test/tmp2/')
, phantomPath: path.join(process.cwd(),phantomCmd) , phantomPath: path.join(process.cwd(),phantomCmd)
, width : 1200 , width : 1200
, css: path.join(__dirname, '..', 'dist', 'mermaid.css')
, sequenceConfig: null
, ganttConfig: null
} }
, multiFile = { , multiFile = {
files: [path.join('test','fixtures','test.mermaid'), path.join('test','fixtures','test2.mermaid')] files: [path.join('test','fixtures','test.mermaid'), path.join('test','fixtures','test2.mermaid')]
, outputDir: 'test/tmp2/' , outputDir: 'test/tmp2/'
, phantomPath: path.join(process.cwd(),phantomCmd) , phantomPath: path.join(process.cwd(),phantomCmd)
, width : 1200 , width : 1200
, css: path.join(__dirname, '..', 'dist', 'mermaid.css')
, sequenceConfig: null
, ganttConfig: null
} }
@ -40,7 +46,7 @@ test('output of single png', function(t) {
opt = clone(singleFile) opt = clone(singleFile)
opt.png = true opt.png = true
mermaid.process(opt.files, opt, function(code) { mermaid.process(opt.files, opt, function(code) {
t.equal(code, 0, 'has clean exit code') t.equal(code, 0, 'has clean exit code')
@ -113,7 +119,7 @@ test('output including CSS', function(t) {
one = fs.statSync(filename) one = fs.statSync(filename)
//console.log('one: '+opt.files[0]); //console.log('one: '+opt.files[0]);
opt2.css = fs.readFileSync(path.join('test','fixtures','test.css'), 'utf8') opt2.css = path.join('test','fixtures','test.css')
//console.log(opt2.css); //console.log(opt2.css);
console.log('Generating #2'); console.log('Generating #2');