From 066540e10c75c2b3b0123d9bbe2f48e3d4631e41 Mon Sep 17 00:00:00 2001 From: RohitKini <57343193+RohitKini@users.noreply.github.com> Date: Thu, 24 Apr 2025 16:35:13 +0530 Subject: [PATCH] checking cleanup script --- .github/workflows/repo-sync.yml | 5 ++ remove-broken-imports.js | 53 +++++++++++++++++++ .../libs/contenttypemapper.js | 1 + 3 files changed, 59 insertions(+) create mode 100644 remove-broken-imports.js diff --git a/.github/workflows/repo-sync.yml b/.github/workflows/repo-sync.yml index 88fa765a1..2ee96741a 100644 --- a/.github/workflows/repo-sync.yml +++ b/.github/workflows/repo-sync.yml @@ -184,6 +184,7 @@ jobs: # npx prettier --write . # eslint api/ ui/ upload-api/ --rule 'import/no-unresolved: error' --format compact | awk -F ':' '{print $1}' | sort -u | xargs -I {} sed -i '/import/d' {} + node scripts/remove-broken-imports.js git add . git commit -m "Sync changes from migration-v2 PR #${{ github.event.pull_request.number }}" @@ -288,6 +289,8 @@ jobs: rsync -av --delete ../ui/ ./ui/ rsync -av --delete ${{ env.RSYNC_CONTENTFUL_UPLOAD_API_SRC_EXCLUDES }} ../upload-api/src/ ./upload-api/src/ rsync -av --delete ../upload-api/migration-contentful/ ./upload-api/migration-contentful/ + node scripts/remove-broken-imports.js + git add . git commit -m "Sync changes from migration-v2 PR #${{ github.event.pull_request.number }}" git push origin sync-from-migration-v2-${{ github.event.pull_request.number }} @@ -390,6 +393,8 @@ jobs: rsync -av --delete ../ui/ ./ui/ rsync -av --delete ${{ env.RSYNC_WORDPRESS_UPLOAD_API_SRC_EXCLUDES }} ../upload-api/src/ ./upload-api/src/ rsync -av --delete ../upload-api/migration-wordpress/ ./upload-api/migration-wordpress/ + node scripts/remove-broken-imports.js + git add . git commit -m "Sync changes from migration-v2 PR #${{ github.event.pull_request.number }}" git push origin sync-from-migration-v2-${{ github.event.pull_request.number }} diff --git a/remove-broken-imports.js b/remove-broken-imports.js new file mode 100644 index 000000000..f425198aa --- /dev/null +++ b/remove-broken-imports.js @@ -0,0 +1,53 @@ +const fs = require('fs'); +const path = require('path'); + +const exts = ['.js', '.ts', '.tsx']; + +function fileExists(importPath, fileDir) { + for (const ext of exts) { + let resolved = path.resolve(fileDir, importPath); + if (!resolved.endsWith(ext)) resolved += ext; + if (fs.existsSync(resolved)) return true; + } + return false; +} + +function cleanImportsInFile(filePath) { + let code = fs.readFileSync(filePath, 'utf-8'); + const fileDir = path.dirname(filePath); + const importRegex = /import\s+.*?from\s+['"](.*?)['"];?/g; + + let changed = false; + code = code.replace(importRegex, (match, importPath) => { + // Ignore node_modules and built-in modules + if (!importPath.startsWith('.') && !importPath.startsWith('/')) return match; + + if (!fileExists(importPath, fileDir)) { + console.log(`Removing broken import in ${filePath}: ${match.trim()}`); + changed = true; + return ''; + } + return match; + }); + + if (changed) { + fs.writeFileSync(filePath, code); + } +} + +function walkDir(dir, callback) { + fs.readdirSync(dir, { withFileTypes: true }).forEach(dirent => { + const entry = path.join(dir, dirent.name); + if (dirent.isDirectory() && dir !== 'node_modules' && !dirent.name.startsWith('.')) { + walkDir(entry, callback); + } else if ( + dirent.isFile() && + exts.some(ext => entry.endsWith(ext)) + ) { + callback(entry); + } + }); +} + +// Start from the repo root +walkDir(process.cwd(), cleanImportsInFile); diff --git a/upload-api/migration-wordpress/libs/contenttypemapper.js b/upload-api/migration-wordpress/libs/contenttypemapper.js index 552fb9388..b9fa15e2c 100644 --- a/upload-api/migration-wordpress/libs/contenttypemapper.js +++ b/upload-api/migration-wordpress/libs/contenttypemapper.js @@ -7,6 +7,7 @@ const config = require('../config'); const { writeFile, writeFileAsync } = require('../utils/helper'); const restrictedUid = require('../utils'); + const { contentTypes: contentTypesConfig } = config.modules; const contentTypesFile = path.join( process.cwd(),