This repository has been archived by the owner on Jul 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_state_diagram.js
executable file
·86 lines (75 loc) · 2.39 KB
/
generate_state_diagram.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
#!/usr/bin/env node
const exec = require('child_process').exec;
const fs = require('fs');
const minimist = require('minimist');
const parseCCD = require('./util/parse_ccd');
const options = {
alias: {
o: 'output',
s: 'ignoredStates',
e: 'ignoredEvents',
r: 'roles',
u: 'hideUnauthorised',
h: 'help'
},
default: {
o: 'state_diagram',
u: false
}
};
const argv = minimist(process.argv.slice(2), options);
function printHelp() {
console.log('Usage ./generate_state_diagram.js [path to ccd def] [options]');
console.log('');
console.log('where options include:');
console.log(' -o, --output=... file to output state diagram to');
console.log(' -s, --ignoredStates=... comma seperated list of states to ignore, can be a regex that matches the whole state name');
console.log(' -e, --ignoredEvents=... comma seperated list of events to ignore, can be a regex that matches the whole event name');
console.log(' -r, --roles=... comma seperated list of roles to build the diagram for');
console.log(' -u, --hideUnauthorised hide any events that a user is unauthorised to run or see');
console.log(' -h, --help to display this help message');
}
function splitInput(input) {
return input ? input.split(',') : [];
}
const help = argv.h;
if (help) {
printHelp();
process.exit(0);
}
if (argv._.length !== 1) {
console.log('Invalid usage');
printHelp();
process.exit(1);
}
const definitionLocation = argv._[0];
const outputFile = argv.o;
const ignoredStates = splitInput(argv.s);
const ignoredEvents = splitInput(argv.e);
const roles = splitInput(argv.r);
const hideUnauthorised = argv.u;
console.log(`Output file [${outputFile}]`);
console.log(`Ignored states [${ignoredStates}]`);
console.log(`Ignored events [${ignoredEvents}]`);
console.log(`Roles [${roles}]`);
console.log(`Hide unauthorised [${hideUnauthorised}]`);
const stateDiagram = parseCCD(
definitionLocation,
ignoredStates,
ignoredEvents,
roles,
hideUnauthorised
);
const outputFileName = `${outputFile}.txt`;
fs.writeFileSync(outputFileName, stateDiagram);
exec(`java -jar ${__dirname}/lib/plantuml.jar ${outputFileName}`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
fs.unlinkSync(`./${outputFileName}`);
});