From e908a2d707be071aa802ea1495d7131dec2aefad Mon Sep 17 00:00:00 2001 From: aXenDeveloper Date: Fri, 22 Nov 2024 18:00:58 +0100 Subject: [PATCH] perf: Remove forced files from frontend package --- docker-compose.yml | 2 +- package.json | 1 + .../helpers/create-packages-json.ts | 6 +- .../templates/docker/deploy.sh | 2 - .../templates/docker/docker-compose.yml | 2 +- .../check-files-and-filter-if-exist.ts | 132 ------------------ packages/frontend/scripts/setup.ts | 34 ----- packages/frontend/src/api/fetcher-client.ts | 2 +- .../frontend/src/helpers/config-with-env.ts | 3 +- .../admin/layout/sidebar/search/search.tsx | 2 +- .../layout/nav-bar-mobile/nav-bar-mobile.tsx | 6 +- turbo.json | 3 + 12 files changed, 16 insertions(+), 179 deletions(-) delete mode 100644 packages/frontend/scripts/helpers/check-files-and-filter-if-exist.ts diff --git a/docker-compose.yml b/docker-compose.yml index b59fc16d1..c2732a99b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,7 +27,7 @@ services: DB_HOST: ${DB_HOST:-database} env_file: - .env - command: sh -c "cd apps/backend && pnpm config:init && node dist/main.js" + command: sh -c "cd apps/backend && npm run config:init && node dist/main.js" ports: - '8080:8080' volumes: diff --git a/package.json b/package.json index c81d4f358..6b479f8f3 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "license": "MIT", "scripts": { "config:init:skip-database": "cd apps/backend && pnpm config:init --skip-database && cd ../..", + "config:init": "turbo config:init", "build:scripts": "turbo build:scripts && pnpm i", "build": "turbo build:packages && turbo build", "build:packages": "turbo build:packages", diff --git a/packages/create-vitnode-app/helpers/create-packages-json.ts b/packages/create-vitnode-app/helpers/create-packages-json.ts index f5f68513d..ca735355e 100644 --- a/packages/create-vitnode-app/helpers/create-packages-json.ts +++ b/packages/create-vitnode-app/helpers/create-packages-json.ts @@ -85,8 +85,8 @@ export const createPackagesJSON = ({ version: '1.0.0', private: true, scripts: { - 'config:init': 'vitnode-frontend init', - dev: 'vitnode-frontend dev && next dev --turbo', + postinstall: 'vitnode-frontend init', + dev: 'next dev --turbo', build: 'next build', start: 'next start', 'start:prod': 'node server.js', @@ -133,7 +133,7 @@ export const createPackagesJSON = ({ scripts: { 'drizzle-kit': 'drizzle-kit', 'config:init': 'vitnode-backend init', - dev: 'pnpm config:init && cross-env NODE_ENV=development nest start -w', + dev: 'vitnode-backend init && cross-env NODE_ENV=development nest start -w', build: 'nest build', start: 'node dist/main', lint: 'eslint .', diff --git a/packages/create-vitnode-app/templates/docker/deploy.sh b/packages/create-vitnode-app/templates/docker/deploy.sh index 69f0360dc..5c60a4e20 100644 --- a/packages/create-vitnode-app/templates/docker/deploy.sh +++ b/packages/create-vitnode-app/templates/docker/deploy.sh @@ -36,8 +36,6 @@ fi # ================================ # Install Nginx sudo apt install nginx -y -# Allow Nginx through the firewall -sudo ufw allow 'Nginx HTTP' # Delete the default Nginx configuration file if it exists if [ -f /etc/nginx/sites-available/default ]; then diff --git a/packages/create-vitnode-app/templates/docker/docker-compose.yml b/packages/create-vitnode-app/templates/docker/docker-compose.yml index b59fc16d1..c2732a99b 100644 --- a/packages/create-vitnode-app/templates/docker/docker-compose.yml +++ b/packages/create-vitnode-app/templates/docker/docker-compose.yml @@ -27,7 +27,7 @@ services: DB_HOST: ${DB_HOST:-database} env_file: - .env - command: sh -c "cd apps/backend && pnpm config:init && node dist/main.js" + command: sh -c "cd apps/backend && npm run config:init && node dist/main.js" ports: - '8080:8080' volumes: diff --git a/packages/frontend/scripts/helpers/check-files-and-filter-if-exist.ts b/packages/frontend/scripts/helpers/check-files-and-filter-if-exist.ts deleted file mode 100644 index 3657462b0..000000000 --- a/packages/frontend/scripts/helpers/check-files-and-filter-if-exist.ts +++ /dev/null @@ -1,132 +0,0 @@ -import * as fs from 'fs'; -import * as path from 'path'; - -/** - * Recursively checks for 'page.tsx' files in `fromPath` and returns an array of relative paths - * of those that do not exist in `toPath`, sorted with root folders on top and nested items below. - * Skips paths if their non-namespace equivalent exists in `toPath` under any namespace directories. - * @param fromPath - The source directory containing files and folders to check. - * @param toPath - The target directory to compare against. - * @returns An array of relative paths from `fromPath` where 'page.tsx' files do not exist in `toPath`, without 'page.tsx' at the end, sorted by depth. - */ -export function checkFilesAndFilterIfExist( - fromPath: string, - toPath: string, -): string[] { - const missingPaths: string[] = []; - const toPathNormalizedSet = new Set(); - - /** - * Normalizes a path by removing namespace directories (enclosed in parentheses or square brackets). - * @param dirPath - The directory path to normalize. - * @returns The normalized path. - */ - function normalizePath(dirPath: string): string { - const segments = dirPath.split(path.sep); - const normalizedSegments = segments.filter(segment => { - return !( - (segment.startsWith('(') && segment.endsWith(')')) || - (segment.startsWith('[') && segment.endsWith(']')) - ); - }); - - return normalizedSegments.join(path.sep); - } - - /** - * Traverses `toPath` to build a map of normalized paths to their actual paths. - * @param currentPath - The current directory path being traversed. - */ - function traverseToPath(currentPath: string) { - const entries = fs.readdirSync(currentPath, { withFileTypes: true }); - - for (const entry of entries) { - const fullPath = path.join(currentPath, entry.name); - - if (entry.isDirectory()) { - traverseToPath(fullPath); - } else if (entry.name === 'page.tsx') { - const relativePath = path.relative(toPath, fullPath); - const dirPath = path.dirname(relativePath); - const normalizedPath = normalizePath(dirPath); - toPathNormalizedSet.add(normalizedPath); - } - } - } - - traverseToPath(toPath); - - /** - * Recursively traverses `fromPath` and updates the missingPaths array. - * @param currentPath - The current directory path being traversed. - */ - function traverseFromPath(currentPath: string) { - const entries = fs.readdirSync(currentPath, { withFileTypes: true }); - - for (const entry of entries) { - const fullPath = path.join(currentPath, entry.name); - const relativePath = path.relative(fromPath, fullPath); - - if (entry.isDirectory()) { - traverseFromPath(fullPath); - } else if (entry.name === 'page.tsx') { - const dirPath = path.dirname(relativePath); - - // Skip paths that consist only of namespace directories after "app" - if (!hasNonBracketedSegment(dirPath)) { - continue; - } - - const normalizedPath = normalizePath(dirPath); - - if (!toPathNormalizedSet.has(normalizedPath)) { - missingPaths.push(dirPath); - } - } - } - } - - traverseFromPath(fromPath); - - // Sort missingPaths by depth (number of path separators), with shallower paths first - missingPaths.sort((a, b) => { - const depthA = a.split(path.sep).length; - const depthB = b.split(path.sep).length; - - return depthA - depthB; - }); - - return missingPaths; -} - -/** - * Checks if the path contains any directory segments not enclosed in parentheses or square brackets, after "app". - * @param relativePath - The relative path to check. - * @returns True if there is at least one segment not enclosed in parentheses or square brackets after "app". - */ -function hasNonBracketedSegment(relativePath: string): boolean { - const segments = relativePath.split(path.sep); - - // Find the index of "app" in the segments - const appIndex = segments.findIndex(segment => segment === 'app'); - - // Start checking from the segment after "app" - const startIndex = appIndex >= 0 ? appIndex + 1 : 0; - - for (let i = startIndex; i < segments.length; i++) { - const segment = segments[i]; - // Skip empty segments (can occur if path starts with separator) - if (segment === '') continue; - - if ( - !( - (segment.startsWith('(') && segment.endsWith(')')) || - (segment.startsWith('[') && segment.endsWith(']')) - ) - ) { - return true; // Found a non-namespace segment - } - } - - return false; // All segments are namespace directories -} diff --git a/packages/frontend/scripts/setup.ts b/packages/frontend/scripts/setup.ts index 9dc761f65..c3ddbaffd 100644 --- a/packages/frontend/scripts/setup.ts +++ b/packages/frontend/scripts/setup.ts @@ -4,8 +4,6 @@ import * as fs from 'fs'; import { join } from 'path'; -import { checkFilesAndFilterIfExist } from './helpers/check-files-and-filter-if-exist'; - const init = ({ dev }: { dev: boolean }) => { const initConsole = '\x1b[34m[VitNode]\x1b[0m \x1b[33m[Frontend]\x1b[0m'; @@ -34,10 +32,6 @@ const init = ({ dev }: { dev: boolean }) => { file: string; path: string; }[] = [ - { - path: join('src', 'app'), - file: 'not-found.tsx', - }, { path: join('src', 'plugins', 'core', 'langs'), file: 'en.json', @@ -48,7 +42,6 @@ const init = ({ dev }: { dev: boolean }) => { }, ]; - // Stage 2 - Force files forcePathFiles.forEach(file => { const packageFromCopyPath = join(packagePath, file.path, file.file); const rootToCopyPath = join(process.cwd(), file.path, file.file); @@ -56,33 +49,6 @@ const init = ({ dev }: { dev: boolean }) => { fs.copyFileSync(packageFromCopyPath, rootToCopyPath); }); - // Stage 3 - Force copy folders - const forceCopyFolders = [ - join('src', 'app', '[locale]', 'admin', '(vitnode)'), - join('src', 'app', '[locale]', 'admin', '(auth)', '(vitnode)'), - ]; - - // Stage 4 - Force folders - forceCopyFolders.forEach(folder => { - const packageFromCopyPath = join(packagePath, folder); - const rootToCopyPath = join(process.cwd(), folder); - - fs.cpSync(packageFromCopyPath, rootToCopyPath, { recursive: true }); - }); - - // Stage 5 - Copy folders if not exist - const foldersOptional = checkFilesAndFilterIfExist( - packagePath, - process.cwd(), - ); - - foldersOptional.forEach(folder => { - const packageFromCopyPath = join(packagePath, folder); - const rootToCopyPath = join(process.cwd(), folder); - - fs.cpSync(packageFromCopyPath, rootToCopyPath, { recursive: true }); - }); - console.log(`${initConsole} ✅ Frontend files copied successfully.`); process.exit(0); }; diff --git a/packages/frontend/src/api/fetcher-client.ts b/packages/frontend/src/api/fetcher-client.ts index 7d30ba3b8..b12dac896 100644 --- a/packages/frontend/src/api/fetcher-client.ts +++ b/packages/frontend/src/api/fetcher-client.ts @@ -21,7 +21,7 @@ export async function fetcherClient< res: Response; }> { const query = queryFromArgs ? buildFilteredQuery(queryFromArgs) : ''; - const href = `${CONFIG.backend_url}${url}${query ? `?${query}` : ''}`; + const href = `${CONFIG.backend_client_url}${url}${query ? `?${query}` : ''}`; const method = options?.method ?? 'GET'; const res = await fetch(href, { ...options, diff --git a/packages/frontend/src/helpers/config-with-env.ts b/packages/frontend/src/helpers/config-with-env.ts index 8de84fcef..841948ce2 100644 --- a/packages/frontend/src/helpers/config-with-env.ts +++ b/packages/frontend/src/helpers/config-with-env.ts @@ -14,8 +14,9 @@ const urls = { export const CONFIG = { backend_url: urls.backend.origin, + backend_client_url: urls.backend_client.origin, frontend_url: urls.frontend.origin, - backend_public_url: `${urls.backend_client.origin}/public`, + backend_public_url: `${urls.backend.origin}/public`, local_storage: { editor_skin_tone: 'emoji:skin-tone', }, diff --git a/packages/frontend/src/views/admin/layout/sidebar/search/search.tsx b/packages/frontend/src/views/admin/layout/sidebar/search/search.tsx index 7aa6da350..dbebdbea5 100644 --- a/packages/frontend/src/views/admin/layout/sidebar/search/search.tsx +++ b/packages/frontend/src/views/admin/layout/sidebar/search/search.tsx @@ -49,7 +49,7 @@ export const SearchSidebarAdmin = ({ return (