gen-jhipster
Version:
VHipster - Spring Boot + Angular/React/Vue in one handy generator
51 lines (50 loc) • 1.82 kB
JavaScript
/**
* TODO move to utils when converted to typescripts
* Converts multiples EditFileCallback callbacks into one.
*/
export function joinCallbacks(...callbacks) {
return function (content, filePath) {
for (const callback of callbacks) {
content = callback.call(this, content, filePath);
}
return content;
};
}
/**
* Utility function add condition to every block in addition to the already existing condition.
*/
export function addSectionsCondition(files, commonCondition) {
return Object.fromEntries(Object.entries(files).map(([sectionName, sectionValue]) => {
sectionValue = sectionValue.map(block => {
const { condition } = block;
let newCondition = commonCondition;
if (typeof condition === 'function') {
newCondition = (...args) => commonCondition(...args) && condition(...args);
}
else if (condition !== undefined) {
newCondition = (...args) => commonCondition(...args) && condition;
}
block = {
...block,
condition: newCondition,
};
return block;
});
return [sectionName, sectionValue];
}));
}
/**
* Utility function to merge sections (jhipster files structure)
* Merging { foo: [blocks1], bar: [block2]} and { foo: [blocks3], bar: [block4]}
* Results in { foo: [blocks1, block3], bar: [block2, block4]}
*/
export function mergeSections(...allFiles) {
const generated = {};
for (const files of allFiles) {
for (const [sectionName, sectionValue] of Object.entries(files)) {
generated[sectionName] = generated[sectionName] || [];
generated[sectionName].push(...sectionValue);
}
}
return generated;
}