#!/usr/bin/env node //================================================================================================= // // This script is used to regenerate the example visualizations // //================================================================================================= var fs = require('fs'), path = require('path'), child = require('child_process'); //------------------------------------------------------------------------------------------------- fs.readdirSync('examples') .filter(function(file) { return path.extname(file) === ".js" }) .map(visualize); //------------------------------------------------------------------------------------------------- function visualize(example) { var name = path.basename(example, '.js'), fsm = require('../examples/' + example), dot = fsm.visualize(), svg = dot2svg(dot), png = dot2png(dot); console.log('visualizing examples/' + example); fs.writeFileSync('examples/' + name + '.dot', dot); fs.writeFileSync('examples/' + name + '.svg', svg); fs.writeFileSync('examples/' + name + '.png', png, 'binary'); } //------------------------------------------------------------------------------------------------- function dot2svg(dot) { var result = child.spawnSync("dot", ["-Tsvg"], { input: dot }); if (result.error) dotError(result.error.errno); return result.stdout.toString(); } //------------------------------------------------------------------------------------------------- function dot2png(dot) { var result = child.spawnSync("dot", ["-Tpng"], { input: dot }); if (result.error) dotError(result.error.errno); return result.stdout; } //------------------------------------------------------------------------------------------------- function dotError(errno) { if (errno === 'ENOENT') throw new Error("dot program not found. Install graphviz (http://graphviz.org)") else throw new Error("unexpected error: " + errno) } //-------------------------------------------------------------------------------------------------