diff --git a/src/rz/angular/effects/library/library.ts b/src/rz/angular/effects/library/library.ts index b06bf4e..865744a 100644 --- a/src/rz/angular/effects/library/library.ts +++ b/src/rz/angular/effects/library/library.ts @@ -12,12 +12,10 @@ export function libraryEffects(fileEffects: EditFileEffect[], parameters: any): const newFileEffects = []; for(const fileEffect of fileEffects) { const filePath = fileEffect.filePath; - const originFilePath = fileEffect.originFilePath; if(filePath.includes('tsconfig.base.json')) { const projectName = parameters['projectName']; const fileName = filePath.split('/').pop(); const fileToBeAddedTo = fileEffect.content; - const libName = parameters['name']; const libPath = parameters['nameFilePath']; const libPathMinusLibFolder = libPath.replace('libs/', ''); diff --git a/src/rz/morph/community-paths/community-paths.ts b/src/rz/morph/community-paths/community-paths.ts index 6fd68b4..c056046 100644 --- a/src/rz/morph/community-paths/community-paths.ts +++ b/src/rz/morph/community-paths/community-paths.ts @@ -1,6 +1,7 @@ export enum CommunityPaths { Angular = 'angular', React = 'react', + Nextjs = 'nextjs', Aws = 'aws', Azure = 'azure', Gcp = 'gcp', @@ -19,6 +20,10 @@ export const supportedCommunityPaths = [ id: 'angular', displayName: 'Angular', }, + { + id: 'nextjs', + displayName: 'Nextjs', + }, { id: 'react', displayName: 'React', diff --git a/src/rz/morph/morph.ts b/src/rz/morph/morph.ts index ee0cfe1..94638a1 100644 --- a/src/rz/morph/morph.ts +++ b/src/rz/morph/morph.ts @@ -12,6 +12,8 @@ import { angularEffects, angularFilesToAffect, angularStandaloneEffects } from ' import { reactTypes } from '../react'; import { TemplateInputParameter } from '../utils/interfaces/template-parameters'; import { EditFileEffect, NOT_SUPPORTED, NOT_SUPPORTED_TYPE } from './interfaces/morph.interface'; +import { nextjsStandaloneEffects } from '../nextjs/effects/nextjs-effects'; +import { NextjsTypeNames } from '../nextjs/types/nextjs-types'; // takes in singular object and makes all edits to files // used when editing a file @@ -40,8 +42,10 @@ export interface Parameters { // This function happens first and then effects is called export function filesToAffect(filePathWithName: string, fileTree: string[], parameter: TemplateInputParameter, programmingLanguage: string): string[] | NOT_SUPPORTED_TYPE { switch(programmingLanguage) { - case 'angular': + case CommunityPaths.Angular: return angularFilesToAffect(filePathWithName, fileTree, (parameter.type) as AngularTypeNames, (parameter.optionalTypes) as any as AngularOptionalType[]); + case 'angular': + return angularFilesToAffect(filePathWithName, fileTree, (parameter.type) as AngularTypeNames, (parameter.optionalTypes) as any as AngularOptionalType[]); default: return NOT_SUPPORTED; } @@ -57,6 +61,8 @@ export function standaloneEffects(programmingLanguage: string, parameter: Templa switch(programmingLanguage) { case CommunityPaths.Angular: return angularStandaloneEffects((parameter.type) as AngularTypeNames, fileEffects, parameters) + case CommunityPaths.Nextjs: + return nextjsStandaloneEffects((parameter.type) as NextjsTypeNames, fileEffects, parameters) default: return [] } diff --git a/src/rz/nextjs/effects/library/nextjs-library.spec.ts b/src/rz/nextjs/effects/library/nextjs-library.spec.ts new file mode 100644 index 0000000..ca0524a --- /dev/null +++ b/src/rz/nextjs/effects/library/nextjs-library.spec.ts @@ -0,0 +1,75 @@ +import { filesToAffect, standaloneEffects } from "../../../morph"; +import { EditFileEffect } from "../../../morph/interfaces/morph.interface"; +import { NextjsTypeNames } from "../../types/nextjs-types"; +import { returnRootTsConfig } from "./nextjs-library"; + +describe('Nextjs Library', () => { + describe('returnRootTsConfig', () => { + it('should choose closest index file', () => { + const mockFilePath = 'path/to/another/src/hello.service.ts'; + const mockParameter = { + optionalTypes: {}, + type: NextjsTypeNames.Library + } 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(['tsconfig.base.json']); + }); + }); + + describe('libraryEffects', () => { + it('should modify the base tsconfig with the appropriate params', () => { + const programmingLanguage = 'nextjs'; + const mockParameter = { + type: NextjsTypeNames.Library + } as any; + const mockParameters = { + name: 'home', + nameFilePath: 'libs/ui/home', + projectName: 'test-codegen-eleven' + } + + const mockTsConfigBase = { + "compileOnSave": false, + "compilerOptions": { + "paths": { + "@test-codegen-eleven/common/common-ui": ["libs/common/common-ui/src/index.ts"], + } + } + }; + const expectedMockTsConfigBase = { + "compileOnSave": false, + "compilerOptions": { + "paths": { + "@test-codegen-eleven/common/common-ui": ["libs/common/common-ui/src/index.ts"], + "@test-codegen-eleven/ui/home": ["libs/ui/home/src/index.ts"], + } + } + }; + const mockTsConfigBaseStringfied = JSON.stringify(mockTsConfigBase); + const mockExpectedMockTsConfigBaseStringified = JSON.stringify(expectedMockTsConfigBase, null, 2); + const mockFileEffects: EditFileEffect[] = [ + { + filePath: 'tsconfig.base.json', + originFilePath: 'path/to/another/src/hello.component.ts', + content: mockTsConfigBaseStringfied + } + ]; + const result = standaloneEffects(programmingLanguage, mockParameter, mockFileEffects, mockParameters); + const expected = [ + { + filePath: 'tsconfig.base.json', + originFilePath: 'path/to/another/src/hello.component.ts', + content: mockExpectedMockTsConfigBaseStringified + } + ]; + expect(result).toEqual(expected); + }); + }); +}); \ No newline at end of file diff --git a/src/rz/nextjs/effects/library/nextjs-library.ts b/src/rz/nextjs/effects/library/nextjs-library.ts new file mode 100644 index 0000000..a3eb031 --- /dev/null +++ b/src/rz/nextjs/effects/library/nextjs-library.ts @@ -0,0 +1,39 @@ +import { morphJson } from "../../../json/json-morph"; +import { EditFileEffect } from "../../../morph/interfaces/morph.interface"; +import { NextjsOptionalType } from "../../types/nextjs-types"; + +// returns package json as well, so can get package json data +export function returnRootTsConfig(filePathWithName: string, fileTree: string[], optionalTypes: NextjsOptionalType[]): string[] { + return ['tsconfig.base.json']; +} + +// will use the name parameter to get name of library +export function nextJsLibraryEffects(fileEffects: EditFileEffect[], parameters: any): EditFileEffect[] { + const newFileEffects = []; + for(const fileEffect of fileEffects) { + const filePath = fileEffect.filePath; + if(filePath.includes('tsconfig.base.json')) { + const projectName = parameters['projectName']; + const fileName = filePath.split('/').pop(); + const fileToBeAddedTo = fileEffect.content; + const libPath = parameters['nameFilePath']; + const libPathMinusLibFolder = libPath.replace('libs/', ''); + + const editInput: any = { + fileType: 'ts', + fileName: fileName, + filePath: filePath, + fileToBeAddedTo: fileToBeAddedTo, + edits: [{ + nodeType: 'addJsonKeyValue', + valueToModify: `paths`, + codeBlock: {[`@${projectName}/${libPathMinusLibFolder}`]: [`${libPath}/src/index.ts`]} + }] + }; + const updatedFileToBeAddedTo = morphJson(editInput); + fileEffect.content = updatedFileToBeAddedTo; + newFileEffects.push(fileEffect); + } + } + return newFileEffects; +} \ No newline at end of file diff --git a/src/rz/nextjs/effects/nextjs-effects.ts b/src/rz/nextjs/effects/nextjs-effects.ts new file mode 100644 index 0000000..cb9d8c9 --- /dev/null +++ b/src/rz/nextjs/effects/nextjs-effects.ts @@ -0,0 +1,22 @@ +import { NOT_SUPPORTED_TYPE } from "../../morph"; +import { EditFileEffect, NOT_SUPPORTED } from "../../morph/interfaces/morph.interface"; +import { NextjsOptionalType, NextjsTypeNames } from "../types/nextjs-types"; +import { nextJsLibraryEffects, returnRootTsConfig } from "./library/nextjs-library"; + +export function nextjsFilesToAffect(filePathWithName: string, fileTree: string[], type: NextjsTypeNames, optionalTypes: NextjsOptionalType[]): string[] | NOT_SUPPORTED_TYPE { + switch(type) { + case NextjsTypeNames.Library: + return returnRootTsConfig(filePathWithName, fileTree, optionalTypes); + default: + return NOT_SUPPORTED; + } +} + +export function nextjsStandaloneEffects(type: NextjsTypeNames, fileEffects: EditFileEffect[], parameters?: any): EditFileEffect[] { + switch(type) { + case NextjsTypeNames.Library: + return nextJsLibraryEffects(fileEffects, parameters); + default: + return []; + } +} \ No newline at end of file diff --git a/src/rz/nextjs/index.ts b/src/rz/nextjs/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/rz/nextjs/types/nextjs-types.ts b/src/rz/nextjs/types/nextjs-types.ts new file mode 100644 index 0000000..d058a12 --- /dev/null +++ b/src/rz/nextjs/types/nextjs-types.ts @@ -0,0 +1,13 @@ +export enum NextjsTypeNames { + Generic = 'generic', + Library = 'library' +} + +export enum GlobalNextjsOptionNames { + IsExported = 'isExported' +} + +export interface NextjsOptionalType { + name: GlobalNextjsOptionNames, + selected: boolean +} \ No newline at end of file