From e12520440045b53f1db421dbee110249d449a2c3 Mon Sep 17 00:00:00 2001 From: Charlie Mordant Date: Sun, 15 Sep 2024 18:28:58 +0200 Subject: [PATCH] remove some unused generator-angular methods --- generators/angular/generator.ts | 146 ++---------------- generators/angular/types.d.ts | 4 +- generators/client/support/template-utils.ts | 27 ++-- .../needle-client-angular-generator.spec.ts | 21 --- test/needle-api/needle-client-angular.spec.ts | 112 -------------- 5 files changed, 27 insertions(+), 283 deletions(-) delete mode 100644 test/needle-api/needle-client-angular.spec.ts diff --git a/generators/angular/generator.ts b/generators/angular/generator.ts index b3ea7497a819..6bc6889ef723 100644 --- a/generators/angular/generator.ts +++ b/generators/angular/generator.ts @@ -16,7 +16,6 @@ * 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'; @@ -33,7 +32,6 @@ 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 { cleanupEntitiesFiles, postWriteEntitiesFiles, writeEntitiesFiles } from './entity-files-angular.js'; import { writeFiles } from './files-angular.js'; import cleanupOldFilesTask from './cleanup.js'; @@ -100,8 +98,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[0], 'needle'>) => @@ -196,7 +201,6 @@ export default class AngularGenerator extends BaseApplicationGenerator { returnValue = fieldDefaultValue; } } - return returnValue; }, }); @@ -314,123 +318,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) { - 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) { - 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 @@ -459,7 +346,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) @@ -493,18 +379,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); - } } diff --git a/generators/angular/types.d.ts b/generators/angular/types.d.ts index 311a20481eaf..bb225f3cda1c 100644 --- a/generators/angular/types.d.ts +++ b/generators/angular/types.d.ts @@ -1 +1,3 @@ -export type AngularApplication = { angularLocaleId: string }; +export type AngularApplication = { + angularLocaleId: string; +}; diff --git a/generators/client/support/template-utils.ts b/generators/client/support/template-utils.ts index 8dd72e90beb5..a1681e43e29e 100644 --- a/generators/client/support/template-utils.ts +++ b/generators/client/support/template-utils.ts @@ -20,6 +20,7 @@ import path from 'path'; import { clientFrameworkTypes, fieldTypes } from '../../../lib/jhipster/index.js'; import type { PrimaryKey } from '../../../lib/types/application/entity.js'; +import type { Field } from '../../../lib/types/application/index.js'; import { getEntryIfTypeOrTypeAttribute } from './types-utils.js'; const { STRING: TYPE_STRING, UUID: TYPE_UUID } = fieldTypes.CommonDBTypes; @@ -74,20 +75,22 @@ export const generateEntityClientImports = (relationships, dto?, clientFramework * @param {string} clientFramework the client framework, 'angular' or 'react'. * @returns typeImports: Map */ -export const generateEntityClientEnumImports = (fields, clientFramework) => { +export const generateEntityClientEnumImports = (fields: Field[] | undefined, clientFramework: string) => { const typeImports = new Map(); const uniqueEnums = {}; - fields.forEach(field => { - const { enumFileName, fieldType } = field; - if (field.fieldIsEnum && (!uniqueEnums[fieldType] || (uniqueEnums[fieldType] && field.fieldValues.length !== 0))) { - const importType = `${fieldType}`; - const basePath = clientFramework === VUE ? '@' : 'app'; - const modelPath = clientFramework === ANGULAR ? 'entities' : 'shared/model'; - const importPath = `${basePath}/${modelPath}/enumerations/${enumFileName}.model`; - uniqueEnums[fieldType] = field.fieldType; - typeImports.set(importType, importPath); - } - }); + if (fields && fields.forEach) { + fields.forEach(field => { + const { enumFileName, fieldType } = field; + if (field.fieldIsEnum && (!uniqueEnums[fieldType] || (uniqueEnums[fieldType] && field.fieldValues?.length !== 0))) { + const importType = `${fieldType}`; + const basePath = clientFramework === VUE ? '@' : 'app'; + const modelPath = clientFramework === ANGULAR ? 'entities' : 'shared/model'; + const importPath = `${basePath}/${modelPath}/enumerations/${enumFileName}.model`; + uniqueEnums[fieldType] = field.fieldType; + typeImports.set(importType, importPath); + } + }); + } return typeImports; }; diff --git a/test/needle-api/needle-client-angular-generator.spec.ts b/test/needle-api/needle-client-angular-generator.spec.ts index 932e45973cb7..6d51d997b4f0 100644 --- a/test/needle-api/needle-client-angular-generator.spec.ts +++ b/test/needle-api/needle-client-angular-generator.spec.ts @@ -13,9 +13,6 @@ const mockAngularBlueprintSubGen = class extends AngularGenerator { get [BaseApplicationGenerator.POST_WRITING_ENTITIES]() { return this.asPostWritingEntitiesTaskGroup({ - addToMenuStep() { - this.addElementToAdminMenu('routerName2', 'iconName2', true); - }, addToModuleStep({ source, application }) { source.addEntitiesToClient({ application, @@ -66,24 +63,6 @@ describe('needle API Angular angular generator : JHipster with blueprint', () => ); }); - it('admin menu contains the admin element added by needle api', () => { - runResult.assertFileContent( - `${CLIENT_MAIN_SRC_DIR}app/layouts/navbar/navbar.component.html`, - ` -
  • - - - Router Name 2 - -
  • -`, - ); - }); - - it('icon imports contains a new icon added by a new admin menu method of needle api ', () => { - runResult.assertFileContent(`${CLIENT_MAIN_SRC_DIR}app/config/font-awesome-icons.ts`, ' faIconName2'); - }); - it('entity module contains the microservice object added by needle api', () => { runResult.assertFileContent( `${CLIENT_MAIN_SRC_DIR}app/entities/entity.routes.ts`, diff --git a/test/needle-api/needle-client-angular.spec.ts b/test/needle-api/needle-client-angular.spec.ts deleted file mode 100644 index e9a9bb2981c1..000000000000 --- a/test/needle-api/needle-client-angular.spec.ts +++ /dev/null @@ -1,112 +0,0 @@ -import { before, describe, it } from 'esmocha'; -import { getGenerator, basicHelpers as helpers, result as runResult } from '../../lib/testing/index.js'; - -import { CLIENT_MAIN_SRC_DIR } from '../../generators/generator-constants.js'; -import BaseApplicationGenerator from '../../generators/base-application/index.js'; -import AngularGenerator from '../../generators/angular/index.js'; - -const mockBlueprintSubGen = class extends AngularGenerator { - constructor(args, opts, features) { - super(args, opts, features); - this.sbsBlueprint = true; - } - - get [BaseApplicationGenerator.POST_WRITING]() { - return this.asPostWritingTaskGroup({ - addCssStylesProperty() { - this.addMainSCSSStyle('@import style_without_comment;'); - this.addMainSCSSStyle('@import style;', 'my comment'); - this.addVendorSCSSStyle('@import style;', 'my comment'); - this.addVendorSCSSStyle('@import style_without_comment;'); - }, - addToMenuStep() { - this.addElementToMenu('routerName1', 'iconName1', true); - }, - addToModuleStep() { - this.addAngularModule('appName', 'angularName', 'folderName', 'fileName', true); - this.addAdminRoute('entity-audit', './entity-audit/entity-audit.module', 'EntityAuditModule', 'entityAudit.home.title'); - }, - }); - } -}; - -describe('needle API Angular: JHipster angular generator with blueprint', () => { - before(async () => { - await helpers - .create(getGenerator('angular')) - .withJHipsterConfig({ - skipServer: true, - }) - .withOptions({ - blueprint: ['myblueprint'], - }) - .withGenerators([[mockBlueprintSubGen, { namespace: 'jhipster-myblueprint:angular' }]]) - .run(); - }); - - it('vendor.scss contains the specific change (without comment) added by needle api', () => { - runResult.assertFileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/vendor.scss`, /\n@import style_without_comment;\n/); - }); - - it('global.scss contains the specific change (without comment) added by needle api', () => { - runResult.assertFileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/global.scss`, /\n@import style_without_comment;\n/); - }); - - it('vendor.scss contains the specific change added by needle api', () => { - runResult.assertFileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/vendor.scss`, /\n@import style;\n/); - runResult.assertFileContent( - `${CLIENT_MAIN_SRC_DIR}content/scss/vendor.scss`, - '* ==========================================================================\n' + - 'my comment\n' + - '========================================================================== */\n', - ); - }); - - it('global.scss contains the specific change added by needle api', () => { - runResult.assertFileContent(`${CLIENT_MAIN_SRC_DIR}content/scss/global.scss`, /\n@import style;\n/); - runResult.assertFileContent( - `${CLIENT_MAIN_SRC_DIR}content/scss/global.scss`, - '* ==========================================================================\n' + - 'my comment\n' + - '========================================================================== */\n', - ); - }); - - it('menu contains the element added by needle api', () => { - runResult.assertFileContent( - `${CLIENT_MAIN_SRC_DIR}app/layouts/navbar/navbar.component.html`, - ` - -`, - ); - }); - - it('icon imports contains a new icon added by a new menu method of needle api ', () => { - runResult.assertFileContent(`${CLIENT_MAIN_SRC_DIR}app/config/font-awesome-icons.ts`, ' faIconName1'); - }); - - it('admin routes contains the routing added by needle api', () => { - runResult.assertFileContent( - `${CLIENT_MAIN_SRC_DIR}app/admin/admin.routes.ts`, - ' },\n' + - ' {\n' + - " path: 'entity-audit',\n" + - " data: { pageTitle: 'entityAudit.home.title' },\n" + - " loadChildren: () => import('./entity-audit/entity-audit.module').then(m => m.EntityAuditModule),\n" + - ' },', - ); - }); - - it('app module contains the import and the module added by needle api', () => { - runResult.assertFileContent( - `${CLIENT_MAIN_SRC_DIR}app/app.config.ts`, - "import { appNameangularNameModule } from './folderName/fileName.module';", - ); - runResult.assertFileContent(`${CLIENT_MAIN_SRC_DIR}app/app.config.ts`, 'appNameangularNameModule,'); - }); -});