Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions remove-broken-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,44 @@ const exts = ['.js', '.ts', '.tsx'];
const targetDirs = ['api', 'ui', 'upload-api'];

function resolveImport(importPath, fileDir) {
// Absolute or relative path
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) {
if (fs.existsSync(basePath + ext)) {
// 1. If import has extension, check directly
if (/\.[jt]sx?$/.test(importPath)) {
if (fs.existsSync(basePath) && fs.statSync(basePath).isFile()) {
return true;
}
}

// 3. Directory with index file
if (fs.existsSync(basePath) && fs.statSync(basePath).isDirectory()) {
} else {
// 2. Try with extensions
for (const ext of exts) {
if (fs.existsSync(path.join(basePath, 'index' + ext))) {
if (fs.existsSync(basePath + ext) && fs.statSync(basePath + ext).isFile()) {
return true;
}
}
// 3. Directory with index file
if (fs.existsSync(basePath) && fs.statSync(basePath).isDirectory()) {
for (const ext of exts) {
const idx = path.join(basePath, 'index' + ext);
if (fs.existsSync(idx) && fs.statSync(idx).isFile()) {
return true;
}
}
}
}

return false;
}

function cleanImportsInFile(filePath) {
let code = fs.readFileSync(filePath, 'utf-8');
const fileDir = path.dirname(filePath);

// Handles both regular and type-only imports
const importRegex = /^import\s+(type\s+)?[\s\S]*?from\s+['"](.*?)['"];?.*$/gm;

let changed = false;
code = code.replace(importRegex, (match, typeKeyword, importPath) => {
// Ignore node_modules and built-in modules
if (!importPath.startsWith('.') && !importPath.startsWith('/')) return match;

if (!resolveImport(importPath, fileDir)) {
Expand Down
1 change: 1 addition & 0 deletions upload-api/migration-wordpress/libs/contenttypemapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { writeFile, writeFileAsync } = require('../utils/helper');
const restrictedUid = require('../utils');



const { contentTypes: contentTypesConfig } = config.modules;
const contentTypesFile = path.join(
process.cwd(),
Expand Down
Loading