Skip to content

Commit c039068

Browse files
committed
bootstrap: add option to remove needles
1 parent cdc32f7 commit c039068

File tree

19 files changed

+84
-3
lines changed

19 files changed

+84
-3
lines changed

.blueprint/generate-sample/templates/test-integration/workflow-samples/angular.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"app-sample": "ng-default",
1414
"jdl-entity": "*",
1515
"generatorOptions": {
16+
"removeNeedles": true,
1617
"clientTestFramework": "vitest",
1718
"clientBundler": "webpack"
1819
}

.blueprint/github-build-matrix/__snapshots__/generator.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ exports[`generator - github-build-matrix with angular should match matrix value
4141
"app-sample": "ng-default",
4242
"jdl-entity": "*",
4343
"generatorOptions": {
44+
"removeNeedles": true,
4445
"clientTestFramework": "vitest",
4546
"clientBundler": "webpack"
4647
},

generators/angular/generators/bootstrap/__snapshots__/generator.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ exports[`generator - angular:bootstrap bootstrapping default config should prepa
219219
"projectVersion": undefined,
220220
"reactive": false,
221221
"rememberMeKey": undefined,
222+
"removeNeedles": undefined,
222223
"routes": undefined,
223224
"sampleWritten": undefined,
224225
"searchEngine": "no",

generators/app/__snapshots__/generator.spec.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports[`generator - app help should print expected information 1`] = `
77
88
Options:
99
--auto-crlf Detect line endings
10+
--remove-needles Remove needles, needles are used by entity and language generators to inject code
1011
--defaults Execute jhipster with default config
1112
--jhi-prefix <value> Add prefix before services, controllers and states name
1213
--entity-suffix <value> Add suffix after entities name
@@ -836,6 +837,7 @@ exports[`generator - app with default config should match snapshot 1`] = `
836837
"reactorBlock": "",
837838
"reactorBlockOptional": "",
838839
"rememberMeKey": undefined,
840+
"removeNeedles": undefined,
839841
"requiresDeleteAllUsers": false,
840842
"routes": undefined,
841843
"sampleWritten": undefined,
@@ -1526,6 +1528,7 @@ exports[`generator - app with gateway should match snapshot 1`] = `
15261528
"reactorBlock": ".block()",
15271529
"reactorBlockOptional": ".blockOptional()",
15281530
"rememberMeKey": undefined,
1531+
"removeNeedles": undefined,
15291532
"requiresDeleteAllUsers": true,
15301533
"routes": undefined,
15311534
"sampleWritten": undefined,
@@ -2129,6 +2132,7 @@ exports[`generator - app with microservice should match snapshot 1`] = `
21292132
"reactorBlock": "",
21302133
"reactorBlockOptional": "",
21312134
"rememberMeKey": undefined,
2135+
"removeNeedles": undefined,
21322136
"requiresDeleteAllUsers": false,
21332137
"routes": undefined,
21342138
"sampleWritten": undefined,

generators/base-core/support/needles.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
*/
1919
import assert from 'node:assert';
2020

21+
import { transformContents } from '@yeoman/transform';
2122
import { escapeRegExp, kebabCase } from 'lodash-es';
23+
import type { MemFsEditorFile } from 'mem-fs-editor';
2224

2325
import type { CascatedEditFileCallback, EditFileCallback } from '../api.ts';
2426
import type CoreGenerator from '../index.ts';
@@ -27,6 +29,10 @@ import { joinCallbacks } from './write-files.ts';
2729

2830
export type NeedleCallback = (content: string) => string;
2931

32+
const needlesWhiteList = [
33+
'liquibase-add-incremental-changelog', // mandatory for incremental changelogs
34+
];
35+
3036
type 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+
});

