generator-begcode
Version:
Spring Boot + Angular/React/Vue in one handy generator
60 lines (59 loc) • 2.77 kB
JavaScript
import { applyDerivedProperty } from '../utils/derived-property.js';
const filteredScopeEntries = (commandsConfigs, scopes) => Object.entries(commandsConfigs).filter(([_key, def]) => scopes.includes(def.scope));
function loadConfigIntoContext(options) {
const { applyDefaults, source, templatesContext, commandsConfigs, scopes = ['storage', 'blueprint'] } = options;
if (commandsConfigs) {
filteredScopeEntries(commandsConfigs, scopes).forEach(([key, def]) => {
let configuredValue = source?.[key] ?? templatesContext[key];
if (applyDefaults && def.default !== undefined && (configuredValue === undefined || configuredValue === null)) {
if (typeof def.default === 'function') {
configuredValue = def.default.call(this, source);
}
else {
configuredValue = def.default;
}
}
templatesContext[key] = configuredValue;
if (def.choices) {
applyDerivedProperty(templatesContext, key, def.choices, { addAny: true });
if (key === 'serviceDiscoveryType') {
templatesContext.serviceDiscovery = templatesContext.serviceDiscoveryType;
applyDerivedProperty(templatesContext, 'serviceDiscovery', def.choices, { addAny: true });
}
}
});
}
}
export function loadCommandConfigsIntoApplication(options) {
const { application, commandsConfigs, source } = options;
loadConfigIntoContext.call(this, {
source,
templatesContext: application,
commandsConfigs,
scopes: ['storage', 'blueprint'],
});
}
export function loadCommandConfigsIntoGenerator(options) {
const { commandsConfigs } = options;
loadConfigIntoContext.call(this, {
source: this.options,
templatesContext: this,
commandsConfigs,
scopes: ['storage', 'blueprint'],
});
}
export function loadCommandConfigsKeysIntoTemplatesContext(options) {
const { templatesContext, commandsConfigs, scopes = ['storage', 'blueprint'] } = options;
if (commandsConfigs) {
filteredScopeEntries(commandsConfigs, scopes).forEach(([key, def]) => {
templatesContext[key] = templatesContext[key] ?? undefined;
if (def.choices) {
applyDerivedProperty(templatesContext, key, def.choices, { addAny: true });
if (key === 'serviceDiscoveryType') {
templatesContext.serviceDiscovery = templatesContext.serviceDiscoveryType;
applyDerivedProperty(templatesContext, 'serviceDiscovery', def.choices, { addAny: true });
}
}
});
}
}