Skip to content

Commit 49c0539

Browse files
committed
fixed problem with import
1 parent 5aabdee commit 49c0539

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

adminforth/commands/utils.js

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

59+
function processAllJsFilesInDir(directory) {
60+
const files = fs.readdirSync(directory);
61+
62+
files.forEach((file) => {
63+
const filePath = path.join(directory, file);
64+
65+
if (fs.statSync(filePath).isDirectory()) {
66+
processAllJsFilesInDir(filePath);
67+
} else if (file.endsWith(".js")) {
68+
try {
69+
const fileContent = fs.readFileSync(filePath, "utf-8");
70+
const updatedContent = fileContent.replace(
71+
/import (.+?) from ["'](.+?)["'];/g,
72+
(match, imports, modulePath) => {
73+
if (
74+
!modulePath.endsWith(".js") &&
75+
(modulePath.startsWith("./") || modulePath.startsWith("../"))
76+
) {
77+
return `import ${imports} from "${modulePath}.js";`;
78+
}
79+
return match;
80+
}
81+
);
82+
fs.writeFileSync(filePath, updatedContent, "utf-8");
83+
} catch (error) {
84+
console.error(`Error processing file '${filePath}':`, error);
85+
}
86+
}
87+
});
88+
}
89+
5990
export async function getInstance(file, currentDirectory) {
6091
const initialFilePath = path.join(currentDirectory, file);
6192
let filePath = initialFilePath;
@@ -73,25 +104,12 @@ export async function getInstance(file, currentDirectory) {
73104
console.log(`Error: Could not compile TypeScript file '${file}'`);
74105
}
75106

107+
const distDir = path.join(currentDirectory, "dist");
108+
processAllJsFilesInDir(distDir);
109+
76110
filePath = filePath
77111
.replace(".ts", ".js")
78-
.replace(currentDirectory, path.join(currentDirectory, "dist"));
79-
80-
try {
81-
const compiledContent = fs.readFileSync(filePath, "utf-8");
82-
const updatedContent = compiledContent.replace(
83-
/import (.+?) from ["'](.+?)["'];/g,
84-
(match, imports, modulePath) => {
85-
if (!modulePath.endsWith(".js") && (modulePath.startsWith("./") || modulePath.startsWith("../"))) {
86-
return `import ${imports} from "${modulePath}.js";`; // Append .js to local imports
87-
}
88-
return match;
89-
}
90-
);
91-
fs.writeFileSync(filePath, updatedContent, "utf-8");
92-
} catch (error) {
93-
console.error("Error during post-processing:", error);
94-
}
112+
.replace(currentDirectory, distDir);
95113
}
96114

97115
const fileContent = fs.readFileSync(initialFilePath, "utf-8");

0 commit comments

Comments
 (0)