Skip to content

Commit f07bbfa

Browse files
Merge pull request #67 from razroo/ZETA-7230-codemod-nextjs-lib
[ZETA-7230]: Add initial standalone effects and codemods for nextjs
2 parents b12a602 + da475bc commit f07bbfa

File tree

8 files changed

+161
-3
lines changed

8 files changed

+161
-3
lines changed

src/rz/angular/effects/library/library.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ export function libraryEffects(fileEffects: EditFileEffect[], parameters: any):
1212
const newFileEffects = [];
1313
for(const fileEffect of fileEffects) {
1414
const filePath = fileEffect.filePath;
15-
const originFilePath = fileEffect.originFilePath;
1615
if(filePath.includes('tsconfig.base.json')) {
1716
const projectName = parameters['projectName'];
1817
const fileName = filePath.split('/').pop();
1918
const fileToBeAddedTo = fileEffect.content;
20-
const libName = parameters['name'];
2119
const libPath = parameters['nameFilePath'];
2220
const libPathMinusLibFolder = libPath.replace('libs/', '');
2321

src/rz/morph/community-paths/community-paths.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export enum CommunityPaths {
22
Angular = 'angular',
33
React = 'react',
4+
Nextjs = 'nextjs',
45
Aws = 'aws',
56
Azure = 'azure',
67
Gcp = 'gcp',
@@ -19,6 +20,10 @@ export const supportedCommunityPaths = [
1920
id: 'angular',
2021
displayName: 'Angular',
2122
},
23+
{
24+
id: 'nextjs',
25+
displayName: 'Nextjs',
26+
},
2227
{
2328
id: 'react',
2429
displayName: 'React',

src/rz/morph/morph.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { angularEffects, angularFilesToAffect, angularStandaloneEffects } from '
1212
import { reactTypes } from '../react';
1313
import { TemplateInputParameter } from '../utils/interfaces/template-parameters';
1414
import { EditFileEffect, NOT_SUPPORTED, NOT_SUPPORTED_TYPE } from './interfaces/morph.interface';
15+
import { nextjsStandaloneEffects } from '../nextjs/effects/nextjs-effects';
16+
import { NextjsTypeNames } from '../nextjs/types/nextjs-types';
1517

1618
// takes in singular object and makes all edits to files
1719
// used when editing a file
@@ -40,8 +42,10 @@ export interface Parameters {
4042
// This function happens first and then effects is called
4143
export function filesToAffect(filePathWithName: string, fileTree: string[], parameter: TemplateInputParameter, programmingLanguage: string): string[] | NOT_SUPPORTED_TYPE {
4244
switch(programmingLanguage) {
43-
case 'angular':
45+
case CommunityPaths.Angular:
4446
return angularFilesToAffect(filePathWithName, fileTree, (parameter.type) as AngularTypeNames, (parameter.optionalTypes) as any as AngularOptionalType[]);
47+
case 'angular':
48+
return angularFilesToAffect(filePathWithName, fileTree, (parameter.type) as AngularTypeNames, (parameter.optionalTypes) as any as AngularOptionalType[]);
4549
default:
4650
return NOT_SUPPORTED;
4751
}
@@ -57,6 +61,8 @@ export function standaloneEffects(programmingLanguage: string, parameter: Templa
5761
switch(programmingLanguage) {
5862
case CommunityPaths.Angular:
5963
return angularStandaloneEffects((parameter.type) as AngularTypeNames, fileEffects, parameters)
64+
case CommunityPaths.Nextjs:
65+
return nextjsStandaloneEffects((parameter.type) as NextjsTypeNames, fileEffects, parameters)
6066
default:
6167
return []
6268
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { filesToAffect, standaloneEffects } from "../../../morph";
2+
import { EditFileEffect } from "../../../morph/interfaces/morph.interface";
3+
import { NextjsTypeNames } from "../../types/nextjs-types";
4+
import { returnRootTsConfig } from "./nextjs-library";
5+
6+
describe('Nextjs Library', () => {
7+
describe('returnRootTsConfig', () => {
8+
it('should choose closest index file', () => {
9+
const mockFilePath = 'path/to/another/src/hello.service.ts';
10+
const mockParameter = {
11+
optionalTypes: {},
12+
type: NextjsTypeNames.Library
13+
} as any;
14+
15+
const fileTree = [
16+
"path/to/another/src",
17+
"path/to/another/src/hello.component.ts",
18+
"path/to/another/index.ts",
19+
"path/to/another"
20+
];
21+
const fileToModify = filesToAffect(mockFilePath, fileTree, mockParameter, 'angular');
22+
expect(fileToModify).toEqual(['tsconfig.base.json']);
23+
});
24+
});
25+
26+
describe('libraryEffects', () => {
27+
it('should modify the base tsconfig with the appropriate params', () => {
28+
const programmingLanguage = 'nextjs';
29+
const mockParameter = {
30+
type: NextjsTypeNames.Library
31+
} as any;
32+
const mockParameters = {
33+
name: 'home',
34+
nameFilePath: 'libs/ui/home',
35+
projectName: 'test-codegen-eleven'
36+
}
37+
38+
const mockTsConfigBase = {
39+
"compileOnSave": false,
40+
"compilerOptions": {
41+
"paths": {
42+
"@test-codegen-eleven/common/common-ui": ["libs/common/common-ui/src/index.ts"],
43+
}
44+
}
45+
};
46+
const expectedMockTsConfigBase = {
47+
"compileOnSave": false,
48+
"compilerOptions": {
49+
"paths": {
50+
"@test-codegen-eleven/common/common-ui": ["libs/common/common-ui/src/index.ts"],
51+
"@test-codegen-eleven/ui/home": ["libs/ui/home/src/index.ts"],
52+
}
53+
}
54+
};
55+
const mockTsConfigBaseStringfied = JSON.stringify(mockTsConfigBase);
56+
const mockExpectedMockTsConfigBaseStringified = JSON.stringify(expectedMockTsConfigBase, null, 2);
57+
const mockFileEffects: EditFileEffect[] = [
58+
{
59+
filePath: 'tsconfig.base.json',
60+
originFilePath: 'path/to/another/src/hello.component.ts',
61+
content: mockTsConfigBaseStringfied
62+
}
63+
];
64+
const result = standaloneEffects(programmingLanguage, mockParameter, mockFileEffects, mockParameters);
65+
const expected = [
66+
{
67+
filePath: 'tsconfig.base.json',
68+
originFilePath: 'path/to/another/src/hello.component.ts',
69+
content: mockExpectedMockTsConfigBaseStringified
70+
}
71+
];
72+
expect(result).toEqual(expected);
73+
});
74+
});
75+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { morphJson } from "../../../json/json-morph";
2+
import { EditFileEffect } from "../../../morph/interfaces/morph.interface";
3+
import { NextjsOptionalType } from "../../types/nextjs-types";
4+
5+
// returns package json as well, so can get package json data
6+
export function returnRootTsConfig(filePathWithName: string, fileTree: string[], optionalTypes: NextjsOptionalType[]): string[] {
7+
return ['tsconfig.base.json'];
8+
}
9+
10+
// will use the name parameter to get name of library
11+
export function nextJsLibraryEffects(fileEffects: EditFileEffect[], parameters: any): EditFileEffect[] {
12+
const newFileEffects = [];
13+
for(const fileEffect of fileEffects) {
14+
const filePath = fileEffect.filePath;
15+
if(filePath.includes('tsconfig.base.json')) {
16+
const projectName = parameters['projectName'];
17+
const fileName = filePath.split('/').pop();
18+
const fileToBeAddedTo = fileEffect.content;
19+
const libPath = parameters['nameFilePath'];
20+
const libPathMinusLibFolder = libPath.replace('libs/', '');
21+
22+
const editInput: any = {
23+
fileType: 'ts',
24+
fileName: fileName,
25+
filePath: filePath,
26+
fileToBeAddedTo: fileToBeAddedTo,
27+
edits: [{
28+
nodeType: 'addJsonKeyValue',
29+
valueToModify: `paths`,
30+
codeBlock: {[`@${projectName}/${libPathMinusLibFolder}`]: [`${libPath}/src/index.ts`]}
31+
}]
32+
};
33+
const updatedFileToBeAddedTo = morphJson(editInput);
34+
fileEffect.content = updatedFileToBeAddedTo;
35+
newFileEffects.push(fileEffect);
36+
}
37+
}
38+
return newFileEffects;
39+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { NOT_SUPPORTED_TYPE } from "../../morph";
2+
import { EditFileEffect, NOT_SUPPORTED } from "../../morph/interfaces/morph.interface";
3+
import { NextjsOptionalType, NextjsTypeNames } from "../types/nextjs-types";
4+
import { nextJsLibraryEffects, returnRootTsConfig } from "./library/nextjs-library";
5+
6+
export function nextjsFilesToAffect(filePathWithName: string, fileTree: string[], type: NextjsTypeNames, optionalTypes: NextjsOptionalType[]): string[] | NOT_SUPPORTED_TYPE {
7+
switch(type) {
8+
case NextjsTypeNames.Library:
9+
return returnRootTsConfig(filePathWithName, fileTree, optionalTypes);
10+
default:
11+
return NOT_SUPPORTED;
12+
}
13+
}
14+
15+
export function nextjsStandaloneEffects(type: NextjsTypeNames, fileEffects: EditFileEffect[], parameters?: any): EditFileEffect[] {
16+
switch(type) {
17+
case NextjsTypeNames.Library:
18+
return nextJsLibraryEffects(fileEffects, parameters);
19+
default:
20+
return [];
21+
}
22+
}

src/rz/nextjs/index.ts

Whitespace-only changes.

src/rz/nextjs/types/nextjs-types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export enum NextjsTypeNames {
2+
Generic = 'generic',
3+
Library = 'library'
4+
}
5+
6+
export enum GlobalNextjsOptionNames {
7+
IsExported = 'isExported'
8+
}
9+
10+
export interface NextjsOptionalType {
11+
name: GlobalNextjsOptionNames,
12+
selected: boolean
13+
}

0 commit comments

Comments
 (0)