Skip to content

Commit 82821c4

Browse files
authored
Merge pull request #637 from contentstack/feature/workflow
checking cleanup script
2 parents 4acc4f3 + 066540e commit 82821c4

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

.github/workflows/repo-sync.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ jobs:
184184
# npx prettier --write .
185185
186186
# eslint api/ ui/ upload-api/ --rule 'import/no-unresolved: error' --format compact | awk -F ':' '{print $1}' | sort -u | xargs -I {} sed -i '/import/d' {}
187+
node scripts/remove-broken-imports.js
187188
188189
git add .
189190
git commit -m "Sync changes from migration-v2 PR #${{ github.event.pull_request.number }}"
@@ -288,6 +289,8 @@ jobs:
288289
rsync -av --delete ../ui/ ./ui/
289290
rsync -av --delete ${{ env.RSYNC_CONTENTFUL_UPLOAD_API_SRC_EXCLUDES }} ../upload-api/src/ ./upload-api/src/
290291
rsync -av --delete ../upload-api/migration-contentful/ ./upload-api/migration-contentful/
292+
node scripts/remove-broken-imports.js
293+
291294
git add .
292295
git commit -m "Sync changes from migration-v2 PR #${{ github.event.pull_request.number }}"
293296
git push origin sync-from-migration-v2-${{ github.event.pull_request.number }}
@@ -390,6 +393,8 @@ jobs:
390393
rsync -av --delete ../ui/ ./ui/
391394
rsync -av --delete ${{ env.RSYNC_WORDPRESS_UPLOAD_API_SRC_EXCLUDES }} ../upload-api/src/ ./upload-api/src/
392395
rsync -av --delete ../upload-api/migration-wordpress/ ./upload-api/migration-wordpress/
396+
node scripts/remove-broken-imports.js
397+
393398
git add .
394399
git commit -m "Sync changes from migration-v2 PR #${{ github.event.pull_request.number }}"
395400
git push origin sync-from-migration-v2-${{ github.event.pull_request.number }}

remove-broken-imports.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const exts = ['.js', '.ts', '.tsx'];
5+
6+
function fileExists(importPath, fileDir) {
7+
for (const ext of exts) {
8+
let resolved = path.resolve(fileDir, importPath);
9+
if (!resolved.endsWith(ext)) resolved += ext;
10+
if (fs.existsSync(resolved)) return true;
11+
}
12+
return false;
13+
}
14+
15+
function cleanImportsInFile(filePath) {
16+
let code = fs.readFileSync(filePath, 'utf-8');
17+
const fileDir = path.dirname(filePath);
18+
const importRegex = /import\s+.*?from\s+['"](.*?)['"];?/g;
19+
20+
let changed = false;
21+
code = code.replace(importRegex, (match, importPath) => {
22+
// Ignore node_modules and built-in modules
23+
if (!importPath.startsWith('.') && !importPath.startsWith('/')) return match;
24+
25+
if (!fileExists(importPath, fileDir)) {
26+
console.log(`Removing broken import in ${filePath}: ${match.trim()}`);
27+
changed = true;
28+
return '';
29+
}
30+
return match;
31+
});
32+
33+
if (changed) {
34+
fs.writeFileSync(filePath, code);
35+
}
36+
}
37+
38+
function walkDir(dir, callback) {
39+
fs.readdirSync(dir, { withFileTypes: true }).forEach(dirent => {
40+
const entry = path.join(dir, dirent.name);
41+
if (dirent.isDirectory() && dir !== 'node_modules' && !dirent.name.startsWith('.')) {
42+
walkDir(entry, callback);
43+
} else if (
44+
dirent.isFile() &&
45+
exts.some(ext => entry.endsWith(ext))
46+
) {
47+
callback(entry);
48+
}
49+
});
50+
}
51+
52+
// Start from the repo root
53+
walkDir(process.cwd(), cleanImportsInFile);

upload-api/migration-wordpress/libs/contenttypemapper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const config = require('../config');
77
const { writeFile, writeFileAsync } = require('../utils/helper');
88
const restrictedUid = require('../utils');
99

10+
1011
const { contentTypes: contentTypesConfig } = config.modules;
1112
const contentTypesFile = path.join(
1213
process.cwd(),

0 commit comments

Comments
 (0)