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
5 changes: 5 additions & 0 deletions .github/workflows/repo-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down
53 changes: 53 additions & 0 deletions remove-broken-imports.js
Original file line number Diff line number Diff line change
@@ -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);
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
Loading