Skip to content

Commit 8aa3c34

Browse files
authored
Merge pull request #640 from contentstack/feature/workflow
wordpress test
2 parents 6f853bf + 2fef2ef commit 8aa3c34

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

remove-broken-imports.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,46 @@ const fs = require('fs');
22
const path = require('path');
33

44
const exts = ['.js', '.ts', '.tsx'];
5+
const targetDirs = ['api', 'ui', 'upload-api'];
56

6-
function fileExists(importPath, fileDir) {
7+
function resolveImport(importPath, fileDir) {
8+
let basePath = path.resolve(fileDir, importPath);
9+
10+
// 1. Direct file with extension
11+
if (fs.existsSync(basePath) && fs.statSync(basePath).isFile()) {
12+
return true;
13+
}
14+
15+
// 2. Try with extensions
716
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;
17+
if (fs.existsSync(basePath + ext)) {
18+
return true;
19+
}
20+
}
21+
22+
// 3. Directory with index file
23+
if (fs.existsSync(basePath) && fs.statSync(basePath).isDirectory()) {
24+
for (const ext of exts) {
25+
if (fs.existsSync(path.join(basePath, 'index' + ext))) {
26+
return true;
27+
}
28+
}
1129
}
30+
1231
return false;
1332
}
1433

1534
function cleanImportsInFile(filePath) {
1635
let code = fs.readFileSync(filePath, 'utf-8');
1736
const fileDir = path.dirname(filePath);
18-
const importRegex = /import\s+.*?from\s+['"](.*?)['"];?/g;
37+
38+
const importRegex = /^import\s+(type\s+)?[\s\S]*?from\s+['"](.*?)['"];?.*$/gm;
1939

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

25-
if (!fileExists(importPath, fileDir)) {
44+
if (!resolveImport(importPath, fileDir)) {
2645
console.log(`Removing broken import in ${filePath}: ${match.trim()}`);
2746
changed = true;
2847
return '';
@@ -36,9 +55,14 @@ function cleanImportsInFile(filePath) {
3655
}
3756

3857
function walkDir(dir, callback) {
58+
if (!fs.existsSync(dir)) return;
3959
fs.readdirSync(dir, { withFileTypes: true }).forEach(dirent => {
4060
const entry = path.join(dir, dirent.name);
41-
if (dirent.isDirectory() && dir !== 'node_modules' && !dirent.name.startsWith('.')) {
61+
if (
62+
dirent.isDirectory() &&
63+
dirent.name !== 'node_modules' &&
64+
!dirent.name.startsWith('.')
65+
) {
4266
walkDir(entry, callback);
4367
} else if (
4468
dirent.isFile() &&
@@ -49,5 +73,8 @@ function walkDir(dir, callback) {
4973
});
5074
}
5175

52-
// Start from the repo root
53-
walkDir(process.cwd(), cleanImportsInFile);
76+
// Only process the specified folders
77+
targetDirs.forEach(folder => {
78+
const absPath = path.join(process.cwd(), folder);
79+
walkDir(absPath, cleanImportsInFile);
80+
});

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)