generator-jhipster
Version:
Spring Boot + Angular/React/Vue in one handy generator
48 lines (47 loc) • 2.27 kB
JavaScript
import { buildMutateDataForProperty } from "../utils/derived-property.js";
export const getCommandDerivedPropertyMutations = (configs, options = {}) => {
const scopes = options.scopes ?? ['storage', 'blueprint', 'context'];
const scopeConfigs = Object.entries(configs).filter(([_key, def]) => scopes.includes(def.scope));
const mutations = {
__override__: false,
};
for (const [key, def] of scopeConfigs) {
if (def.choices) {
const array = def.internal?.type === Array;
const anyCheck = array ? undefined : (value, choices) => (typeof value !== 'string' || value === 'no' ? false : choices.includes(value));
const choiceValues = def.choices.map(choice => (typeof choice === 'object' ? choice.value : choice));
Object.assign(mutations, buildMutateDataForProperty(key, choiceValues, { array, anyCheck }));
if (def.internal?.alias) {
Object.assign(mutations, { [def.internal.alias]: (context) => context[key] }, buildMutateDataForProperty(key, choiceValues, { array, anyCheck, prefix: def.internal.alias }));
}
}
}
return mutations;
};
export const getCommandDefaultMutations = (configs, options = {}) => {
const scopes = options.scopes ?? ['storage', 'blueprint', 'context'];
const scopeConfigs = Object.entries(configs).filter(([_key, def]) => scopes.includes(def.scope));
const mutations = {
__override__: false,
};
for (const [key, def] of scopeConfigs) {
mutations[key] = (context, { delayMarker }) => {
if (delayMarker) {
return delayMarker;
}
return typeof def.default === 'function' ? def.default(context) : def.default;
};
}
return mutations;
};
export const getCommandBlueprintLoadingMutations = (blueprintGenerator, configs) => {
const blueprintScopedConfigs = Object.entries(configs).filter(([_key, def]) => def.scope === 'blueprint');
const getValue = (key) => blueprintGenerator.blueprintStorage.get(key) ?? undefined;
const mutations = {
__override__: false,
};
for (const [key] of blueprintScopedConfigs) {
mutations[key] = () => getValue(key);
}
return mutations;
};