forked from jakesgordon/javascript-state-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples
More file actions
executable file
·60 lines (47 loc) · 2.04 KB
/
examples
File metadata and controls
executable file
·60 lines (47 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/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)
}
//-------------------------------------------------------------------------------------------------