From 125aeb988408f359db0155f081f64740fff91bdc Mon Sep 17 00:00:00 2001 From: RohitKini <57343193+RohitKini@users.noreply.github.com> Date: Thu, 24 Apr 2025 17:00:45 +0530 Subject: [PATCH 1/2] wordpress test --- remove-broken-imports.js | 49 +++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/remove-broken-imports.js b/remove-broken-imports.js index f425198aa..ff375c93e 100644 --- a/remove-broken-imports.js +++ b/remove-broken-imports.js @@ -2,27 +2,46 @@ const fs = require('fs'); const path = require('path'); const exts = ['.js', '.ts', '.tsx']; +const targetDirs = ['api', 'ui', 'upload-api']; -function fileExists(importPath, fileDir) { +function resolveImport(importPath, fileDir) { + let basePath = path.resolve(fileDir, importPath); + + // 1. Direct file with extension + if (fs.existsSync(basePath) && fs.statSync(basePath).isFile()) { + return true; + } + + // 2. Try with extensions for (const ext of exts) { - let resolved = path.resolve(fileDir, importPath); - if (!resolved.endsWith(ext)) resolved += ext; - if (fs.existsSync(resolved)) return true; + if (fs.existsSync(basePath + ext)) { + return true; + } + } + + // 3. Directory with index file + if (fs.existsSync(basePath) && fs.statSync(basePath).isDirectory()) { + for (const ext of exts) { + if (fs.existsSync(path.join(basePath, 'index' + ext))) { + 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; + + const importRegex = /^import\s+(type\s+)?[\s\S]*?from\s+['"](.*?)['"];?.*$/gm; let changed = false; - code = code.replace(importRegex, (match, importPath) => { - // Ignore node_modules and built-in modules + code = code.replace(importRegex, (match, typeKeyword, importPath) => { if (!importPath.startsWith('.') && !importPath.startsWith('/')) return match; - if (!fileExists(importPath, fileDir)) { + if (!resolveImport(importPath, fileDir)) { console.log(`Removing broken import in ${filePath}: ${match.trim()}`); changed = true; return ''; @@ -36,9 +55,14 @@ function cleanImportsInFile(filePath) { } function walkDir(dir, callback) { + if (!fs.existsSync(dir)) return; fs.readdirSync(dir, { withFileTypes: true }).forEach(dirent => { const entry = path.join(dir, dirent.name); - if (dirent.isDirectory() && dir !== 'node_modules' && !dirent.name.startsWith('.')) { + if ( + dirent.isDirectory() && + dirent.name !== 'node_modules' && + !dirent.name.startsWith('.') + ) { walkDir(entry, callback); } else if ( dirent.isFile() && @@ -49,5 +73,8 @@ function walkDir(dir, callback) { }); } -// Start from the repo root -walkDir(process.cwd(), cleanImportsInFile); +// Only process the specified folders +targetDirs.forEach(folder => { + const absPath = path.join(process.cwd(), folder); + walkDir(absPath, cleanImportsInFile); +}); From 3b5da019349003ea82383500c00b7cefe7770596 Mon Sep 17 00:00:00 2001 From: RohitKini <57343193+RohitKini@users.noreply.github.com> Date: Thu, 24 Apr 2025 17:04:53 +0530 Subject: [PATCH 2/2] lets test on workflow --- upload-api/migration-wordpress/libs/contenttypemapper.js | 1 + 1 file changed, 1 insertion(+) 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(),