const fs = require('fs'); const path = require('path'); const srcDir = path.resolve(__dirname, 'src'); const applicationErrorPath = path.resolve(srcDir, 'shared/domain/errors/application-error'); function walkDir(dir, callback) { fs.readdirSync(dir).forEach(file => { const fullPath = path.join(dir, file); if (fs.statSync(fullPath).isDirectory()) { walkDir(fullPath, callback); } else if (fullPath.endsWith('.ts')) { callback(fullPath); } }); } walkDir(path.resolve(srcDir, 'modules'), file => { let content = fs.readFileSync(file, 'utf8'); // Match any import containing application-error const regex = /from\s+['"]([^'"]*application-error)['"]/g; let matches = [...content.matchAll(regex)]; if (matches.length > 0) { let changed = false; for (const match of matches) { const importPath = match[1]; // calculate correct relative path const dirOfFile = path.dirname(file); // Let's use posix for imports let correctRelative = path.posix.relative( dirOfFile.split(path.sep).join(path.posix.sep), applicationErrorPath.split(path.sep).join(path.posix.sep) ); if (!correctRelative.startsWith('.')) { correctRelative = './' + correctRelative; } if (importPath !== correctRelative) { console.log(`Fixing ${path.relative(srcDir, file)}: ${importPath} -> ${correctRelative}`); content = content.replace(match[0], `from "${correctRelative}"`); changed = true; } } if (changed) { fs.writeFileSync(file, content); } } });