Skip to content

Commit f42dd62

Browse files
committed
internal: add support to array of content and contentToCheck to createNeedleCallback
1 parent 281f4d0 commit f42dd62

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

generators/base/support/needles.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,34 @@ content to add2
317317
`);
318318
});
319319

320+
it('returned function should add contentToAdd array of objects', () => {
321+
const log = test.mock.fn(createJHipsterLogger());
322+
const needleCallback = createNeedleCallback({
323+
contentToAdd: [
324+
{ content: contentToAdd },
325+
{ content: `${contentToAdd}2` },
326+
{ content: 'existing content' },
327+
{ content: 'test', contentToCheck: 'another existing' },
328+
],
329+
needle,
330+
});
331+
expect(
332+
needleCallback.call(
333+
{ log } as any,
334+
`existing content
335+
another existing
336+
// jhipster-needle-${needle}`,
337+
'any-file',
338+
),
339+
).toMatchInlineSnapshot(`
340+
"existing content
341+
another existing
342+
content to add
343+
content to add2
344+
// jhipster-needle-a-needle"
345+
`);
346+
});
347+
320348
it('contentToAdd should be called', () => {
321349
const log = test.mock.fn(createJHipsterLogger());
322350
const contentToAdd = test.mock.fn((_arg0: string, _arg1: any) => 'result content');

generators/base/support/needles.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ type NeedleContentToAddCallback = {
3535
indentPrefix: string;
3636
};
3737

38+
type ContentToAdd = { content: string; contentToCheck?: string | RegExp };
39+
3840
export type NeedleInsertion = {
3941
needle: string;
4042
/**
4143
* Content to add.
4244
*/
43-
contentToAdd: string | string[] | ((content: string, options: NeedleContentToAddCallback) => string);
45+
contentToAdd: string | string[] | ContentToAdd[] | ((content: string, options: NeedleContentToAddCallback) => string);
4446
contentToCheck?: string | RegExp;
4547
/**
4648
* check existing content ignoring white spaces and new lines.
@@ -56,7 +58,7 @@ export type NeedleInsertion = {
5658
autoIndent?: boolean;
5759
};
5860

59-
type NeedleFileInsertion = NeedleInsertion & {
61+
type NeedleFileInsertion = Omit<NeedleInsertion, 'needle' | 'contentToAdd'> & {
6062
/**
6163
* Path to file.
6264
* The generator context must be passed.
@@ -68,8 +70,9 @@ type NeedleFileInsertion = NeedleInsertion & {
6870
needlesPrefix?: string;
6971
};
7072

71-
type NeedleContentInsertion = NeedleInsertion & {
73+
type NeedleContentInsertion = Pick<NeedleInsertion, 'needle' | 'autoIndent'> & {
7274
content: string;
75+
contentToAdd: string | string[] | ((content: string, options: NeedleContentToAddCallback) => string);
7376
};
7477

7578
/**
@@ -81,6 +84,10 @@ export const convertToPrettierExpressions = (str: string): string =>
8184
.replace(/(?! )(>|\\\))/g, ',?\\n?[\\s]*$1')
8285
.replace(/\s+/g, '[\\s\\n]*');
8386

87+
const isArrayOfContentToAdd = (value: unknown): value is ContentToAdd[] => {
88+
return Array.isArray(value) && value.every(item => typeof item === 'object' && 'content' in item);
89+
};
90+
8491
/**
8592
* Check if contentToCheck existing in content
8693
*
@@ -200,6 +207,15 @@ export const createNeedleCallback = <Generator extends CoreGenerator = CoreGener
200207
assert(contentToAdd, 'contentToAdd is required');
201208

202209
return function (content, filePath) {
210+
if (isArrayOfContentToAdd(contentToAdd)) {
211+
contentToAdd = contentToAdd.filter(({ content: itemContent, contentToCheck }) => {
212+
return !checkContentIn(contentToCheck ?? itemContent, content, ignoreWhitespaces);
213+
});
214+
if (contentToAdd.length === 0) {
215+
return content;
216+
}
217+
contentToAdd = contentToAdd.map(({ content }) => content);
218+
}
203219
if (contentToCheck && checkContentIn(contentToCheck, content, ignoreWhitespaces)) {
204220
return content;
205221
}
@@ -238,19 +254,16 @@ export const createNeedleCallback = <Generator extends CoreGenerator = CoreGener
238254
*
239255
* @param this - generator if provided, editFile will be executed
240256
*/
241-
export function createBaseNeedle(
242-
options: Omit<NeedleFileInsertion, 'filePath' | 'needle' | 'contentToAdd'>,
243-
needles: Record<string, string>,
244-
): NeedleCallback;
257+
export function createBaseNeedle(options: Omit<NeedleFileInsertion, 'filePath'>, needles: Record<string, string>): NeedleCallback;
245258
export function createBaseNeedle(needles: Record<string, string>): NeedleCallback;
246259
export function createBaseNeedle<Generator extends CoreGenerator = CoreGenerator>(
247260
this: Generator,
248-
options: Omit<NeedleFileInsertion, 'filePath' | 'needle' | 'contentToAdd'> & { filePath: string },
261+
options: NeedleFileInsertion,
249262
needles: Record<string, string>,
250263
): CascatedEditFileCallback<Generator>;
251264
export function createBaseNeedle<Generator extends CoreGenerator = CoreGenerator>(
252265
this: Generator | void,
253-
options: Omit<NeedleFileInsertion, 'filePath' | 'needle' | 'contentToAdd'> | Record<string, string>,
266+
options: NeedleFileInsertion | Record<string, string>,
254267
needles?: Record<string, string>,
255268
): EditFileCallback<Generator> | CascatedEditFileCallback<Generator> {
256269
const actualNeedles = needles === undefined ? (options as Record<string, string>) : needles;

0 commit comments

Comments
 (0)