From 35d1f36536d350457d5fdc787684800632a437df Mon Sep 17 00:00:00 2001 From: Dcode22 Date: Thu, 28 Sep 2023 10:26:08 +0300 Subject: [PATCH] added directive standalone effects --- src/rz/angular/angular-effects.ts | 6 ++- .../effects/directive/directive.spec.ts | 37 ++++++++++++++++++- src/rz/angular/effects/directive/directive.ts | 6 +++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/rz/angular/angular-effects.ts b/src/rz/angular/angular-effects.ts index ac8c5af..2def41c 100644 --- a/src/rz/angular/angular-effects.ts +++ b/src/rz/angular/angular-effects.ts @@ -1,7 +1,7 @@ import { Parameters } from "../morph"; import { EditFileEffect, NOT_SUPPORTED, NOT_SUPPORTED_TYPE } from "../morph/interfaces/morph.interface"; import { addClassToDeclarationsAndImports, componentEffects, fileToAddClassToDeclarationsAndImports } from "./effects/component/component-effects"; -import { exportDirectiveFile } from "./effects/directive/directive"; +import { directiveEffects, exportDirectiveFile } from "./effects/directive/directive"; import { exportGraphqlFile } from "./effects/graphql/graphql"; import { exportInterfaceFile } from "./effects/interface/interface"; import { addEffectToNgModule } from "./effects/ngrx/effects/ngrx-effects"; @@ -25,6 +25,8 @@ export function angularFilesToAffect(filePathWithName: string, fileTree: string[ return closestIndexFileToImportTo(filePathWithName, fileTree, optionalTypes) case AngularTypeNames.Pipe: return closestIndexFileToImportTo(filePathWithName, fileTree, optionalTypes) + case AngularTypeNames.Directive: + return closestIndexFileToImportTo(filePathWithName, fileTree, optionalTypes) default: return NOT_SUPPORTED; } @@ -38,6 +40,8 @@ export function angularStandaloneEffects(type: AngularTypeNames, fileEffects: Ed return standaloneComponentEffects(fileEffects); case AngularTypeNames.Service: return serviceEffects(fileEffects); + case AngularTypeNames.Directive: + return directiveEffects(fileEffects); case AngularTypeNames.Pipe: return pipeEffects(fileEffects); case AngularTypeNames.Library: diff --git a/src/rz/angular/effects/directive/directive.spec.ts b/src/rz/angular/effects/directive/directive.spec.ts index 79df579..0c8c092 100644 --- a/src/rz/angular/effects/directive/directive.spec.ts +++ b/src/rz/angular/effects/directive/directive.spec.ts @@ -1,7 +1,9 @@ import { TemplateInputParameter } from './../../../utils/interfaces/template-parameters'; import { writeFileSync, readFileSync } from 'fs'; -import { effects } from "../../../morph"; +import { effects, filesToAffect } from "../../../morph"; import { AngularTypeNames } from "../../types/types"; +import { directiveEffects } from './directive'; +import { EditFileEffect } from '../../../morph/interfaces/morph.interface'; describe('exportDirectiveFile', () => { afterEach(() => { @@ -24,4 +26,37 @@ describe('exportDirectiveFile', () => { expect(result).toEqual(expected); }); + + it('should trigger directive effects', () => { + const mockFileEffects: EditFileEffect[] = [{ + filePath: 'path/to/another/index.ts', + originFilePath: 'path/to/another/src/hello.component.ts', + content: `` + }]; + const result = directiveEffects(mockFileEffects); + const indexContent = `export * from "./src/hello.component"; +`; + expect(result).toEqual([{ + content: indexContent, + originFilePath: "path/to/another/src/hello.component.ts", + filePath: 'path/to/another/index.ts' + }]); + }); + + it('should choose closest index file', () => { + const mockFilePath = 'path/to/another/src/hello.directive.ts'; + const mockParameter = { + optionalTypes: {}, + type: AngularTypeNames.Directive + } as any; + + const fileTree = [ + "path/to/another/src", + "path/to/another/src/hello.component.ts", + "path/to/another/index.ts", + "path/to/another" + ]; + const fileToModify = filesToAffect(mockFilePath, fileTree, mockParameter, 'angular'); + expect(fileToModify).toEqual(['path/to/another/index.ts']); + }); }); \ No newline at end of file diff --git a/src/rz/angular/effects/directive/directive.ts b/src/rz/angular/effects/directive/directive.ts index 2490a36..a229ac6 100644 --- a/src/rz/angular/effects/directive/directive.ts +++ b/src/rz/angular/effects/directive/directive.ts @@ -1,7 +1,13 @@ +import { EditFileEffect } from "../../../morph/interfaces/morph.interface"; import { exportTsFiles, isTsFile } from "../../../utils/add-export"; +import { exportInIndex } from "../../export-in-index"; export function exportDirectiveFile(filePathWithName: string): void { if(isTsFile(filePathWithName)) { exportTsFiles(filePathWithName); } +} + +export function directiveEffects(fileEffects: EditFileEffect[]): EditFileEffect[]{ + return exportInIndex(fileEffects) } \ No newline at end of file