From 4ea28553e15f5bc097193cfb019b8f3749f01062 Mon Sep 17 00:00:00 2001 From: erdemcaygor Date: Mon, 21 Apr 2025 19:10:10 +0300 Subject: [PATCH] template type enum added --- .../src/commands/create-lib/index.ts | 21 +++++++++++-------- .../create-lib/models/generate-lib-schema.ts | 7 ++++++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts b/npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts index 01eb241dbe..b30a7cc011 100644 --- a/npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts +++ b/npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts @@ -37,7 +37,7 @@ import { ProjectDefinition, WorkspaceDefinition } from '../../utils/angular/work import { addLibToWorkspaceFile } from '../../utils/angular-schematic/generate-lib'; import * as cases from '../../utils/text'; import { Exception } from '../../enums/exception'; -import { GenerateLibSchema } from './models/generate-lib-schema'; +import { GenerateLibSchema, GenerateLibTemplateType } from './models/generate-lib-schema'; import { getMainFilePath } from '../../utils/angular/standalone/util'; export default function (schema: GenerateLibSchema) { @@ -74,13 +74,13 @@ function createLibrary(options: GenerateLibSchema): Rule { const target = await resolveProject(tree, options.packageName, null); if (!target || options.override) { if (options.isSecondaryEntrypoint) { - if (options.templateType === 'standalone') { + if (options.templateType === GenerateLibTemplateType.Standalone) { return createLibSecondaryEntryWithStandaloneTemplate(tree, options); } else { return createLibSecondaryEntry(tree, options); } } - if (options.templateType === 'module') { + if (options.templateType === GenerateLibTemplateType.Module) { return createLibFromModuleTemplate(tree, options); } else { return createLibFromModuleStandaloneTemplate(tree, options); @@ -238,16 +238,16 @@ export function importConfigModuleToDefaultProjectAppModule( : await hasImportInNgModule( tree, projectName, - options.templateType === 'standalone' + options.templateType === GenerateLibTemplateType.Standalone ? `provide${pascal(packageName)}Config` : `${pascal(packageName)}ConfigModule`, - options.templateType === 'standalone' ? 'providers' : 'imports', + options.templateType === GenerateLibTemplateType.Standalone ? 'providers' : 'imports', ); if (providerAlreadyExists) { return; } - if (options.templateType === 'standalone') { + if (options.templateType === GenerateLibTemplateType.Standalone) { rules.push( addRootProvider(projectName, code => { const configFn = code.external( @@ -299,7 +299,7 @@ export function addRoutingToAppRoutingModule( const content = buffer.toString(); const source = ts.createSourceFile(appRoutesPath, content, ts.ScriptTarget.Latest, true); const routeExpr = - options.templateType === 'standalone' + options.templateType === GenerateLibTemplateType.Standalone ? `() => import('${routePath}').then(m => m.${macroName}_ROUTES)` : `() => import('${routePath}').then(m => m.${moduleName}.forLazy())`; const routeToAdd = `{ path: '${routePath}', loadChildren: ${routeExpr} }`; @@ -322,7 +322,10 @@ export function addRoutingToAppRoutingModule( return; } const appRoutingModuleContent = appRoutingModule.toString(); - const routeExpr = options.templateType === 'standalone' ? `${macroName}_ROUTES` : moduleName; + const routeExpr = + options.templateType === GenerateLibTemplateType.Standalone + ? `${macroName}_ROUTES` + : moduleName; if (appRoutingModuleContent.includes(routeExpr)) { return; } @@ -334,7 +337,7 @@ export function addRoutingToAppRoutingModule( true, ); const importStatement = - options.templateType === 'standalone' + options.templateType === GenerateLibTemplateType.Standalone ? `() => import('${routePath}').then(m => m.${macroName}_ROUTES)` : `() => import('${routePath}').then(m => m.${moduleName}.forLazy())`; const routeDefinition = `{ path: '${routePath}', loadChildren: ${importStatement} }`; diff --git a/npm/ng-packs/packages/schematics/src/commands/create-lib/models/generate-lib-schema.ts b/npm/ng-packs/packages/schematics/src/commands/create-lib/models/generate-lib-schema.ts index a3a9314abe..aca9f08e49 100644 --- a/npm/ng-packs/packages/schematics/src/commands/create-lib/models/generate-lib-schema.ts +++ b/npm/ng-packs/packages/schematics/src/commands/create-lib/models/generate-lib-schema.ts @@ -1,3 +1,8 @@ +export enum GenerateLibTemplateType { + Standalone = 'standalone', + Module = 'module', +} + export interface GenerateLibSchema { /** * Angular package name will create @@ -11,7 +16,7 @@ export interface GenerateLibSchema { /** * İs the package has standalone template */ - templateType: 'standalone' | 'module'; + templateType: GenerateLibTemplateType; override: boolean;