generators/bootstrap/command.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ const command = {
2727
},
2828
scope: 'storage',
2929
},
30+
removeNeedles: {
31+
description: 'Remove needles, needles are used by entity and language generators to inject code',
32+
cli: {
33+
type: Boolean,
34+
},
35+
jdl: {
36+
tokenType: 'BOOLEAN',
37+
type: 'boolean',
38+
},
39+
scope: 'storage',
40+
},
3041
skipPrettier: {
3142
description: 'Skip prettier transformation',
3243
cli: {

generators/bootstrap/generator.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ import { isFilePending, isFileStateModified } from 'mem-fs-editor/state';
2626
import { createCommitTransform } from 'mem-fs-editor/transform';
2727
import type { Options as PrettierOptions } from 'prettier';
2828

29-
import BaseGenerator from '../base/index.ts';
29+
import BaseGenerator, { CommandBaseGenerator } from '../base/index.ts';
3030
import type { Features as BaseFeatures, Options as BaseOptions } from '../base/types.d.ts';
3131
import { PRIORITY_NAMES, QUEUES } from '../base-application/priorities.ts';
32+
import { createNeedleTransform } from '../base-core/support/needles.ts';
3233
import { PRETTIER_EXTENSIONS } from '../generator-constants.js';
3334

35+
import type command from './command.ts';
3436
import {
3537
autoCrlfTransform,
3638
createESLintTransform,
@@ -48,7 +50,7 @@ const { MULTISTEP_TRANSFORM_QUEUE, PRE_CONFLICTS_QUEUE } = QUEUES;
4850
const MULTISTEP_TRANSFORM_PRIORITY = BaseGenerator.asPriority(MULTISTEP_TRANSFORM);
4951
const PRE_CONFLICTS_PRIORITY = BaseGenerator.asPriority(PRE_CONFLICTS);
5052

51-
export default class BootstrapGenerator extends BaseGenerator {
53+
export default class BootstrapGenerator extends CommandBaseGenerator<typeof command> {
5254
static MULTISTEP_TRANSFORM = MULTISTEP_TRANSFORM_PRIORITY;
5355

5456
static PRE_CONFLICTS = PRE_CONFLICTS_PRIORITY;
@@ -240,11 +242,17 @@ export default class BootstrapGenerator extends BaseGenerator {
240242
};
241243
}
242244

245+
const removeNeedlesTransforms: FileTransform<MemFsEditorFile>[] = [];
246+
if (this.jhipsterConfig.removeNeedles) {
247+
removeNeedlesTransforms.push(createNeedleTransform());
248+
}
249+
243250
const transformStreams = [
244251
...skipYoResolveTransforms,
245252
forceYoFiles(),
246253
createSortConfigFilesTransform(),
247254
createForceWriteConfigFilesTransform(),
255+
...removeNeedlesTransforms,
248256
...prettierTransforms,
249257
...autoCrlfTransforms,
250258
createConflicterTransform(this.env.adapter, { ...this.env.conflicterOptions, customizeActions }),

generators/ci-cd/__snapshots__/generator.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ exports[`generator - ci-cd cli with multiples values should match context snapsh
360360
"reactorBlock": "",
361361
"reactorBlockOptional": "",
362362
"rememberMeKey": undefined,
363+
"removeNeedles": undefined,
363364
"routes": undefined,
364365
"sampleWritten": undefined,
365366
"searchEngine": "no",

generators/client/generators/bootstrap/__snapshots__/generator.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ exports[`generator - client:bootstrap bootstrapping default config should prepar
220220
"projectVersion": undefined,
221221
"reactive": false,
222222
"rememberMeKey": undefined,
223+
"removeNeedles": undefined,
223224
"routes": undefined,
224225
"sampleWritten": undefined,
225226
"searchEngine": "no",

generators/common/generators/bootstrap/__snapshots__/generator.spec.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ exports[`generator - common:bootstrap bootstrapping default config should prepar
209209
"projectVersion": undefined,
210210
"reactive": false,
211211
"rememberMeKey": undefined,
212+
"removeNeedles": undefined,
212213
"routes": undefined,
213214
"sampleWritten": undefined,
214215
"searchEngine": "no",

0 commit comments

Comments
 (0)