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

Adds CSS option to the CLI #116

Merged
merged 1 commit into from
Feb 3, 2015
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function cli(options) {
, verbose: 'v'
, phantomPath: 'e'
, sequenceConfig: 'c'
, css: 't'
}
, 'boolean': ['help', 'png', 'svg']
, 'string': ['outputDir']
Expand All @@ -42,6 +43,7 @@ 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`"
, " -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"
, " -h --help Show this message"
, " -v --verbose Show logging"
Expand Down Expand Up @@ -72,7 +74,7 @@ cli.prototype.parse = function(argv, next) {
}

// ensure that parameter-expecting options have parameters
;['outputDir', 'phantomPath', 'sequenceConfig'].forEach(function(i) {
;['outputDir', 'phantomPath', 'sequenceConfig', '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 All @@ -92,6 +94,14 @@ cli.prototype.parse = function(argv, next) {
options.sequenceConfig = checkConfig(options.sequenceConfig)
}

if (options.css) {
try {
options.css = fs.readFileSync(options.css, 'utf8')
} catch (err) {
this.errors.push(err)
}
}

this.checkPhantom = createCheckPhantom(options.phantomPath)

this.checkPhantom(function(err, path) {
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function processMermaid(files, _options, _next) {
, outputDir
, options.png
, options.svg
, options.css || ''
, options.sequenceConfig
, options.verbose
]
Expand Down
9 changes: 5 additions & 4 deletions lib/phantomscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@ var system = require('system')
, webpage = require('webpage')

var page = webpage.create()
, files = phantom.args.slice(5, phantom.args.length)
, files = phantom.args.slice(6, phantom.args.length)
, options = {
outputDir: phantom.args[0]
, png: phantom.args[1] === 'true' ? true : false
, svg: phantom.args[2] === 'true' ? true : false
, sequenceConfig: phantom.args[3]
, verbose: phantom.args[4] === 'true' ? true : false
, css: phantom.args[3] !== '' ? phantom.args[3] : '* { margin: 0; padding: 0; }'
, sequenceConfig: phantom.args[4]
, verbose: phantom.args[5] === 'true' ? true : false
}
, log = logger(options.verbose)

page.content = [
'<html>'
, '<head>'
, '<style type="text/css">'
, '* { margin: 0; padding: 0; }'
, options.css
, '</style>'
, '</head>'
, '<body>'
Expand Down
29 changes: 29 additions & 0 deletions test/cli_test-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ test('output of multiple svg', function(t) {
})
})

test('output including CSS', function(t) {
t.plan(5)

var expected = ['test.mermaid.png']
, opt = clone(singleFile)
, filename
, one
, two

opt.png = true

mermaid.process(opt.files, opt, function(code) {
t.equal(code, 0, 'has clean exit code')
filename = path.join(opt.outputDir, path.basename(expected[0]))
one = fs.statSync(filename)

opt.css = fs.readFileSync('test/fixtures/test.css', 'utf8')

mermaid.process(opt.files, opt, function(code) {
t.equal(code, 0, 'has clean exit code')
two = fs.statSync(filename)

t.notEqual(one.size, two.size)

verifyFiles(expected, opt.outputDir, t)
})
})
})

function verifyFiles(expected, dir, t) {
async.each(
expected
Expand Down
12 changes: 12 additions & 0 deletions test/cli_test-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ test('setting an output directory succeeds', function(t) {
})
})

test('setting a css source file succeeds', function(t) {
t.plan(1)

var cli = require(cliPath)
, argv = ['-t', 'test/fixtures/test.css']

cli.parse(argv, function(err, msg, opt) {
t.ok(opt.css, 'css file is populated')
t.end()
})
})

test('setting an output directory incorrectly causes an error', function(t) {
t.plan(1)

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: #f00;
}