Skip to content

angular: convert entity-navbar-items.ts to needles #29531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions generators/angular/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,29 @@ export default class AngularGenerator extends BaseApplicationGenerator<AngularEn
const ignoreNonExisting = chalk.yellow('Reference to entities not added to menu.');
const editCallback = addToEntitiesMenu(param);
this.editFile(filePath, { ignoreNonExisting }, editCallback);

this.editFile(
`${application.clientSrcDir}app/entities/entity-navbar-items.ts`,
createNeedleCallback({
needle: 'add-entity-navbar',
contentToAdd: param.entities
.filter(e => !e.adminEntity)
.map(entity => ({
contentToCheck: `route: '/${entity.entityPage}',`,
content: `{
name: '${entity.entityAngularName}',
route: '/${entity.entityPage}',${
application.enableTranslation
? `
translationKey: 'global.menu.entities.${entity.entityTranslationKey}',`
: ''
}
},`,
})),
}),
);
};

source.addAdminRoute = (args: Omit<Parameters<typeof addRoute>[0], 'needle'>) =>
this.editFile(
`${application.srcMainWebapp}app/admin/admin.routes.ts`,
Expand Down Expand Up @@ -249,9 +271,6 @@ export default class AngularGenerator extends BaseApplicationGenerator<AngularEn

get default() {
return this.asDefaultTaskGroup({
loadEntities({ application, entities }) {
application.angularEntities = entities.filter(entity => !entity.builtIn && !entity.skipClient) as AngularEntity[];
},
queueTranslateTransform({ application }) {
const { enableTranslation, jhiPrefix } = application;
this.queueTransformStream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,5 @@
import NavbarItem from 'app/layouts/navbar/navbar-item.model';

export const EntityNavbarItems: NavbarItem[] = [
<%_ for (const entity of angularEntities) { _%>
{
name: '<%= entity.entityAngularName %>',
route: '/<%= entity.entityPage %>',
<%_ if (enableTranslation) { _%>
translationKey: 'global.menu.entities.<%= entity.entityTranslationKey %>',
<%_ } _%>
},
<%_ } _%>
/* jhipster-needle-add-entity-navbar - JHipster will add entity navbar items here */
];
4 changes: 1 addition & 3 deletions generators/angular/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,5 @@ export type AngularEntity = Entity & {
};

export type AngularApplication = {
/** @experimental to be replaced with needles */
angularEntities?: AngularEntity[];
angularLocaleId: string;
angularLocaleId?: string;
} & ApplicationType<AngularEntity>;
2 changes: 0 additions & 2 deletions generators/app/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ exports[`generator - app with default config should match snapshot 1`] = `
"addOpenapiGeneratorPlugin": undefined,
"addPrettierExtensions": [Function],
"addSpringMilestoneRepository": false,
"angularEntities": [],
"angularLocaleId": "en",
"anyEntityHasRelationshipWithUser": false,
"applicationType": "monolith",
Expand Down Expand Up @@ -902,7 +901,6 @@ exports[`generator - app with gateway should match snapshot 1`] = `
"addOpenapiGeneratorPlugin": undefined,
"addPrettierExtensions": [Function],
"addSpringMilestoneRepository": false,
"angularEntities": [],
"angularLocaleId": "en",
"anyEntityHasRelationshipWithUser": false,
"applicationType": "gateway",
Expand Down
28 changes: 28 additions & 0 deletions generators/base/support/needles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,34 @@ content to add2
`);
});

it('returned function should add contentToAdd array of objects', () => {
const log = test.mock.fn(createJHipsterLogger());
const needleCallback = createNeedleCallback({
contentToAdd: [
{ content: contentToAdd },
{ content: `${contentToAdd}2` },
{ content: 'existing content' },
{ content: 'test', contentToCheck: 'another existing' },
],
needle,
});
expect(
needleCallback.call(
{ log } as any,
`existing content
another existing
// jhipster-needle-${needle}`,
'any-file',
),
).toMatchInlineSnapshot(`
"existing content
another existing
content to add
content to add2
// jhipster-needle-a-needle"
`);
});

it('contentToAdd should be called', () => {
const log = test.mock.fn(createJHipsterLogger());
const contentToAdd = test.mock.fn((_arg0: string, _arg1: any) => 'result content');
Expand Down
31 changes: 22 additions & 9 deletions generators/base/support/needles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ type NeedleContentToAddCallback = {
indentPrefix: string;
};

type ContentToAdd = { content: string; contentToCheck?: string | RegExp };

export type NeedleInsertion = {
needle: string;
/**
* Content to add.
*/
contentToAdd: string | string[] | ((content: string, options: NeedleContentToAddCallback) => string);
contentToAdd: string | string[] | ContentToAdd[] | ((content: string, options: NeedleContentToAddCallback) => string);
contentToCheck?: string | RegExp;
/**
* check existing content ignoring white spaces and new lines.
Expand All @@ -56,7 +58,7 @@ export type NeedleInsertion = {
autoIndent?: boolean;
};

type NeedleFileInsertion = NeedleInsertion & {
type NeedleFileInsertion = Omit<NeedleInsertion, 'needle' | 'contentToAdd'> & {
/**
* Path to file.
* The generator context must be passed.
Expand All @@ -68,8 +70,9 @@ type NeedleFileInsertion = NeedleInsertion & {
needlesPrefix?: string;
};

type NeedleContentInsertion = NeedleInsertion & {
type NeedleContentInsertion = Pick<NeedleInsertion, 'needle' | 'autoIndent'> & {
content: string;
contentToAdd: string | string[] | ((content: string, options: NeedleContentToAddCallback) => string);
};

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

const isArrayOfContentToAdd = (value: unknown): value is ContentToAdd[] => {
return Array.isArray(value) && value.every(item => typeof item === 'object' && 'content' in item);
};

/**
* Check if contentToCheck existing in content
*
Expand Down Expand Up @@ -200,6 +207,15 @@ export const createNeedleCallback = <Generator extends CoreGenerator = CoreGener
assert(contentToAdd, 'contentToAdd is required');

return function (content, filePath) {
if (isArrayOfContentToAdd(contentToAdd)) {
contentToAdd = contentToAdd.filter(({ content: itemContent, contentToCheck }) => {
return !checkContentIn(contentToCheck ?? itemContent, content, ignoreWhitespaces);
});
if (contentToAdd.length === 0) {
return content;
}
contentToAdd = contentToAdd.map(({ content }) => content);
}
if (contentToCheck && checkContentIn(contentToCheck, content, ignoreWhitespaces)) {
return content;
}
Expand Down Expand Up @@ -238,19 +254,16 @@ export const createNeedleCallback = <Generator extends CoreGenerator = CoreGener
*
* @param this - generator if provided, editFile will be executed
*/
export function createBaseNeedle(
options: Omit<NeedleFileInsertion, 'filePath' | 'needle' | 'contentToAdd'>,
needles: Record<string, string>,
): NeedleCallback;
export function createBaseNeedle(options: Omit<NeedleFileInsertion, 'filePath'>, needles: Record<string, string>): NeedleCallback;
export function createBaseNeedle(needles: Record<string, string>): NeedleCallback;
export function createBaseNeedle<Generator extends CoreGenerator = CoreGenerator>(
this: Generator,
options: Omit<NeedleFileInsertion, 'filePath' | 'needle' | 'contentToAdd'> & { filePath: string },
options: NeedleFileInsertion,
needles: Record<string, string>,
): CascatedEditFileCallback<Generator>;
export function createBaseNeedle<Generator extends CoreGenerator = CoreGenerator>(
this: Generator | void,
options: Omit<NeedleFileInsertion, 'filePath' | 'needle' | 'contentToAdd'> | Record<string, string>,
options: NeedleFileInsertion | Record<string, string>,
needles?: Record<string, string>,
): EditFileCallback<Generator> | CascatedEditFileCallback<Generator> {
const actualNeedles = needles === undefined ? (options as Record<string, string>) : needles;
Expand Down
Loading