-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
jdl-application-definition.ts
68 lines (62 loc) · 2.53 KB
/
jdl-application-definition.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
import { jhipsterOptionTypes, jhipsterOptionValues, jhipsterQuotedOptionNames } from '../jhipster/application-options.js';
export type JDLApplicationOptionValue = string | number | boolean | undefined | never[] | Record<string, string>;
export type JDLApplicationOptionTypeValue = 'string' | 'integer' | 'boolean' | 'list' | 'quotedList';
export type JDLApplicationOptionType = { type: JDLApplicationOptionTypeValue };
export default class JDLApplicationDefinition {
optionValues: Record<string, JDLApplicationOptionValue> = jhipsterOptionValues;
optionTypes: Record<string, JDLApplicationOptionType> = jhipsterOptionTypes;
quotedOptionNames: string[] = jhipsterQuotedOptionNames;
/**
* Returns the option's type, one of string, boolean, list or integer.
* @param {String} optionName - the option's name.
* @returns {string} the option's type.
*/
getTypeForOption(optionName: string): JDLApplicationOptionTypeValue {
if (!optionName) {
throw new Error('A name has to be passed to get the option type.');
}
if (!this.optionTypes[optionName]) {
throw new Error(`Unrecognised application option name: ${optionName}.`);
}
return this.optionTypes[optionName].type;
}
/**
* Checks whether the option value exists for the passed option name.
* @param {String} name - the option name.
* @param {String|Boolean|Number} value - the option value.
* @returns {Boolean} whether the option value exists for the name.
*/
doesOptionValueExist(name, value) {
if (!this.doesOptionExist(name)) {
return false;
}
const values = this.optionValues[name];
if (typeof values !== 'object' || Array.isArray(values)) {
return true;
}
if (Array.isArray(value)) {
return value.every(val => values[val] != null);
}
return values[value] != null;
}
/**
* Checks whether the option's exists.
* @param {String} optionName - the option's name.
* @returns {Boolean} the option's existence.
*/
doesOptionExist(optionName) {
return !!optionName && optionName in this.optionTypes;
}
/**
* Checks whether the corresponding option has a value that should be quoted in the JDL, like the jhipsterVersion
* attribute.
* @param {String} optionName - the name of the option to check.
* @return {boolean} whether it should be quoted in the JDL.
*/
shouldTheValueBeQuoted(optionName) {
if (!optionName) {
throw new Error('An option name has to be passed to know whether it is quoted.');
}
return this.quotedOptionNames.includes(optionName);
}
}