Browse Source

template type enum added

pull/22645/head
erdemcaygor 1 year ago
parent
commit
4ea28553e1
  1. 21
      npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts
  2. 7
      npm/ng-packs/packages/schematics/src/commands/create-lib/models/generate-lib-schema.ts

21
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 { addLibToWorkspaceFile } from '../../utils/angular-schematic/generate-lib';
import * as cases from '../../utils/text'; import * as cases from '../../utils/text';
import { Exception } from '../../enums/exception'; 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'; import { getMainFilePath } from '../../utils/angular/standalone/util';
export default function (schema: GenerateLibSchema) { export default function (schema: GenerateLibSchema) {
@ -74,13 +74,13 @@ function createLibrary(options: GenerateLibSchema): Rule {
const target = await resolveProject(tree, options.packageName, null); const target = await resolveProject(tree, options.packageName, null);
if (!target || options.override) { if (!target || options.override) {
if (options.isSecondaryEntrypoint) { if (options.isSecondaryEntrypoint) {
if (options.templateType === 'standalone') { if (options.templateType === GenerateLibTemplateType.Standalone) {
return createLibSecondaryEntryWithStandaloneTemplate(tree, options); return createLibSecondaryEntryWithStandaloneTemplate(tree, options);
} else { } else {
return createLibSecondaryEntry(tree, options); return createLibSecondaryEntry(tree, options);
} }
} }
if (options.templateType === 'module') { if (options.templateType === GenerateLibTemplateType.Module) {
return createLibFromModuleTemplate(tree, options); return createLibFromModuleTemplate(tree, options);
} else { } else {
return createLibFromModuleStandaloneTemplate(tree, options); return createLibFromModuleStandaloneTemplate(tree, options);
@ -238,16 +238,16 @@ export function importConfigModuleToDefaultProjectAppModule(
: await hasImportInNgModule( : await hasImportInNgModule(
tree, tree,
projectName, projectName,
options.templateType === 'standalone' options.templateType === GenerateLibTemplateType.Standalone
? `provide${pascal(packageName)}Config` ? `provide${pascal(packageName)}Config`
: `${pascal(packageName)}ConfigModule`, : `${pascal(packageName)}ConfigModule`,
options.templateType === 'standalone' ? 'providers' : 'imports', options.templateType === GenerateLibTemplateType.Standalone ? 'providers' : 'imports',
); );
if (providerAlreadyExists) { if (providerAlreadyExists) {
return; return;
} }
if (options.templateType === 'standalone') { if (options.templateType === GenerateLibTemplateType.Standalone) {
rules.push( rules.push(
addRootProvider(projectName, code => { addRootProvider(projectName, code => {
const configFn = code.external( const configFn = code.external(
@ -299,7 +299,7 @@ export function addRoutingToAppRoutingModule(
const content = buffer.toString(); const content = buffer.toString();
const source = ts.createSourceFile(appRoutesPath, content, ts.ScriptTarget.Latest, true); const source = ts.createSourceFile(appRoutesPath, content, ts.ScriptTarget.Latest, true);
const routeExpr = const routeExpr =
options.templateType === 'standalone' options.templateType === GenerateLibTemplateType.Standalone
? `() => import('${routePath}').then(m => m.${macroName}_ROUTES)` ? `() => import('${routePath}').then(m => m.${macroName}_ROUTES)`
: `() => import('${routePath}').then(m => m.${moduleName}.forLazy())`; : `() => import('${routePath}').then(m => m.${moduleName}.forLazy())`;
const routeToAdd = `{ path: '${routePath}', loadChildren: ${routeExpr} }`; const routeToAdd = `{ path: '${routePath}', loadChildren: ${routeExpr} }`;
@ -322,7 +322,10 @@ export function addRoutingToAppRoutingModule(
return; return;
} }
const appRoutingModuleContent = appRoutingModule.toString(); 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)) { if (appRoutingModuleContent.includes(routeExpr)) {
return; return;
} }
@ -334,7 +337,7 @@ export function addRoutingToAppRoutingModule(
true, true,
); );
const importStatement = const importStatement =
options.templateType === 'standalone' options.templateType === GenerateLibTemplateType.Standalone
? `() => import('${routePath}').then(m => m.${macroName}_ROUTES)` ? `() => import('${routePath}').then(m => m.${macroName}_ROUTES)`
: `() => import('${routePath}').then(m => m.${moduleName}.forLazy())`; : `() => import('${routePath}').then(m => m.${moduleName}.forLazy())`;
const routeDefinition = `{ path: '${routePath}', loadChildren: ${importStatement} }`; const routeDefinition = `{ path: '${routePath}', loadChildren: ${importStatement} }`;

7
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 { export interface GenerateLibSchema {
/** /**
* Angular package name will create * Angular package name will create
@ -11,7 +16,7 @@ export interface GenerateLibSchema {
/** /**
* İs the package has standalone template * İs the package has standalone template
*/ */
templateType: 'standalone' | 'module'; templateType: GenerateLibTemplateType;
override: boolean; override: boolean;

Loading…
Cancel
Save