UNPKG

generator-begcode

Version:

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

34 lines (33 loc) 1.71 kB
import { escapeRegExp } from 'lodash-es'; const addJavaImportToContent = (content, identifier, { staticImport } = {}) => { const importStatement = `import ${staticImport ? 'static ' : ''}${identifier};`; return new RegExp(escapeRegExp(importStatement)).test(content) ? content : content.replace(/(package [\w.]+;\n\n?)/, `$1${importStatement}\n`); }; export function addJavaImport(contentOrIdentifier, identifierOrType, type) { if (typeof identifierOrType === 'string') { return addJavaImportToContent(contentOrIdentifier, identifierOrType, type); } return (content) => addJavaImportToContent(content, contentOrIdentifier, identifierOrType); } const addJavaAnnotationToContent = (content, annotationDef) => { const { package: packageName, annotation, parameters } = annotationDef; if (packageName) { content = addJavaImport(content, `${packageName}.${annotation}`); } const annotationToAdd = parameters ? `${annotation}(${parameters()})` : annotation; if (!new RegExp(escapeRegExp(`\n@${annotationToAdd}\n`)).test(content)) { if (new RegExp(escapeRegExp(`\n@${annotation}(`)).test(content)) { throw new Error(`Annotation already exists: ${annotation} replace is not implemented yet.`); } content = content.replace(/\n([a-w ]*(class|@?interface|enum) )/, `\n@${annotationToAdd}\n$1`); } return content; }; export function addJavaAnnotation(contentOrAnnotation, annotation) { if (typeof contentOrAnnotation === 'string') { return addJavaAnnotationToContent(contentOrAnnotation, annotation); } return (content) => addJavaAnnotationToContent(content, contentOrAnnotation); }