Skip to content

Commit

Permalink
Merge pull request #67 from razroo/ZETA-7230-codemod-nextjs-lib
Browse files Browse the repository at this point in the history
[ZETA-7230]: Add initial standalone effects and codemods for nextjs
  • Loading branch information
CharlieGreenman authored Dec 5, 2023
2 parents b12a602 + da475bc commit f07bbfa
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/rz/angular/effects/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/', '');

Expand Down
5 changes: 5 additions & 0 deletions src/rz/morph/community-paths/community-paths.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum CommunityPaths {
Angular = 'angular',
React = 'react',
Nextjs = 'nextjs',
Aws = 'aws',
Azure = 'azure',
Gcp = 'gcp',
Expand All @@ -19,6 +20,10 @@ export const supportedCommunityPaths = [
id: 'angular',
displayName: 'Angular',
},
{
id: 'nextjs',
displayName: 'Nextjs',
},
{
id: 'react',
displayName: 'React',
Expand Down
8 changes: 7 additions & 1 deletion src/rz/morph/morph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 []
}
Expand Down
75 changes: 75 additions & 0 deletions src/rz/nextjs/effects/library/nextjs-library.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
39 changes: 39 additions & 0 deletions src/rz/nextjs/effects/library/nextjs-library.ts
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 22 additions & 0 deletions src/rz/nextjs/effects/nextjs-effects.ts
Original file line number Diff line number Diff line change
@@ -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 [];
}
}
Empty file added src/rz/nextjs/index.ts
Empty file.
13 changes: 13 additions & 0 deletions src/rz/nextjs/types/nextjs-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export enum NextjsTypeNames {
Generic = 'generic',
Library = 'library'
}

export enum GlobalNextjsOptionNames {
IsExported = 'isExported'
}

export interface NextjsOptionalType {
name: GlobalNextjsOptionNames,
selected: boolean
}

0 comments on commit f07bbfa

Please sign in to comment.