-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
89 lines (80 loc) · 3.26 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
module.exports = function convert(argv) {
// This one is from jest and is terrific to format object nicely
// but we used JSON.stringify in the end as we need JS-legit formatting here
// Kept here because I never remember the name of the module
// const prettyFormat = require('pretty-format');
const prettier = require("prettier");
const prettyFormat = require("pretty-format");
const fs = require('fs');
const {Command} = require('commander');
const {computeTransitionsAndStatesFromXmlString} = require('@brucou/utilities');
const {checkKinglyContracts} = require('./helpers');
const {implDoStr, implEveryStr, esmExports, cjsExports, cjsImports, esmImports, computeFileContents} = require('./template');
const program = new Command();
// Configure syntax, parse and run
program
.version('0.1.0')
.arguments('<file>')
.action(convertYedFile);
program.parse(process.argv);
// Conversion function
// DOC: We export two files: one cjs for node.js and js for browser esm consumption
// NTH: Configure exports through an option
// NTH: Handle several files at the same time
// NTH: Add an output option
function convertYedFile(_file) {
const file = _file.endsWith('.graphml') ? _file : `${_file}.graphml`;
// Read the file
try {
const yedString = fs.readFileSync(file, 'utf8');
// Convert the file
const {
states,
stateYed2KinglyMap,
events,
transitionsWithoutGuardsActions,
transitionsWithFakeGuardsActions,
getKinglyTransitions,
errors,
} = computeTransitionsAndStatesFromXmlString(yedString);
if (errors.length > 0) {
console.error(`${errors.length} error(s) found! See log.`);
errors.map(console.error);
throw new Error(errors)
}
else {
const kinglyErrors = checkKinglyContracts(states, events, transitionsWithFakeGuardsActions);
if (!kinglyErrors) {
throw new Error(`The input graph does not represent a valid Kingly machine! Cf. log.`)
}
const fileContents = computeFileContents(transitionsWithoutGuardsActions, events, states);
// Write the esm output file
const esmContents = [esmImports, fileContents, esmExports].join("\n\n");
try {
const prettyFileContents = prettier.format(esmContents, {semi: true, parser: "babel", printWidth: 120});
fs.writeFileSync(`${file}.fsm.js`, prettyFileContents)
//file written successfully
} catch (err) {
console.error(`error writing converted esm file.`);
console.error(err);
process.exit(1);
}
// Write the cjs output filee
const cjsContents = [cjsImports, fileContents, cjsExports].join("\n\n");
try {
const prettyFileContents = prettier.format(cjsContents, {semi: true, parser: "babel", printWidth: 120});
fs.writeFileSync(`${file}.fsm.cjs`, prettyFileContents)
//file written successfully
} catch (err) {
console.error(`Error writing converted esm file.`);
console.error(err);
process.exit(1);
}
}
} catch (err) {
// The actual message must have already be logged in the console
console.error(`Exiting due to errors`, err);
process.exit(1);
}
}
}