Aethel Bot OSS repository!
aethel.xyz
bot
fun
ai
discord
discord-bot
aethel
1import fs from 'fs';
2import path from 'path';
3import { fileURLToPath } from 'url';
4import { dirname } from 'path';
5
6const __filename = fileURLToPath(import.meta.url);
7const __dirname = dirname(__filename);
8
9function fixImports(dir) {
10 const files = fs.readdirSync(dir);
11
12 for (const file of files) {
13 const filePath = path.join(dir, file);
14 const stat = fs.statSync(filePath);
15
16 if (stat.isDirectory()) {
17 fixImports(filePath);
18 } else if (file.endsWith('.js')) {
19 let content = fs.readFileSync(filePath, 'utf8');
20
21 content = content.replace(/from ['"]@\/([^'"]+)['"]/g, (match, importPath) => {
22 const targetPath = path.join('dist', importPath);
23 const relativePath = path.relative(path.dirname(filePath), targetPath);
24 const normalizedPath = relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
25 return `from '${normalizedPath.replace(/\\/g, '/')}.js'`;
26 });
27
28 content = content.replace(/import\(['"]@\/([^'"]+)['"]\)/g, (match, importPath) => {
29 const targetPath = path.join('dist', importPath);
30 const relativePath = path.relative(path.dirname(filePath), targetPath);
31 const normalizedPath = relativePath.startsWith('.') ? relativePath : `./${relativePath}`;
32 return `import('${normalizedPath.replace(/\\/g, '/')}.js')`;
33 });
34
35 content = content.replace(/from ['"]\.\/([^'"]+)['"]/g, (match, importPath) => {
36 if (importPath.endsWith('.js')) {
37 return match;
38 }
39
40 const importDir = path.join(path.dirname(filePath), importPath);
41 const indexPath = path.join(importDir, 'index.js');
42 if (fs.existsSync(indexPath)) {
43 return `from './${importPath}/index.js'`;
44 }
45 return `from './${importPath}.js'`;
46 });
47
48 content = content.replace(/from ['"]\.\.\/([^'"]+)['"]/g, (match, importPath) => {
49 if (importPath.endsWith('.js')) {
50 const importDir = path.join(path.dirname(filePath), '..', importPath.replace('.js', ''));
51 const indexPath = path.join(importDir, 'index.js');
52 if (fs.existsSync(indexPath)) {
53 return `from '../${importPath.replace('.js', '')}/index.js'`;
54 }
55 return match;
56 }
57
58 const importDir = path.join(path.dirname(filePath), '..', importPath);
59 const indexPath = path.join(importDir, 'index.js');
60 if (fs.existsSync(indexPath)) {
61 return `from '../${importPath}/index.js'`;
62 }
63 return `from '../${importPath}.js'`;
64 });
65
66 fs.writeFileSync(filePath, content);
67 }
68 }
69}
70
71fixImports(path.join(__dirname, '../dist'));