1818 */
1919import assert from 'node:assert' ;
2020
21+ import { transformContents } from '@yeoman/transform' ;
2122import { escapeRegExp , kebabCase } from 'lodash-es' ;
23+ import type { MemFsEditorFile } from 'mem-fs-editor' ;
2224
2325import type { CascatedEditFileCallback , EditFileCallback } from '../api.ts' ;
2426import type CoreGenerator from '../index.ts' ;
@@ -27,6 +29,10 @@ import { joinCallbacks } from './write-files.ts';
2729
2830export type NeedleCallback = ( content : string ) => string ;
2931
32+ const needlesWhiteList = [
33+ 'liquibase-add-incremental-changelog' , // mandatory for incremental changelogs
34+ ] ;
35+
3036type NeedleContentToAddCallback = {
3137 /**
3238 * Position of the needle start.
@@ -86,6 +92,27 @@ export const convertToPrettierExpressions = (str: string): string =>
8692 . replace ( / (? ! ) ( > | \\ \) ) / g, ',?\\n?[\\s]*$1' )
8793 . replace ( / \s + / g, '[\\s\\n]*' ) ;
8894
95+ export const createNeedleRegexp = ( needle : string ) : RegExp => new RegExp ( `(?://|<!--|\\{?/\\*|#) ${ needle } (?: [^$\\n]*)?(?:$|\\n)` , 'g' ) ;
96+
97+ type NeedleLinePosition = {
98+ start : number ;
99+ end : number ;
100+ } ;
101+
102+ export const getNeedlesPositions = ( content : string , needle = 'jhipster-needle-(?:[-\\w]*)' ) : NeedleLinePosition [ ] => {
103+ const regexp = createNeedleRegexp ( needle ) ;
104+ const positions : NeedleLinePosition [ ] = [ ] ;
105+ let match : RegExpExecArray | null ;
106+ while ( ( match = regexp . exec ( content ) ) ) {
107+ if ( needlesWhiteList . some ( whileList => match ! [ 0 ] . includes ( whileList ) ) ) {
108+ continue ;
109+ }
110+ const needleLineIndex = content . lastIndexOf ( '\n' , match . index ) + 1 ;
111+ positions . unshift ( { start : needleLineIndex , end : regexp . lastIndex } ) ;
112+ }
113+ return positions ;
114+ } ;
115+
89116/**
90117 * Check if contentToCheck existing in content
91118 *
@@ -135,7 +162,7 @@ export const insertContentBeforeNeedle = ({ content, contentToAdd, needle, autoI
135162
136163 needle = addNeedlePrefix ( needle ) ;
137164
138- const regexp = new RegExp ( `(?://|<!--|\\{?/\\*|#) ${ needle } (?:$|\n| )` , 'g' ) ;
165+ const regexp = createNeedleRegexp ( needle ) ;
139166 let firstMatch = regexp . exec ( content ) ;
140167 if ( ! firstMatch ) {
141168 return null ;
@@ -307,3 +334,18 @@ export function createBaseNeedle<Generator extends CoreGenerator = CoreGenerator
307334
308335 return callback ;
309336}
337+
338+ export const createNeedleTransform = ( ) =>
339+ transformContents < MemFsEditorFile > ( content => {
340+ if ( content ) {
341+ let contentAsString = content . toString ( ) ;
342+ const positions = getNeedlesPositions ( contentAsString ) ;
343+ if ( positions . length > 0 ) {
344+ for ( const position of positions ) {
345+ contentAsString = contentAsString . slice ( 0 , position . start ) + contentAsString . slice ( position . end ) ;
346+ }
347+ return Buffer . from ( contentAsString ) ;
348+ }
349+ }
350+ return content ;
351+ } ) ;
0 commit comments