UNPKG

gen-jhipster

Version:

Spring Boot + Angular/React/Vue in one handy generator

77 lines (76 loc) 2.98 kB
/** * Copyright 2013-2024 the original author or authors from the JHipster project. * * This file is part of the JHipster project, see https://www.jhipster.tech/ * for more information. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { platform } from 'os'; import { normalizeLineEndings } from './contents.js'; const isWin32 = platform() === 'win32'; /** * TODO move to utils when converted to typescripts * Converts multiples EditFileCallback callbacks into one. */ export function joinCallbacks(...callbacks) { return function (content, filePath) { if (isWin32 && /\r\n/.exec(content)) { const removeSlashRSlashN = ct => normalizeLineEndings(ct, '\n'); const addSlashRSlashN = ct => normalizeLineEndings(ct, '\r\n'); callbacks = [removeSlashRSlashN, ...callbacks, addSlashRSlashN]; } 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; }