-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·93 lines (76 loc) · 1.78 KB
/
index.js
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
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
const moduleName = process.argv[2];
const exportVariable = process.argv[3];
if (!moduleName) {
console.log('Usage: xstate-plantuml <module> [<variable>]\n\n xstate-plantuml ./on-off\n xstate-plantuml ./stateMachines lightStates');
return;
}
let machine = require(moduleName);
if (exportVariable) {
machine = machine[exportVariable];
}
let indentation = 0;
function print(name, state) {
startCompound(name);
indentation++
initial(state.initial);
events(name, state);
entry(name, state);
exit(name, state);
const children = state.states || {};
Object.keys(children).forEach(name => print(name, children[name]));
indentation--
endCompound(name);
}
print("", machine);
function echo(val) {
console.log(Array(indentation*2).join(" ") + val);
}
function startCompound(name) {
if (name == '') { // root state
echo('left to right direction');
echo(`@startuml`);
}
else {
echo(`state ${name} {`);
}
}
function initial(name) {
if (name) {
echo(`[*] --> ${name}`);
}
}
function event(name, e, target) {
echo(`${name} --> ${target}: ${e}`);
}
function events(name, state) {
const on = state.on || {};
Object.keys(on).forEach(e => event(name, e, on[e]));
}
function action(name, when, action) {
if (Object.keys(action).length > 0) {
echo(`${name}: on${when} / ${action.type}`);
}
}
function actions(name, when, actions) {
if (Array.isArray(actions)) {
actions.forEach(a => action(name, when, a));
}
else {
action(name, when, actions);
}
}
function entry(name, state) {
actions(name, 'entry', state.onEntry || {});
}
function exit(name, state) {
actions(name, 'exit', state.onExit || {});
}
function endCompound(name) {
if (name == '') { // root state
echo(`@enduml`);
}
else {
echo(`}`);
}
}