Skip to content

Commit 162588c

Browse files
committed
improved imports
1 parent 6552081 commit 162588c

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

adminforth/commands/utils.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const findInstance = (fileContent) => {
5858

5959
export async function getInstance(file, currentDirectory) {
6060
const initialFilePath = path.join(currentDirectory, file);
61-
let filePath = path.join(currentDirectory, file);
61+
let filePath = initialFilePath;
6262

6363
if (file.endsWith(".ts")) {
6464
console.log(`Compiling TypeScript file: ${file}`);
@@ -76,11 +76,26 @@ export async function getInstance(file, currentDirectory) {
7676
filePath = filePath
7777
.replace(".ts", ".js")
7878
.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+
}
7995
}
8096

8197
const fileContent = fs.readFileSync(initialFilePath, "utf-8");
8298
const instanceName = findInstance(fileContent);
83-
8499
if (instanceName) {
85100
try {
86101
const module = await import(`file://${filePath}`);

0 commit comments

Comments
 (0)