/
normalizations.ts
172 lines (157 loc) · 4.49 KB
/
normalizations.ts
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
160
161
162
163
164
165
166
167
168
169
170
171
172
"use strict";
import { CommanderStatic } from "commander";
import * as path from "path";
import { INormalizedOptions, OutputType } from "../types";
const INPUT_EXTENSIONS = Object.freeze({
ast: "json",
json: "json",
msc: "mscgen",
mscgen: "mscgen",
mscin: "mscgen",
msgenny: "msgenny",
seq: "mscgen",
xu: "xu",
});
const OUTPUT_EXTENSIONS = Object.freeze({
ast: "json",
dot: "dot",
doxygen: "doxygen",
jpeg: "jpeg",
jpg: "jpeg",
json: "json",
msc: "mscgen",
mscgen: "mscgen",
mscin: "mscgen",
msgenny: "msgenny",
png: "png",
seq: "mscgen",
svg: "svg",
xu: "xu",
});
/**
* Given a filename in pString, returns what language is probably
* contained in that file, judging from the extension (the last dot
* in the string to end-of-string)
*
* When in doubt returns pDefault
*
* @param {string} pString - filename
* @param {object} pExtensionMap - a dictionary with
* extension : classification pairs
* @param {string} pDefault - the default to return when the extension
* does not occur in the extension map
* @return {string} - language. Possible values: LHS of the passed
* extension map.
*/
function classifyExtension(
pString: string | undefined,
pExtensionMap: any,
pDefault: string
): string {
if (!pString) {
return pDefault;
}
const lPos = pString.lastIndexOf(".");
if (lPos > -1) {
const lExt = pString.slice(lPos + 1);
if (pExtensionMap[lExt]) {
return pExtensionMap[lExt];
}
}
return pDefault;
}
function deriveOutputFromInput(
pInputFrom: string,
pOutputType: OutputType
): string | void {
if (!pInputFrom || "-" === pInputFrom) {
return;
}
return path
.join(
path.dirname(pInputFrom),
path.basename(pInputFrom, path.extname(pInputFrom))
)
.concat(".")
.concat(pOutputType);
}
function determineOutputTo(
pOutputTo: string | undefined,
pInputFrom: string,
pOutputType: OutputType
): string | void {
return Boolean(pOutputTo)
? pOutputTo
: deriveOutputFromInput(pInputFrom, pOutputType);
}
function determineInputType(
pInputType: string | undefined,
pInputFrom: string
): string {
if (pInputType) {
return pInputType === "ast" ? "json" : pInputType;
}
return classifyExtension(pInputFrom, INPUT_EXTENSIONS, "mscgen");
}
function determineOutputType(
pOutputType: string | undefined,
pOutputTo: string | undefined,
pParserOutput: string
): OutputType {
if (Boolean(pParserOutput)) {
return "json";
}
if (Boolean(pOutputType)) {
return pOutputType === "ast" ? "json" : (pOutputType as OutputType);
}
if (Boolean(pOutputTo)) {
return classifyExtension(pOutputTo, OUTPUT_EXTENSIONS, "svg") as OutputType;
}
return "svg";
}
/**
* transforms the given argument and options to a uniform format
*
* - guesses the input type when not given
* - guesses the output type when not given
* - gueses the filename to output to when not given
* - translates parserOutput to a regular output type
*
* @param {string} pArgument an argument (containing the filename to parse)
* @param {object} pOptions a commander options object
* @return {object} a commander options object with options 'normalized'
*/
export function normalize(
pArgument: string,
pOptions: CommanderStatic
): INormalizedOptions {
const lRetval = JSON.parse(JSON.stringify(pOptions));
lRetval.inputFrom = Boolean(pArgument) ? pArgument : pOptions.inputFrom;
lRetval.inputType = determineInputType(pOptions.inputType, lRetval.inputFrom);
lRetval.outputType = determineOutputType(
pOptions.outputType,
pOptions.outputTo,
pOptions.parserOutput
);
lRetval.outputTo = determineOutputTo(
pOptions.outputTo,
lRetval.inputFrom,
lRetval.outputType
);
lRetval.regularArcTextVerticalAlignment =
pOptions.verticalAlignment || "middle";
return lRetval;
}
/*
This file is part of mscgenjs-cli.
mscgenjs-cli is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
mscgen_js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with mscgenjs-cli. If not, see <http://www.gnu.org/licenses/>.
*/