Skip to content
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

A bit of code improvement #27289

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion generators/angular/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default asWritingTask(function cleanupOldFilesTask(this, { application })
this.removeFile(`${application.clientSrcDir}app/shared/login/login.component.ts`);
this.removeFile(`${application.clientSrcDir}app/shared/login/login.component.html`);
this.removeFile(`${application.clientSrcDir}app/core/auth/user-route-access-service.ts`);
if (!application.authenticationTypeSession || !(application as any).communicationSpringWebsocket) {
if (!application.authenticationTypeSession || !application.communicationSpringWebsocket) {
this.removeFile(`${application.clientSrcDir}app/core/auth/csrf.service.ts`);
}
this.removeFolder(`${application.clientSrcDir}app/core/login`);
Expand Down
158 changes: 18 additions & 140 deletions generators/angular/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { camelCase } from 'lodash-es';
import chalk from 'chalk';
import { isFileStateModified } from 'mem-fs-editor/state';

import type { Entity } from '../../lib/types/application/entity.js';
import BaseApplicationGenerator from '../base-application/index.js';
import { GENERATOR_ANGULAR, GENERATOR_CLIENT, GENERATOR_LANGUAGES } from '../generator-list.js';
import { defaultLanguage } from '../languages/support/index.js';
Expand All @@ -33,7 +32,7 @@ import {
} from '../client/support/index.js';
import { createNeedleCallback, mutateData } from '../base/support/index.js';
import { writeEslintClientRootConfigFile } from '../javascript/generators/eslint/support/tasks.js';
import type { PostWritingEntitiesTaskParam } from '../../lib/types/application/tasks.js';
import type { TaskTypes as DefaultTaskTypes } from '../../lib/types/application/tasks.js';
import { cleanupEntitiesFiles, postWriteEntitiesFiles, writeEntitiesFiles } from './entity-files-angular.js';
import { writeFiles } from './files-angular.js';
import cleanupOldFilesTask from './cleanup.js';
Expand All @@ -48,10 +47,15 @@ import {
isTranslatedAngularFile,
translateAngularFilesTransform,
} from './support/index.js';
import type { AngularApplication } from './types.js';

const { ANGULAR } = clientFrameworkTypes;

export default class AngularGenerator extends BaseApplicationGenerator {
export default class AngularGenerator extends BaseApplicationGenerator<
Entity,
AngularApplication,
DefaultTaskTypes<Entity, AngularApplication>
> {
localEntities?: any[];

async beforeQueue() {
Expand Down Expand Up @@ -100,8 +104,15 @@ export default class AngularGenerator extends BaseApplicationGenerator {
},
addNeedles({ source, application }) {
source.addEntitiesToClient = param => {
this.addEntitiesToModule(param);
this.addEntitiesToMenu(param);
const routeTemplatePath = `${param.application.clientSrcDir}app/entities/entity.routes.ts`;
const ignoreNonExistingRoute = chalk.yellow(`Route(s) not added to ${routeTemplatePath}.`);
const addRouteCallback = addEntitiesRoute(param);
this.editFile(routeTemplatePath, { ignoreNonExisting: ignoreNonExistingRoute }, addRouteCallback);

const filePath = `${application.clientSrcDir}app/layouts/navbar/navbar.component.html`;
const ignoreNonExisting = chalk.yellow('Reference to entities not added to menu.');
const editCallback = addToEntitiesMenu(param);
this.editFile(filePath, { ignoreNonExisting }, editCallback);
};

source.addAdminRoute = (args: Omit<Parameters<typeof addRoute>[0], 'needle'>) =>
Expand Down Expand Up @@ -196,7 +207,6 @@ export default class AngularGenerator extends BaseApplicationGenerator {
returnValue = fieldDefaultValue;
}
}

return returnValue;
},
});
Expand All @@ -222,7 +232,7 @@ export default class AngularGenerator extends BaseApplicationGenerator {
filter: file => isFileStateModified(file) && file.path.startsWith(this.destinationPath()) && isTranslatedAngularFile(file),
refresh: false,
},
translateAngularFilesTransform(control.getWebappTranslation, { enableTranslation, jhiPrefix }),
translateAngularFilesTransform(control.getWebappTranslation!, { enableTranslation, jhiPrefix }),
);
},
});
Expand Down Expand Up @@ -314,123 +324,6 @@ export default class AngularGenerator extends BaseApplicationGenerator {
get [BaseApplicationGenerator.END]() {
return this.delegateTasksToBlueprint(() => this.end);
}

/**
* @private
* Add new scss style to the angular application in "vendor.scss".
*
* @param {string} style - scss to add in the file
* @param {string} comment - comment to add before css code
*
* example:
*
* style = '.success {\n @extend .message;\n border-color: green;\n}'
* comment = 'Message'
*
* * ==========================================================================
* Message
* ========================================================================== *
* .success {
* @extend .message;
* border-color: green;
* }
*
*/
addVendorSCSSStyle(style, comment?) {
this.needleApi.clientAngular.addVendorSCSSStyle(style, comment);
}

/**
* @private
* Add a new lazy loaded module to admin routing file.
*
* @param {string} route - The route for the module. For example 'entity-audit'.
* @param {string} modulePath - The path to the module file. For example './entity-audit/entity-audit.module'.
* @param {string} moduleName - The name of the module. For example 'EntityAuditModule'.
* @param {string} pageTitle - The translation key if i18n is enabled or the text if i18n is disabled for the page title in the browser.
* For example 'entityAudit.home.title' for i18n enabled or 'Entity audit' for i18n disabled.
* If undefined then application global page title is used in the browser title bar.
*/
addAdminRoute(route, modulePath, moduleName, pageTitle) {
this.needleApi.clientAngular.addAdminRoute(route, modulePath, moduleName, pageTitle);
}

/**
* @private
* Add a new module in the TS modules file.
*
* @param {string} appName - Angular2 application name.
* @param {string} angularName - The name of the new admin item.
* @param {string} folderName - The name of the folder.
* @param {string} fileName - The name of the file.
* @param {boolean} enableTranslation - If translations are enabled or not.
* @param {string} clientFramework - The name of the client framework.
*/
addAngularModule(appName, angularName, folderName, fileName, enableTranslation) {
this.needleApi.clientAngular.addModule(appName, angularName, folderName, fileName, enableTranslation);
}

/**
* @private
* Add a new icon to icon imports.
*
* @param {string} iconName - The name of the Font Awesome icon.
*/
addIcon(iconName) {
this.needleApi.clientAngular.addIcon(iconName);
}

/**
* Add a new menu element to the admin menu.
*
* @param {string} routerName - The name of the Angular router that is added to the admin menu.
* @param {string} iconName - The name of the Font Awesome icon that will be displayed.
* @param {boolean} enableTranslation - If translations are enabled or not
* @param {string} translationKeyMenu - i18n key for entry in the admin menu
*/
addElementToAdminMenu(routerName, iconName, enableTranslation, translationKeyMenu = camelCase(routerName), jhiPrefix?) {
this.needleApi.clientAngular.addElementToAdminMenu(routerName, iconName, enableTranslation, translationKeyMenu, jhiPrefix);
}

addEntitiesToMenu({ application, entities }: Pick<PostWritingEntitiesTaskParam, 'application' | 'entities'>) {
const filePath = `${application.clientSrcDir}app/layouts/navbar/navbar.component.html`;
const ignoreNonExisting = chalk.yellow('Reference to entities not added to menu.');
const editCallback = addToEntitiesMenu({ application, entities });

this.editFile(filePath, { ignoreNonExisting }, editCallback);
}

addEntitiesToModule(param: Pick<PostWritingEntitiesTaskParam, 'application' | 'entities'>) {
const filePath = `${param.application.clientSrcDir}app/entities/entity.routes.ts`;
const ignoreNonExisting = chalk.yellow(`Route(s) not added to ${filePath}.`);
const addRouteCallback = addEntitiesRoute(param);
this.editFile(filePath, { ignoreNonExisting }, addRouteCallback);
}

/**
* @private
* Add new scss style to the angular application in "global.scss
*
* @param {string} style - css to add in the file
* @param {string} comment - comment to add before css code
*
* example:
*
* style = '.jhipster {\n color: #baa186;\n}'
* comment = 'New JHipster color'
*
* * ==========================================================================
* New JHipster color
* ========================================================================== *
* .jhipster {
* color: #baa186;
* }
*
*/
addMainSCSSStyle(style, comment?) {
this.needleApi.clientAngular.addGlobalSCSSStyle(style, comment);
}

/**
* Returns the typescript import section of enums referenced by all fields of the entity.
* @param fields returns the import of enums that are referenced by the fields
Expand Down Expand Up @@ -459,7 +352,6 @@ export default class AngularGenerator extends BaseApplicationGenerator {
generateTestEntityId(primaryKey, index = 0, wrapped = true) {
return getTestEntityId(primaryKey, index, wrapped);
}

/**
* @private
* Generate a test entity, for the PK references (when the PK is a composite, derived key)
Expand Down Expand Up @@ -493,18 +385,4 @@ export default class AngularGenerator extends BaseApplicationGenerator {
buildAngularFormPath(reference, prefix = []) {
return angularFormPath(reference, prefix);
}

/**
* @private
* Add a new menu element, at the root of the menu.
*
* @param {string} routerName - The name of the router that is added to the menu.
* @param {string} iconName - The name of the Font Awesome icon that will be displayed.
* @param {boolean} enableTranslation - If translations are enabled or not
* @param {string} clientFramework - The name of the client framework
* @param {string} translationKeyMenu - i18n key for entry in the menu
*/
addElementToMenu(routerName, iconName, enableTranslation, _clientFramework?, translationKeyMenu = camelCase(routerName)) {
this.needleApi.clientAngular.addElementToMenu(routerName, iconName, enableTranslation, translationKeyMenu);
}
}
2 changes: 2 additions & 0 deletions generators/angular/support/translate-angular.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ describe('generator - angular - transform', () => {
beforeEach(() => {
let value = 0;
const testImpl = (key, data) => (key === 'blank' ? '' : `translated-value-${key}-${data ? `${inspect(data)}-` : ''}${value++}`);
// @ts-ignore
replaceAngularTranslations = createTranslationReplacer(esmocha.fn().mockImplementation(testImpl), {
jhiPrefix: 'jhi',
enableTranslation: false,
});
// @ts-ignore
enabledAngularTranslations = createTranslationReplacer(esmocha.fn().mockImplementation(testImpl), {
jhiPrefix: 'jhi',
enableTranslation: true,
Expand Down
Loading
Loading