-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from razroo/ZETA-7230-codemod-nextjs-lib
[ZETA-7230]: Add initial standalone effects and codemods for nextjs
- Loading branch information
Showing
8 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |