gen-jhipster
Version:
VHipster - Spring Boot + Angular/React/Vue in one handy generator
53 lines (52 loc) • 2.25 kB
JavaScript
import { applyDerivedProperty } from "../../../lib/utils/derived-property.js";
export function loadConfig(configsDef, { application, config }) {
if (configsDef) {
for (const [name, def] of Object.entries(configsDef)) {
let value = application[name];
if (value === undefined || value === null) {
let source = config;
if (!source) {
switch (def.scope) {
case 'context': {
source = this.context;
break;
}
case 'blueprint': {
// TODO Convert type to BaseGenerator
source = this.blueprintStorage.getAll();
break;
}
case 'storage':
case undefined: {
source = this.jhipsterConfigWithDefaults;
break;
}
}
}
if (source) {
value = application[name] = source[name] ?? undefined;
if (value === undefined && def.default !== undefined) {
application[name] = typeof def.default === 'function' ? def.default.call(this, source) : def.default;
}
}
}
}
}
}
export const loadDerivedConfig = (configsDef, { application }) => {
for (const [name, def] of Object.entries(configsDef)) {
if (['storage', 'blueprint', 'context'].includes(def.scope) && def.choices) {
applyDerivedProperty(application, name, def.choices, { addAny: true });
}
}
};
export const loadConfigDefaults = (configsDef, { context, scopes }) => {
for (const [name, def] of Object.entries(configsDef)) {
if (context[name] === undefined) {
const defaultValue = def.default ?? def.cli?.default;
if (scopes.includes(def.scope) && defaultValue !== undefined) {
context[name] = typeof defaultValue === 'function' ? defaultValue.call(this, context) : defaultValue;
}
}
}
};