Skip to content

Commit c4a99e8

Browse files
committed
fix: enhance generate-models command by removing /index.js from import paths in compiled files.
1 parent 43f2925 commit c4a99e8

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

adminforth/commands/utils.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,20 @@ export const findInstance = (fileContent) => {
5656
return matches[0][2];
5757
};
5858

59-
function processAllJsFilesInDir(directory) {
59+
function processJsFilesInDir(directory) {
6060
const files = fs.readdirSync(directory);
6161

6262
files.forEach((file) => {
6363
const filePath = path.join(directory, file);
6464

6565
if (fs.statSync(filePath).isDirectory()) {
66-
processAllJsFilesInDir(filePath);
66+
processJsFilesInDir(filePath);
6767
} else if (file.endsWith(".js")) {
6868
try {
69-
const fileContent = fs.readFileSync(filePath, "utf-8");
70-
const updatedContent = fileContent.replace(
69+
let fileContent = fs.readFileSync(filePath, "utf-8");
70+
71+
// Add .js to relative imports if missing
72+
fileContent = fileContent.replace(
7173
/import (.+?) from ["'](.+?)["'];/g,
7274
(match, imports, modulePath) => {
7375
if (
@@ -79,7 +81,16 @@ function processAllJsFilesInDir(directory) {
7981
return match;
8082
}
8183
);
82-
fs.writeFileSync(filePath, updatedContent, "utf-8");
84+
85+
// Remove /index.js from imports
86+
fileContent = fileContent.replace(
87+
/import (.+?) from ["'](.+?)\/index\.js["'];/g,
88+
(match, imports, modulePath) => {
89+
return `import ${imports} from "${modulePath}";`;
90+
}
91+
);
92+
93+
fs.writeFileSync(filePath, fileContent, "utf-8");
8394
} catch (error) {
8495
console.error(`Error processing file '${filePath}':`, error);
8596
}
@@ -101,11 +112,10 @@ export async function getInstance(file, currentDirectory) {
101112
}
102113
);
103114
} catch (error) {
104-
console.log(`Error: Could not compile TypeScript file '${file}'`);
115+
//console.log(`Error: Could not compile TypeScript file '${file}'`);
105116
}
106-
107117
const distDir = path.join(currentDirectory, "dist");
108-
processAllJsFilesInDir(distDir);
118+
processJsFilesInDir(distDir);
109119

110120
filePath = filePath
111121
.replace(".ts", ".js")

0 commit comments

Comments
 (0)