-
Notifications
You must be signed in to change notification settings - Fork 952
Expand file tree
/
Copy pathexamples
More file actions
executable file
·83 lines (68 loc) · 2.63 KB
/
examples
File metadata and controls
executable file
·83 lines (68 loc) · 2.63 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
//=================================================================================================
//
// This script is used to regenerate the example visualizations
//
// It will scan all the .js files in the 'examples' folder and generate a
// * .dot - file with dot directives for graphviz
// * .svg - svg graphics representation
// * .png - png graphics representation
//
// If provided with an argument, it will only generate output for the specified
// argument.
//
//=================================================================================================
var fs = require('fs'),
path = require('path'),
child = require('child_process');
//-------------------------------------------------------------------------------------------------
process.chdir(path.dirname(process.argv[1]));
if (process.argv.length > 2) {
var fn = process.argv[2],
fnfull = '../examples/' + fn;
if (!fs.existsSync(fnfull)) {
console.log('Error - file: ' + fnfull + ' does not exist');
}
else {
visualize(fn);
}
}
else {
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)
}
//-------------------------------------------------------------------------------------------------