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
49 changes: 38 additions & 11 deletions remove-broken-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand All @@ -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() &&
Expand All @@ -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);
});
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 @@ -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(),
Expand Down