-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.js
93 lines (81 loc) · 2.38 KB
/
parser.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
const toml = require('toml');
const fs = require('fs');
const path = require('path');
const fg = require('fast-glob');
const Mustache = require('mustache');
const commandLineArgs = require('command-line-args')
const options = commandLineArgs([
{ name: 'environment', alias: 'e', defaultValue: 'max', type: String},
{ name: 'output', alias: 'o', defaultValue: './output', type: String}
])
const dir = options.output;
const cce = options.environment;
const extensions = new Map([
['max', '.maxref.xml'],
['pd', '.html']
])
const sanitise = (data) => {
// Messages
let copy = data;
let messages = []
if (data.messages) {
for (const [k, v] of Object.entries(data.messages)) {
let message = v;
message.name = k;
messages.push(message)
}
}
// Attributes
let attributes = []
if (data.attributes) {
for (const [k, v] of Object.entries(data.attributes)) {
let attr = v;
attr.name = k;
attributes.push(attr)
}
}
let arguments = []
if (data.arguments) {
for (const [k, v] of Object.entries(data.arguments)) {
let arg = v;
arg.name = k;
arguments.push(arg)
}
}
// Inlets
let inlets = []
if (data.inlets) {
for (const [k, v] of Object.entries(data.inlets)) {
let inlet = v;
inlet.id = k;
inlets.push(inlet)
}
}
// Outlets
let outlets = []
if (data.outlets) {
for (const [k, v] of Object.entries(data.outlets)) {
let outlet = v;
outlet.id = k;
outlets.push(outlet)
}
}
copy.messages = messages
copy.attributes = attributes
copy.arguments = arguments
copy.inlets = inlets
copy.outlets = outlets
return copy
}
const configs = fg.sync([`${cce}/*.toml`]);
const template = fs.readFileSync(`./templates/${cce}.mustache`, 'utf8')
Mustache.escape = (text) => text; // escape maxref tags
if (!fs.existsSync(dir)) { fs.mkdirSync(dir) };
for (const config of configs) {
const data = fs.readFileSync(config);
const parsed = sanitise(toml.parse(data));
const ext = extensions.get(cce);
const output = path.join(dir, path.parse(config).name + ext);
fs.writeFileSync(output, Mustache.render(template, parsed))
console.log('Wrote ' + output);
}