diff --git a/backend/src/build-system/__tests__/test.copy-project-template.spec.ts b/backend/src/build-system/__tests__/test.copy-project-template.spec.ts new file mode 100644 index 00000000..0b59c934 --- /dev/null +++ b/backend/src/build-system/__tests__/test.copy-project-template.spec.ts @@ -0,0 +1,17 @@ +import { copyProjectTemplate } from 'src/build-system/utils/files'; +import { promises as fs } from 'fs'; +import { v4 as uuidv4 } from 'uuid'; +import { getTemplatePath } from 'src/config/common-path'; +import { Logger } from '@nestjs/common'; + +describe('Copy Project Template', () => { + it('should copy the template to the specified UUID folder', async () => { + const templatePath = getTemplatePath('template-backend'); + const projectUUID = uuidv4(); + + Logger.log('template-path:', templatePath); + const projectPath = await copyProjectTemplate(templatePath, projectUUID); + expect(await fs.access(projectPath)).toBeUndefined(); // Project folder exists + await fs.rm(projectPath, { recursive: true, force: true }); + }); +}); diff --git a/backend/src/build-system/handlers/project-init.ts b/backend/src/build-system/handlers/project-init.ts index 88b12746..9a50b63d 100644 --- a/backend/src/build-system/handlers/project-init.ts +++ b/backend/src/build-system/handlers/project-init.ts @@ -1,12 +1,15 @@ import { BuilderContext } from '../context'; import { BuildHandlerManager } from '../hanlder-manager'; import { BuildHandler, BuildResult } from '../types'; +import { Logger } from '@nestjs/common'; export class ProjectInitHandler implements BuildHandler { readonly id = 'op:PROJECT::STATE:SETUP'; + private readonly logger = new Logger('ProjectInitHandler'); async run(context: BuilderContext): Promise { - console.log('Setting up project...'); + this.logger.log('Setting up project...'); + const result = { projectName: 'online shoping', descreption: 'sell products', @@ -14,6 +17,7 @@ export class ProjectInitHandler implements BuildHandler { path: '/path/to/project', }; context.setGlobalContext('projectConfig', result); + return { success: true, data: result, diff --git a/backend/src/build-system/utils/files.ts b/backend/src/build-system/utils/files.ts index 3a72318a..8ae2fc92 100644 --- a/backend/src/build-system/utils/files.ts +++ b/backend/src/build-system/utils/files.ts @@ -1,5 +1,6 @@ import { Logger } from '@nestjs/common'; -import fs from 'fs-extra'; +import * as fs from 'fs-extra'; +import { getProjectPath } from 'src/config/common-path'; const logger = new Logger('file-utils'); /** @@ -24,3 +25,42 @@ export async function saveGeneratedCode( throw error; } } + +/** + * Copies a project template to a specific location under a given UUID folder. + * + * @param templatePath - The path to the project template containing files. + * @param projectUUID - The UUID of the project folder. + * @returns The path to the copied project directory. + */ +export async function copyProjectTemplate( + templatePath: string, + projectUUID: string, +): Promise { + try { + // Validate the template path + const templateExists = await fs + .access(templatePath) + .then(() => true) + .catch(() => false); + if (!templateExists) { + throw new Error(`Template path does not exist: ${templatePath}`); + } + + // Resolve the destination path and ensure path exist + const destinationPath = getProjectPath(projectUUID); + + // Copy the template to the destination + logger.log( + `Copying template from ${templatePath} to ${destinationPath}...`, + ); + await fs.copy(templatePath, destinationPath); + + // Return the destination path + logger.log(`Template copied successfully to ${destinationPath}`); + return destinationPath; + } catch (error) { + logger.error('Error copying project template:', error); + throw error; + } +} diff --git a/backend/src/config/common-path.ts b/backend/src/config/common-path.ts index b51d58e8..6de9809b 100644 --- a/backend/src/config/common-path.ts +++ b/backend/src/config/common-path.ts @@ -1,12 +1,15 @@ import * as path from 'path'; -import * as os from 'os'; import { existsSync, mkdirSync, promises } from 'fs-extra'; -import { createHash } from 'crypto'; // Constants for base directories const APP_NAME = 'codefox'; -const ROOT_DIR = path.join(os.homedir(), `.${APP_NAME}`); +// TODO: hack way to get the root directory of the workspace +const WORKSPACE_ROOT = path.resolve(__dirname, '../../../'); +const ROOT_DIR = path.join(WORKSPACE_ROOT, `.${APP_NAME}`); +export const TEMPLATE_PATH = path.join(WORKSPACE_ROOT, 'backend/template'); +export const getTemplatePath = (templateName: string): string => + path.join(TEMPLATE_PATH, templateName); // Utility function to ensure a directory exists const ensureDir = (dirPath: string): string => { if (!existsSync(dirPath)) { diff --git a/frontend/src/config/common-path.ts b/frontend/src/config/common-path.ts index 40ca5559..ed989604 100644 --- a/frontend/src/config/common-path.ts +++ b/frontend/src/config/common-path.ts @@ -1,4 +1,4 @@ -import path from 'path'; +import * as path from 'path'; import { existsSync, mkdirSync, promises as fsPromises } from 'fs-extra'; import { createHash } from 'crypto';