From 149e41ff66dba0973daf6e7c6650a57709c95631 Mon Sep 17 00:00:00 2001 From: ZHallen122 Date: Wed, 30 Oct 2024 14:31:38 -0400 Subject: [PATCH 1/4] update common path --- backend/src/config/common-path.ts | 299 ++++++++++++++---------------- 1 file changed, 142 insertions(+), 157 deletions(-) diff --git a/backend/src/config/common-path.ts b/backend/src/config/common-path.ts index 173274f1..f765ab24 100644 --- a/backend/src/config/common-path.ts +++ b/backend/src/config/common-path.ts @@ -1,161 +1,146 @@ import path from 'path'; import os from 'os'; -import { name } from '../../package.json'; import { existsSync, mkdirSync, promises } from 'fs-extra'; - -export class CodeFoxPaths { - private static readonly APP_NAME = name; - private static readonly ROOT_DIR = path.join( - os.homedir(), - `.${CodeFoxPaths.APP_NAME}`, +import { createHash } from 'crypto'; + +// Constants for base directories +const APP_NAME = 'codefox'; +const ROOT_DIR = path.join(os.homedir(), `.${APP_NAME}`); +const CLIENT_CACHE_DIR = path.resolve( + __dirname, + '../../frontend/.codefox-client/.cache', +); + +// Utility function to ensure a directory exists +const ensureDir = (dirPath: string): string => { + if (!existsSync(dirPath)) { + mkdirSync(dirPath, { recursive: true }); + } + return dirPath; +}; + +// Root Directory Accessor +export const getRootDir = (): string => ensureDir(ROOT_DIR); + +// Configuration Paths +export const getConfigDir = (): string => + ensureDir(path.join(getRootDir(), 'config')); +export const getConfigPath = (configName: string): string => + path.join(getConfigDir(), `${configName}.json`); + +// Models Directory +export const getModelsDir = (): string => + ensureDir(path.join(getRootDir(), 'models')); +export const getModelPath = (modelName: string): string => + path.join(getModelsDir(), modelName); + +// Project-Specific Paths +export const getProjectsDir = (): string => + ensureDir(path.join(getRootDir(), 'projects')); +export const getProjectPath = (projectId: string): string => + ensureDir(path.join(getProjectsDir(), projectId)); +export const getProjectSourceDir = (projectId: string): string => + ensureDir(path.join(getProjectPath(projectId), 'src')); +export const getProjectGeneratedDir = (projectId: string): string => + ensureDir(path.join(getProjectPath(projectId), 'generated')); +export const getProjectTestsDir = (projectId: string): string => + ensureDir(path.join(getProjectPath(projectId), 'tests')); + +// Database Paths +export const getDatabaseDir = (): string => + ensureDir(path.join(getRootDir(), 'data')); +export const getDatabasePath = (): string => + path.join(getDatabaseDir(), 'codefox.db'); + +// Vector Database (INDEX) Path +export const getIndexDir = (): string => + ensureDir(path.join(getRootDir(), 'INDEX')); +export const getIndexFilePath = (indexFileName: string): string => + path.join(getIndexDir(), indexFileName); + +// Temporary files +export const getTempDir = (): string => { + const tempDir = path.join(ROOT_DIR, '.codefox', 'temp'); + if (!existsSync(tempDir)) { + mkdirSync(tempDir, { recursive: true }); + } + return tempDir; +}; + +// Client Cache Paths +export const getCacheDir = (): string => ensureDir(CLIENT_CACHE_DIR); +export const getUserCacheDir = (userId: string): string => + ensureDir(path.join(CLIENT_CACHE_DIR, hashUserId(userId))); +export const getProjectCacheDir = (userId: string, projectId: string): string => + ensureDir(path.join(getUserCacheDir(userId), projectId)); + +// Access or create content file within a project’s cache directory +export const getProjectContentPath = ( + userId: string, + projectId: string, + fileName: string, +): string => { + return path.join(getProjectCacheDir(userId, projectId), fileName); +}; + +// Utility functions to read and write content files in the project cache directory +export const writeProjectContent = async ( + userId: string, + projectId: string, + fileName: string, + content: string, +): Promise => { + const contentPath = getProjectContentPath(userId, projectId, fileName); + await promises.writeFile(contentPath, content, 'utf8'); +}; + +export const readProjectContent = async ( + userId: string, + projectId: string, + fileName: string, +): Promise => { + const contentPath = getProjectContentPath(userId, projectId, fileName); + return promises.readFile(contentPath, 'utf8'); +}; + +export const deleteProjectContent = async ( + userId: string, + projectId: string, + fileName: string, +): Promise => { + const contentPath = getProjectContentPath(userId, projectId, fileName); + if (existsSync(contentPath)) { + await promises.unlink(contentPath); + } +}; + +// Helper function to hash user IDs +const hashUserId = (userId: string): string => { + return createHash('md5').update(userId).digest('hex'); +}; + +// Utility Functions +export const exists = (filePath: string): boolean => existsSync(filePath); + +export const cleanTempDir = async (): Promise => { + const tempDir = getTempDir(); + const files = await promises.readdir(tempDir); + await Promise.all( + files.map((file) => promises.unlink(path.join(tempDir, file))), ); - - /** - * Internal helper to ensure a directory exists before returning its path - * @param dirPath The directory path to check/create - * @returns The same directory path - */ - private static ensureDir(dirPath: string): string { - if (!existsSync(path.dirname(dirPath))) { - this.ensureDir(path.dirname(dirPath)); - } - if (!existsSync(dirPath)) { - mkdirSync(dirPath, { recursive: true }); - } - return dirPath; - } - - /** - * Root Directory - */ - public static getRootDir(): string { - return this.ensureDir(CodeFoxPaths.ROOT_DIR); - } - - /** - * Models Directory - */ - public static getModelsDir(): string { - return this.ensureDir(path.join(this.getRootDir(), 'models')); - } - - public static getModelPath(modelName: string): string { - return path.join(this.getModelsDir(), modelName); - } - - /** - * Projects Directory - */ - public static getProjectsDir(): string { - return this.ensureDir(path.join(this.getRootDir(), 'projects')); - } - - public static getProjectPath(projectId: string): string { - return this.ensureDir(path.join(this.getProjectsDir(), projectId)); - } - - public static getProjectSourceDir(projectId: string): string { - return this.ensureDir(path.join(this.getProjectPath(projectId), 'src')); - } - - public static getProjectGeneratedDir(projectId: string): string { - return this.ensureDir( - path.join(this.getProjectPath(projectId), 'generated'), - ); - } - - public static getProjectTestsDir(projectId: string): string { - return this.ensureDir(path.join(this.getProjectPath(projectId), 'tests')); - } - - /** - * Database - */ - public static getDatabaseDir(): string { - return this.ensureDir(path.join(this.getRootDir(), 'data')); - } - - public static getDatabasePath(): string { - this.getDatabaseDir(); // Ensure database directory exists - return path.join(this.getDatabaseDir(), 'codefox.db'); - } - - /** - * Configuration - */ - public static getConfigDir(): string { - return this.ensureDir(path.join(this.getRootDir(), 'config')); - } - - public static getConfigPath(configName: string): string { - this.getConfigDir(); // Ensure config directory exists - return path.join(this.getConfigDir(), `${configName}.json`); - } - - /** - * Cache - */ - public static getCacheDir(): string { - return this.ensureDir(path.join(this.getRootDir(), 'cache')); - } - - /** - * Logs - */ - public static getLogsDir(): string { - return this.ensureDir(path.join(this.getRootDir(), 'logs')); - } - - /** - * Temporary files - */ - public static getTempDir(): string { - return this.ensureDir(path.join(this.getRootDir(), 'temp')); - } - - /** - * Templates - */ - public static getTemplatesDir(): string { - return this.ensureDir(path.join(this.getRootDir(), 'templates')); - } - - public static getPromptTemplatePath(templateName: string): string { - this.getTemplatesDir(); // Ensure templates directory exists - return path.join(this.getTemplatesDir(), `${templateName}.txt`); - } - - /** - * Utility Methods - */ - public static resolvePath(...pathSegments: string[]): string { - const resolvedPath = path.join(this.getRootDir(), ...pathSegments); - return this.ensureDir(path.dirname(resolvedPath)); - } - - public static exists(filePath: string): boolean { - return existsSync(filePath); - } - - public static async cleanTempDir(): Promise { - const tempDir = this.getTempDir(); - const files = await promises.readdir(tempDir); - await Promise.all( - files.map((file) => promises.unlink(path.join(tempDir, file))), - ); - } - - public static getProjectStructure(projectId: string): { - root: string; - src: string; - generated: string; - tests: string; - } { - return { - root: this.getProjectPath(projectId), - src: this.getProjectSourceDir(projectId), - generated: this.getProjectGeneratedDir(projectId), - tests: this.getProjectTestsDir(projectId), - }; - } -} +}; + +// Access Project Structure +export const getProjectStructure = ( + projectId: string, +): { + root: string; + src: string; + generated: string; + tests: string; +} => ({ + root: getProjectPath(projectId), + src: getProjectSourceDir(projectId), + generated: getProjectGeneratedDir(projectId), + tests: getProjectTestsDir(projectId), +}); From a398a4f99189d9d30a08b1f6d4e6f75b6c3984c0 Mon Sep 17 00:00:00 2001 From: ZHallen122 Date: Fri, 1 Nov 2024 20:03:28 -0400 Subject: [PATCH 2/4] seperate frontend and backend --- backend/src/config/common-path.ts | 56 -------------------- frontend/src/config/common-path.ts | 85 ++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 56 deletions(-) create mode 100644 frontend/src/config/common-path.ts diff --git a/backend/src/config/common-path.ts b/backend/src/config/common-path.ts index f765ab24..8e390d3e 100644 --- a/backend/src/config/common-path.ts +++ b/backend/src/config/common-path.ts @@ -6,10 +6,6 @@ import { createHash } from 'crypto'; // Constants for base directories const APP_NAME = 'codefox'; const ROOT_DIR = path.join(os.homedir(), `.${APP_NAME}`); -const CLIENT_CACHE_DIR = path.resolve( - __dirname, - '../../frontend/.codefox-client/.cache', -); // Utility function to ensure a directory exists const ensureDir = (dirPath: string): string => { @@ -67,58 +63,6 @@ export const getTempDir = (): string => { return tempDir; }; -// Client Cache Paths -export const getCacheDir = (): string => ensureDir(CLIENT_CACHE_DIR); -export const getUserCacheDir = (userId: string): string => - ensureDir(path.join(CLIENT_CACHE_DIR, hashUserId(userId))); -export const getProjectCacheDir = (userId: string, projectId: string): string => - ensureDir(path.join(getUserCacheDir(userId), projectId)); - -// Access or create content file within a project’s cache directory -export const getProjectContentPath = ( - userId: string, - projectId: string, - fileName: string, -): string => { - return path.join(getProjectCacheDir(userId, projectId), fileName); -}; - -// Utility functions to read and write content files in the project cache directory -export const writeProjectContent = async ( - userId: string, - projectId: string, - fileName: string, - content: string, -): Promise => { - const contentPath = getProjectContentPath(userId, projectId, fileName); - await promises.writeFile(contentPath, content, 'utf8'); -}; - -export const readProjectContent = async ( - userId: string, - projectId: string, - fileName: string, -): Promise => { - const contentPath = getProjectContentPath(userId, projectId, fileName); - return promises.readFile(contentPath, 'utf8'); -}; - -export const deleteProjectContent = async ( - userId: string, - projectId: string, - fileName: string, -): Promise => { - const contentPath = getProjectContentPath(userId, projectId, fileName); - if (existsSync(contentPath)) { - await promises.unlink(contentPath); - } -}; - -// Helper function to hash user IDs -const hashUserId = (userId: string): string => { - return createHash('md5').update(userId).digest('hex'); -}; - // Utility Functions export const exists = (filePath: string): boolean => existsSync(filePath); diff --git a/frontend/src/config/common-path.ts b/frontend/src/config/common-path.ts new file mode 100644 index 00000000..d0d5e231 --- /dev/null +++ b/frontend/src/config/common-path.ts @@ -0,0 +1,85 @@ +import path from 'path'; +import os from 'os'; +import { existsSync, mkdirSync, promises } from 'fs-extra'; +import { createHash } from 'crypto'; + +// Constants for base directories +const CLIENT_CACHE_DIR = path.resolve( + __dirname, + '../../frontend/.codefox-client/.cache' +); + +// Utility function to ensure a directory exists +const ensureDir = (dirPath: string): string => { + if (!existsSync(dirPath)) { + mkdirSync(dirPath, { recursive: true }); + } + return dirPath; +}; + +// Client Cache Paths +export const getCacheDir = (): string => ensureDir(CLIENT_CACHE_DIR); +export const getUserCacheDir = (userId: string): string => + ensureDir(path.join(CLIENT_CACHE_DIR, hashUserId(userId))); +export const getProjectCacheDir = (userId: string, projectId: string): string => + ensureDir(path.join(getUserCacheDir(userId), projectId)); + +// Access or create content file within a project’s cache directory +export const getProjectContentPath = ( + userId: string, + projectId: string, + fileName: string +): string => { + return path.join(getProjectCacheDir(userId, projectId), fileName); +}; + +// Utility functions to read and write content files in the project cache directory +export const writeProjectContent = async ( + userId: string, + projectId: string, + fileName: string, + content: string +): Promise => { + const contentPath = getProjectContentPath(userId, projectId, fileName); + await promises.writeFile(contentPath, content, 'utf8'); +}; + +export const readProjectContent = async ( + userId: string, + projectId: string, + fileName: string +): Promise => { + const contentPath = getProjectContentPath(userId, projectId, fileName); + return promises.readFile(contentPath, 'utf8'); +}; + +export const deleteProjectContent = async ( + userId: string, + projectId: string, + fileName: string +): Promise => { + const contentPath = getProjectContentPath(userId, projectId, fileName); + if (existsSync(contentPath)) { + await promises.unlink(contentPath); + } +}; + +// Helper function to hash user IDs +const hashUserId = (userId: string): string => { + return createHash('md5').update(userId).digest('hex'); +}; + +// Utility Functions +export const exists = (filePath: string): boolean => existsSync(filePath); + +// Access Project Structure +export const getProjectStructure = ( + userId: string, + projectId: string +): { + root: string; + content: string; +} => ({ + root: getProjectCacheDir(userId, projectId), + content: path.join(getProjectCacheDir(userId, projectId), 'content'), +}); From d75abcb94f9870960945578e57c2cde86f05d05c Mon Sep 17 00:00:00 2001 From: ZHallen122 Date: Fri, 1 Nov 2024 20:04:12 -0400 Subject: [PATCH 3/4] update path --- frontend/src/config/common-path.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/frontend/src/config/common-path.ts b/frontend/src/config/common-path.ts index d0d5e231..b608f916 100644 --- a/frontend/src/config/common-path.ts +++ b/frontend/src/config/common-path.ts @@ -4,10 +4,7 @@ import { existsSync, mkdirSync, promises } from 'fs-extra'; import { createHash } from 'crypto'; // Constants for base directories -const CLIENT_CACHE_DIR = path.resolve( - __dirname, - '../../frontend/.codefox-client/.cache' -); +const CLIENT_CACHE_DIR = path.resolve(__dirname, '../.codefox-client/.cache'); // Utility function to ensure a directory exists const ensureDir = (dirPath: string): string => { From d50fe4a304f33d1daa504e4ae5cb6dbb0ad60daf Mon Sep 17 00:00:00 2001 From: ZHallen122 Date: Sat, 2 Nov 2024 10:30:16 -0400 Subject: [PATCH 4/4] update --- backend/src/config/common-path.ts | 5 +- frontend/src/config/common-path.ts | 79 +++++++++++++++++------------- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/backend/src/config/common-path.ts b/backend/src/config/common-path.ts index 8e390d3e..6112fbb8 100644 --- a/backend/src/config/common-path.ts +++ b/backend/src/config/common-path.ts @@ -15,6 +15,9 @@ const ensureDir = (dirPath: string): string => { return dirPath; }; +// ----------- We need path traverse Protection after we decide how we read and store the file !!!!!!!!!!!!! ------------ +// ------------------------------------------------------------------------------------------------------------- + // Root Directory Accessor export const getRootDir = (): string => ensureDir(ROOT_DIR); @@ -56,7 +59,7 @@ export const getIndexFilePath = (indexFileName: string): string => // Temporary files export const getTempDir = (): string => { - const tempDir = path.join(ROOT_DIR, '.codefox', 'temp'); + const tempDir = path.join(ROOT_DIR, 'temp'); if (!existsSync(tempDir)) { mkdirSync(tempDir, { recursive: true }); } diff --git a/frontend/src/config/common-path.ts b/frontend/src/config/common-path.ts index b608f916..40ca5559 100644 --- a/frontend/src/config/common-path.ts +++ b/frontend/src/config/common-path.ts @@ -1,10 +1,10 @@ import path from 'path'; -import os from 'os'; -import { existsSync, mkdirSync, promises } from 'fs-extra'; +import { existsSync, mkdirSync, promises as fsPromises } from 'fs-extra'; import { createHash } from 'crypto'; -// Constants for base directories -const CLIENT_CACHE_DIR = path.resolve(__dirname, '../.codefox-client/.cache'); +// Constants for the frontend root directory +const FRONTEND_ROOT_DIR = path.resolve(__dirname, '../.codefox-client'); +const CLIENT_CACHE_DIR = path.join(FRONTEND_ROOT_DIR, '.cache'); // Utility function to ensure a directory exists const ensureDir = (dirPath: string): string => { @@ -14,23 +14,52 @@ const ensureDir = (dirPath: string): string => { return dirPath; }; -// Client Cache Paths +// ----------- We need path traverse Protection after we decide how we read and store the file !!!!!!!!!!!!! ------------ +// ------------------------------------------------------------------------------------------------------------- + +// Step 1: Frontend Root Directory +export const getFrontendRootDir = (): string => ensureDir(FRONTEND_ROOT_DIR); + +// Step 2: Cache Directory export const getCacheDir = (): string => ensureDir(CLIENT_CACHE_DIR); -export const getUserCacheDir = (userId: string): string => - ensureDir(path.join(CLIENT_CACHE_DIR, hashUserId(userId))); -export const getProjectCacheDir = (userId: string, projectId: string): string => - ensureDir(path.join(getUserCacheDir(userId), projectId)); -// Access or create content file within a project’s cache directory +// Step 3: User Cache Directory +export const getUserCacheDir = (userId: string): string => { + const hashedUserId = hashUserId(userId); + return ensureDir(path.join(getCacheDir(), hashedUserId)); +}; + +// Step 4: Project Cache Directory within a User's Directory +export const getProjectCacheDir = ( + userId: string, + projectId: string +): string => { + return ensureDir(path.join(getUserCacheDir(userId), projectId)); +}; + +// Step 5: Content Directory within a Project's Cache Directory +export const getProjectContentDir = ( + userId: string, + projectId: string +): string => { + return ensureDir(path.join(getProjectCacheDir(userId, projectId), 'content')); +}; + +// Updated function to get the full path to a specific file within the 'content' directory export const getProjectContentPath = ( userId: string, projectId: string, fileName: string ): string => { - return path.join(getProjectCacheDir(userId, projectId), fileName); + return path.join(getProjectContentDir(userId, projectId), fileName); }; -// Utility functions to read and write content files in the project cache directory +// Helper function to hash user IDs for unique cache directories +const hashUserId = (userId: string): string => { + return createHash('md5').update(userId).digest('hex'); +}; + +// Utility Functions for File Operations export const writeProjectContent = async ( userId: string, projectId: string, @@ -38,7 +67,7 @@ export const writeProjectContent = async ( content: string ): Promise => { const contentPath = getProjectContentPath(userId, projectId, fileName); - await promises.writeFile(contentPath, content, 'utf8'); + await fsPromises.writeFile(contentPath, content, 'utf8'); }; export const readProjectContent = async ( @@ -47,7 +76,7 @@ export const readProjectContent = async ( fileName: string ): Promise => { const contentPath = getProjectContentPath(userId, projectId, fileName); - return promises.readFile(contentPath, 'utf8'); + return fsPromises.readFile(contentPath, 'utf8'); }; export const deleteProjectContent = async ( @@ -57,26 +86,6 @@ export const deleteProjectContent = async ( ): Promise => { const contentPath = getProjectContentPath(userId, projectId, fileName); if (existsSync(contentPath)) { - await promises.unlink(contentPath); + await fsPromises.unlink(contentPath); } }; - -// Helper function to hash user IDs -const hashUserId = (userId: string): string => { - return createHash('md5').update(userId).digest('hex'); -}; - -// Utility Functions -export const exists = (filePath: string): boolean => existsSync(filePath); - -// Access Project Structure -export const getProjectStructure = ( - userId: string, - projectId: string -): { - root: string; - content: string; -} => ({ - root: getProjectCacheDir(userId, projectId), - content: path.join(getProjectCacheDir(userId, projectId), 'content'), -});