refactor: Update import statements and configurations for ES module compatibility
This commit is contained in:
60
scripts/fix-imports.cjs
Executable file
60
scripts/fix-imports.cjs
Executable file
@@ -0,0 +1,60 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// Get all TypeScript files in a directory recursively
|
||||
function getTypeScriptFiles(dir) {
|
||||
const files = [];
|
||||
|
||||
function traverse(currentDir) {
|
||||
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(currentDir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
traverse(fullPath);
|
||||
} else if (entry.isFile() && entry.name.endsWith('.ts')) {
|
||||
files.push(fullPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
traverse(dir);
|
||||
return files;
|
||||
}
|
||||
|
||||
// Fix imports in a file
|
||||
function fixImportsInFile(filePath) {
|
||||
console.log(`Processing ${filePath}`);
|
||||
let content = fs.readFileSync(filePath, 'utf8');
|
||||
|
||||
// Regular expression to match relative imports without file extensions
|
||||
const importRegex = /(import\s+(?:[^'"]*\s+from\s+)?['"])(\.\.[^'"]*?)(['"])/g;
|
||||
|
||||
// Add .js extension to relative imports
|
||||
content = content.replace(importRegex, (match, start, importPath, end) => {
|
||||
// Don't add extension if it already has one or ends with a directory
|
||||
if (importPath.endsWith('.js') || importPath.endsWith('/')) {
|
||||
return match;
|
||||
}
|
||||
return `${start}${importPath}.js${end}`;
|
||||
});
|
||||
|
||||
fs.writeFileSync(filePath, content);
|
||||
}
|
||||
|
||||
// Main function
|
||||
function main() {
|
||||
const srcDir = path.join(__dirname, '..', 'src');
|
||||
const files = getTypeScriptFiles(srcDir);
|
||||
|
||||
console.log(`Found ${files.length} TypeScript files`);
|
||||
|
||||
for (const file of files) {
|
||||
fixImportsInFile(file);
|
||||
}
|
||||
|
||||
console.log('Done');
|
||||
}
|
||||
|
||||
main();
|
||||
60
scripts/fix-imports.js
Executable file
60
scripts/fix-imports.js
Executable file
@@ -0,0 +1,60 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// Get all TypeScript files in a directory recursively
|
||||
function getTypeScriptFiles(dir) {
|
||||
const files = [];
|
||||
|
||||
function traverse(currentDir) {
|
||||
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = path.join(currentDir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
traverse(fullPath);
|
||||
} else if (entry.isFile() && entry.name.endsWith('.ts')) {
|
||||
files.push(fullPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
traverse(dir);
|
||||
return files;
|
||||
}
|
||||
|
||||
// Fix imports in a file
|
||||
function fixImportsInFile(filePath) {
|
||||
console.log(`Processing ${filePath}`);
|
||||
let content = fs.readFileSync(filePath, 'utf8');
|
||||
|
||||
// Regular expression to match relative imports without file extensions
|
||||
const importRegex = /(import\s+(?:[^'"]*\s+from\s+)?['"])(\.\.[^'"]*?)(['"])/g;
|
||||
|
||||
// Add .js extension to relative imports
|
||||
content = content.replace(importRegex, (match, start, importPath, end) => {
|
||||
// Don't add extension if it already has one or ends with a directory
|
||||
if (importPath.endsWith('.js') || importPath.endsWith('/')) {
|
||||
return match;
|
||||
}
|
||||
return `${start}${importPath}.js${end}`;
|
||||
});
|
||||
|
||||
fs.writeFileSync(filePath, content);
|
||||
}
|
||||
|
||||
// Main function
|
||||
function main() {
|
||||
const srcDir = path.join(__dirname, '..', 'src');
|
||||
const files = getTypeScriptFiles(srcDir);
|
||||
|
||||
console.log(`Found ${files.length} TypeScript files`);
|
||||
|
||||
for (const file of files) {
|
||||
fixImportsInFile(file);
|
||||
}
|
||||
|
||||
console.log('Done');
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user