|
18 | 18 | */ |
19 | 19 | import assert from 'assert'; |
20 | 20 | import { escapeRegExp, kebabCase } from 'lodash-es'; |
| 21 | +import { transformContents } from '@yeoman/transform'; |
| 22 | +import type { MemFsEditorFile } from 'mem-fs-editor'; |
21 | 23 | import type CoreGenerator from '../../base-core/index.js'; |
22 | 24 | import type { CascatedEditFileCallback, EditFileCallback, NeedleCallback } from '../api.js'; |
23 | 25 | import { joinCallbacks } from './write-files.js'; |
24 | 26 |
|
| 27 | +const needlesWhiteList = [ |
| 28 | + 'liquibase-add-incremental-changelog', // mandatory for incremental changelogs |
| 29 | +]; |
| 30 | + |
25 | 31 | type NeedleContentToAddCallback = { |
26 | 32 | /** |
27 | 33 | * Position of the needle start. |
@@ -81,6 +87,27 @@ export const convertToPrettierExpressions = (str: string): string => |
81 | 87 | .replace(/(?! )(>|\\\))/g, ',?\\n?[\\s]*$1') |
82 | 88 | .replace(/\s+/g, '[\\s\\n]*'); |
83 | 89 |
|
| 90 | +export const createNeedleRegexp = (needle: string): RegExp => new RegExp(`(?://|<!--|/\\*|#) ${needle}(?: [^$\\n]*)?(?:$|\\n)`, 'g'); |
| 91 | + |
| 92 | +type NeedleLinePosition = { |
| 93 | + start: number; |
| 94 | + end: number; |
| 95 | +}; |
| 96 | + |
| 97 | +export const getNeedlesPositions = (content: string, needle = 'jhipster-needle-(?:[-\\w]*)'): NeedleLinePosition[] => { |
| 98 | + const regexp = createNeedleRegexp(needle); |
| 99 | + const positions: NeedleLinePosition[] = []; |
| 100 | + let match: RegExpExecArray | null; |
| 101 | + while ((match = regexp.exec(content))) { |
| 102 | + if (needlesWhiteList.some(whileList => match![0].includes(whileList))) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + const needleLineIndex = content.lastIndexOf('\n', match.index) + 1; |
| 106 | + positions.unshift({ start: needleLineIndex, end: regexp.lastIndex }); |
| 107 | + } |
| 108 | + return positions; |
| 109 | +}; |
| 110 | + |
84 | 111 | /** |
85 | 112 | * Check if contentToCheck existing in content |
86 | 113 | * |
@@ -120,7 +147,7 @@ export const insertContentBeforeNeedle = ({ content, contentToAdd, needle, autoI |
120 | 147 |
|
121 | 148 | needle = needle.includes('jhipster-needle-') ? needle : `jhipster-needle-${needle}`; |
122 | 149 |
|
123 | | - const regexp = new RegExp(`(?://|<!--|/\\*|#) ${needle}(?:$|\n| )`, 'g'); |
| 150 | + const regexp = createNeedleRegexp(needle); |
124 | 151 | const firstMatch = regexp.exec(content); |
125 | 152 | if (!firstMatch) { |
126 | 153 | return null; |
@@ -279,3 +306,18 @@ export function createBaseNeedle<Generator extends CoreGenerator = CoreGenerator |
279 | 306 |
|
280 | 307 | return callback; |
281 | 308 | } |
| 309 | + |
| 310 | +export const createNeedleTransform = () => |
| 311 | + transformContents<MemFsEditorFile>(content => { |
| 312 | + if (content) { |
| 313 | + let contentAsString = content.toString(); |
| 314 | + const positions = getNeedlesPositions(contentAsString); |
| 315 | + if (positions.length > 0) { |
| 316 | + for (const position of positions) { |
| 317 | + contentAsString = contentAsString.slice(0, position.start) + contentAsString.slice(position.end); |
| 318 | + } |
| 319 | + return Buffer.from(contentAsString); |
| 320 | + } |
| 321 | + } |
| 322 | + return content; |
| 323 | + }); |
0 commit comments