forked from lbl-srg/modelica-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
107 lines (92 loc) · 2.54 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
const fs = require('fs')
const pa = require('./lib/parser.js')
const ut = require('./lib/util.js')
const logger = require('winston')
const path = require('path')
const ArgumentParser = require('argparse').ArgumentParser
/// ///////////////////////////////////////
var parser = new ArgumentParser({
version: '0.0.1',
addHelp: true,
description: 'Modelica parser'
})
parser.addArgument(
[ '-o', '--output' ],
{
help: 'Specify output format.',
choices: ['html', 'raw-json', 'json', 'docx', 'svg'],
defaultValue: 'html'
}
)
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, 'cdl' is the default.",
choices: ['cdl', 'modelica'],
defaultValue: 'cdl'
}
)
parser.addArgument(
[ '-f', '--file' ],
{
help: 'Filename or packagename that contains the top-level Modelica class.',
required: true
}
)
parser.addArgument(
[ '-d', '--directory' ],
{
help: 'Specify output directory, with the default being the current.',
defaultValue: 'current'
}
)
parser.addArgument(
'--strict',
{
help: 'Exit with code 1 if there is any warning.',
defaultValue: 'false'
}
)
var 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.mode === 'modelica' && args.output === 'svg') {
throw new Error('svg output option has not been enabled in modelica mode.')
}
// Get mo files array
var moFiles = ut.getMoFiles(args.mode, args.file)
// Parse the json representation for moFiles
var json = pa.getJSON(moFiles, args.mode, args.output)
// Get the name array of output files
var outFile = ut.getOutFile(args.mode, args.file, args.output, args.directory, moFiles, json)
pa.exportJSON(json, outFile, args.output, args.mode, args.directory)
var schema
if (args.mode === 'cdl') {
schema = path.join(`${__dirname}`, 'schema-CDL.json')
} else {
schema = path.join(`${__dirname}`, 'schema-modelica.json')
}
setTimeout(function () { ut.jsonSchemaValidate(args.mode, outFile[0], args.output, schema) }, 100)
if (args.strict === 'true' && pa.warnCounter > 0) {
process.exit(1)
}