UNPKG

gen-jhipster

Version:

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

84 lines (83 loc) 4.09 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 { SERVER_MAIN_RES_DIR, SERVER_MAIN_SRC_DIR, SERVER_TEST_RES_DIR, SERVER_TEST_SRC_DIR, SERVER_MAIN_PROTO_DIR, } from '../../generator-constants.js'; export const replaceEntityFilePathVariables = (data, filePath) => { filePath = filePath ?.replace(/_package_/, data.packageFolder) ?.replace(/_entityPackage_/, data.entityJavaPackageFolder) ?.replace(/_mainClass_/, data.mainClass) ?.replace(/_persistClass_/, data.persistClass) ?.replace(/_entityClass_/, data.entityClass) ?.replace(/_dtoClass_/, data.dtoClass) ?.replace(/_entityUnderscoredName_/, data.entityUnderscoredName); return filePath?.includes('.jhi.') ? filePath : filePath?.replace(/_\w*/, ''); }; /** * Move the template to `javaPackageSrcDir` (defaults to`src/main/java/${packageFolder}/${filePath}`). * Removes trailing specifiers. */ export const moveToJavaPackageSrcDir = (data, filePath) => `${data.javaPackageSrcDir}${replaceEntityFilePathVariables(data, filePath) ?? ''}`; /** * Move the template to `javaPackageTestDir` (defaults to`src/main/java/${packageFolder}/${filePath}`). * Removes trailing specifiers. */ export const moveToJavaPackageTestDir = (data, filePath) => `${data.javaPackageTestDir}${replaceEntityFilePathVariables(data, filePath) ?? ''}`; export const moveToSrcMainResourcesDir = (data, filePath) => `${data.srcMainResources}${replaceEntityFilePathVariables(data, filePath) ?? ''}`; export const moveToSrcMainProtoDir = (data, filePath) => `${SERVER_MAIN_PROTO_DIR}${replaceEntityFilePathVariables(data, filePath) ?? ''}`; export const moveToSrcMainProtoUtilDir = (data, filePath) => `${SERVER_MAIN_PROTO_DIR}util/${filePath.substring(1)}`; export function javaMainPackageTemplatesBlock(blockOrRelativePath = '') { return javaBlock({ srcPath: `${SERVER_MAIN_SRC_DIR}_package_/`, destProperty: 'javaPackageSrcDir', blockOrRelativePath, }); } export function javaMainResourceTemplatesBlock(blockOrRelativePath = '') { return javaBlock({ srcPath: SERVER_MAIN_RES_DIR, destProperty: 'srcMainResources', blockOrRelativePath, }); } export function javaTestResourceTemplatesBlock(blockOrRelativePath = '') { return javaBlock({ srcPath: SERVER_TEST_RES_DIR, destProperty: 'srcTestResources', blockOrRelativePath, }); } export function javaTestPackageTemplatesBlock(blockOrRelativePath = '') { return javaBlock({ srcPath: `${SERVER_TEST_SRC_DIR}_package_/`, destProperty: 'javaPackageTestDir', blockOrRelativePath, }); } function javaBlock({ srcPath, destProperty, blockOrRelativePath = '', }) { const block = typeof blockOrRelativePath !== 'string' ? blockOrRelativePath : undefined; const blockRenameTo = typeof block?.renameTo === 'function' ? block.renameTo : undefined; const relativePath = typeof blockOrRelativePath === 'string' ? blockOrRelativePath : (blockOrRelativePath.relativePath ?? ''); return { path: `${srcPath}${relativePath}`, ...block, renameTo(data, filePath) { return `${data[destProperty]}${replaceEntityFilePathVariables(data, relativePath) ?? ''}${replaceEntityFilePathVariables(data, blockRenameTo?.call?.(this, data, filePath) ?? filePath) ?? ''}`; }, }; }