-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend)file generate function specific place (#71)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a new test suite for path utilities, validating various path-related functionalities. - Added a utility function to save generated code to a specified file path. - Introduced a utility function to extract JSON from Markdown content. - Registered the `FileGeneratorHandler` for file generation processes. - **Bug Fixes** - Enhanced error handling for file writing and JSON extraction processes. - **Chores** - Updated import statements for utility modules to improve code organization. - Removed deprecated utility class related to JSON extraction. - Modified import style for path and os modules in the common-path configuration. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Jackson Chen <[email protected]> Co-authored-by: Jackson Chen <[email protected]>
- Loading branch information
1 parent
dfe754a
commit 418a3a8
Showing
8 changed files
with
163 additions
and
40 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
backend/src/build-system/__tests__/test-file-create-and-path.spec.ts
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,86 @@ | ||
import * as path from 'path'; | ||
import * as os from 'os'; | ||
import { existsSync, rmdirSync } from 'fs-extra'; | ||
import * as pathUtil from '../../config/common-path'; | ||
import { saveGeneratedCode } from 'src/build-system/utils/files'; | ||
import { | ||
getRootDir, | ||
getProjectsDir, | ||
getProjectPath, | ||
} from 'src/config/common-path'; | ||
|
||
describe('Path Utilities', () => { | ||
const APP_NAME = 'codefox'; | ||
const ROOT_DIR = path.join(os.homedir(), `.${APP_NAME}`); | ||
|
||
const cleanUp = () => { | ||
// if (existsSync(ROOT_DIR)) { | ||
// rmdirSync(ROOT_DIR, { recursive: true }); | ||
// } | ||
}; | ||
|
||
beforeEach(() => { | ||
cleanUp(); | ||
}); | ||
|
||
afterAll(() => { | ||
cleanUp(); | ||
}); | ||
|
||
it('should return a valid root directory', () => { | ||
const rootDir = getRootDir(); | ||
expect(rootDir).toBeDefined(); | ||
expect(rootDir).toContain('.codefox'); | ||
}); | ||
|
||
it('should return a valid projects directory', () => { | ||
const projectsDir = getProjectsDir(); | ||
expect(projectsDir).toBeDefined(); | ||
expect(projectsDir).toContain('projects'); | ||
}); | ||
|
||
it('should return a valid project path for a given ID', () => { | ||
const projectId = 'test-project'; | ||
const projectPath = getProjectPath(projectId); | ||
expect(projectPath).toBeDefined(); | ||
expect(projectPath).toContain(projectId); | ||
}); | ||
|
||
it('should resolve paths correctly', () => { | ||
const rootDir = pathUtil.getRootDir(); | ||
const projectsDir = pathUtil.getProjectsDir(); | ||
expect(rootDir).toBeDefined(); | ||
expect(projectsDir).toBeDefined(); | ||
}); | ||
|
||
it('should create and return the root directory', async () => { | ||
const rootDir = pathUtil.getRootDir(); | ||
|
||
await generateAndSaveCode(); | ||
expect(rootDir).toBe(ROOT_DIR); | ||
expect(existsSync(ROOT_DIR)).toBe(true); | ||
}); | ||
}); | ||
|
||
async function generateAndSaveCode() { | ||
const generatedCode = ` | ||
import { Controller, Get } from '@nestjs/common'; | ||
@Controller('example') | ||
export class ExampleController { | ||
@Get() | ||
getHello(): string { | ||
return 'Hello World!'; | ||
} | ||
} | ||
`; | ||
|
||
const fileName = 'example.controller.ts'; | ||
|
||
try { | ||
const filePath = await saveGeneratedCode(fileName, generatedCode); | ||
console.log(`Generated code saved at: ${filePath}`); | ||
} catch (error) { | ||
console.error('Failed to save generated code:', error); | ||
} | ||
} |
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
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,26 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import fs from 'fs-extra'; | ||
|
||
const logger = new Logger('file-utils'); | ||
/** | ||
* Saves the given content to the specified file path using fs-extra. | ||
* Ensures that all directories in the path exist before writing the file. | ||
* | ||
* @param filePath - The complete file path including the file name. | ||
* @param content - The content to be written to the file. | ||
* @returns The file path where the content was written. | ||
* @throws Will throw an error if the file could not be written. | ||
*/ | ||
export async function saveGeneratedCode( | ||
filePath: string, | ||
content: string, | ||
): Promise<string> { | ||
try { | ||
// fs-extra's outputFile creates all directories if they don't exist | ||
await fs.outputFile(filePath, content, 'utf8'); | ||
return filePath; | ||
} catch (error) { | ||
logger.error('Error saving generated code:', error); | ||
throw error; | ||
} | ||
} |
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,26 @@ | ||
import { Logger } from '@nestjs/common'; | ||
|
||
const logger = new Logger('common-utils'); | ||
|
||
/** | ||
* Extract JSON data from Markdown content. | ||
* @param markdownContent The Markdown content containing the JSON. | ||
*/ | ||
export function extractJsonFromMarkdown(markdownContent: string): { | ||
files: Record<string, { dependsOn: string[] }>; | ||
} { | ||
const jsonMatch = /<GENERATEDCODE>([\s\S]*?)<\/GENERATEDCODE>/m.exec( | ||
markdownContent, | ||
); | ||
if (!jsonMatch) { | ||
logger.error('No JSON found in the provided Markdown content.'); | ||
return null; | ||
} | ||
|
||
try { | ||
return JSON.parse(jsonMatch[1]); | ||
} catch (error) { | ||
logger.error('Invalid JSON format in the Markdown content: ' + error); | ||
return null; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.