-
Notifications
You must be signed in to change notification settings - Fork 17
/
app.js
159 lines (148 loc) · 4.76 KB
/
app.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const fs = require('fs')
const pa = require('./lib/parser.js')
const ut = require('./lib/util.js')
const se = require('./lib/semanticExtractor.js')
const ce = require('./lib/cxfExtractor.js')
const logger = require('winston')
const path = require('path')
const ArgumentParser = require('argparse').ArgumentParser
/// ///////////////////////////////////////
const parser = new ArgumentParser({
version: '0.0.1',
addHelp: true,
description: 'Modelica parser'
})
parser.addArgument(
['-o', '--output'],
{
help: 'Specify output format.',
choices: ['raw-json', 'json', 'modelica', 'semantic', 'cxf'],
defaultValue: 'json'
}
)
parser.addArgument(
['-l', '--log'],
{
help: "Logging level, 'info' is the default.",
choices: ['error', 'warn', 'info', 'verbose', 'debug'],
defaultValue: 'info'
}
)
parser.addArgument(
['-m', '--mode'],
{
help: "Parsing mode, CDL model or a package of the Modelica Buildings library, 'modelica' is the default.",
choices: ['cdl', 'modelica'],
defaultValue: 'modelica'
}
)
parser.addArgument(
['-f', '--file'],
{
help: "Filename or packagename that contains the top-level Modelica class, or a json file when the output format is 'modelica'.",
required: true
}
)
parser.addArgument(
['-d', '--directory'],
{
help: 'Specify output directory, with the default being the current.',
defaultValue: 'current'
}
)
parser.addArgument(
['-p', '--prettyPrint'],
{
help: 'Pretty print JSON output. The -o/--output should be raw-json/json/cxf.',
action: 'storeTrue'
}
)
parser.addArgument(
['--elementary'],
{
help: 'If this flag is present, generate CXF of elementary blocks in addition to composite blocks. -o/--output should be cxf.',
action: 'storeTrue'
}
)
parser.addArgument(
['--cxfCore'],
{
help: 'If this flag is present, generate CXF-core.jsonld. -o/--output should be cxf, -f/--file should be path/to/CDL and --elementary flag must be used.',
action: 'storeTrue'
}
)
const args = parser.parseArgs()
const logFile = 'modelica-json.log'
try {
fs.unlinkSync(logFile)
} catch (ex) {}
logger.configure({
transports: [
new logger.transports.Console(),
new logger.transports.File({ filename: logFile })
],
handleExceptions: true,
humanReadableUnhandledException: true
})
logger.cli()
logger.level = args.log
if (args.output === 'modelica' && !args.file.endsWith('.json')) {
throw new Error('Modelica output requires the input file (-f) to be a json file.')
}
if (args.output !== 'modelica' && args.file.endsWith('.json')) {
throw new Error("The json input file required only when the output format (-o) is 'modelica'.")
}
if (args.output === 'modelica') {
pa.convertToModelica(args.file, args.directory, false)
} else {
// Get mo files array
if (args.elementary || args.cxfCore) {
if (!args.output === 'cxf') {
throw new Error('In order to generate CXF (jsonld) of elementary blocks, -o/--output should be cxf.')
}
}
if (args.cxfCore) {
if (!args.file.endsWith('CDL') && !args.file.endsWith('cdl')) {
throw new Error('In order to generate CXF-core.jsonld containing all elementary blocks, -f/--file should be path/to/CDL.')
}
if (!args.elementary) {
throw new Error('In order to generate CXF-core.jsonld containing all elementary blocks, --elementary flag must be used.')
}
}
const completedJsonGeneration = new Promise(
function (resolve, reject) {
const moFiles = ut.getMoFiles(args.file)
// Parse the json representation for moFiles
pa.getJsons(moFiles, args.mode, args.output, args.directory, args.prettyPrint, args.elementary, args.cxfCore)
resolve(0)
}
)
completedJsonGeneration.then(function () {
if (args.output === 'semantic') {
se.getSemanticInformation(args.file, args.directory)
}
if (args.output === 'cxf' && args.cxfCore && args.elementary) {
ce.getCxfCore(args.file, args.directory, args.prettyPrint)
}
})
}
if (args.output === 'json') {
let schema
if (args.mode === 'cdl') {
schema = path.join(`${__dirname}`, 'schema-cdl.json')
} else {
schema = path.join(`${__dirname}`, 'schema-modelica.json')
}
const jsonDir = (args.directory === 'current') ? process.cwd() : args.directory
let jsonFiles = ut.findFilesInDir(path.join(jsonDir, 'json'), '.json')
// exclude CDL folder and possibly Modelica folder
const pathSep = path.sep
const cdlPath = path.join(pathSep, 'CDL', pathSep)
const modelicaPath = path.join('Modelica', pathSep)
jsonFiles = jsonFiles.filter(obj => !(obj.includes(cdlPath) || obj.includes(modelicaPath)))
// validate json schema
for (let i = 0; i < jsonFiles.length; i++) {
const eachFile = jsonFiles[i]
setTimeout(function () { ut.jsonSchemaValidation(args.mode, eachFile, 'json', schema) }, 100)
}
}