102 lines
2.7 KiB
JavaScript
102 lines
2.7 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// Function to recursively find all TypeScript files
|
|
function findTsFiles(dir, fileList = []) {
|
|
const files = fs.readdirSync(dir);
|
|
|
|
files.forEach(file => {
|
|
const filePath = path.join(dir, file);
|
|
const stat = fs.statSync(filePath);
|
|
|
|
if (stat.isDirectory() && !filePath.includes('node_modules') && !filePath.includes('.next')) {
|
|
fileList = findTsFiles(filePath, fileList);
|
|
} else if (
|
|
stat.isFile() &&
|
|
(filePath.endsWith('.ts') || filePath.endsWith('.tsx')) &&
|
|
!filePath.includes('node_modules') &&
|
|
!filePath.includes('.next')
|
|
) {
|
|
fileList.push(filePath);
|
|
}
|
|
});
|
|
|
|
return fileList;
|
|
}
|
|
|
|
// Function to update import statements in a file
|
|
function updateImportInFile(filePath) {
|
|
try {
|
|
let content = fs.readFileSync(filePath, 'utf8');
|
|
let updated = false;
|
|
|
|
// Update absolute imports
|
|
if (content.includes('import { authOptions } from "@/app/api/auth/[...nextauth]/route"')) {
|
|
content = content.replace(
|
|
'import { authOptions } from "@/app/api/auth/[...nextauth]/route"',
|
|
'import { authOptions } from "@/app/api/auth/options"'
|
|
);
|
|
updated = true;
|
|
}
|
|
|
|
// Update relative imports
|
|
if (content.includes('import { authOptions } from "../../auth/[...nextauth]/route"')) {
|
|
content = content.replace(
|
|
'import { authOptions } from "../../auth/[...nextauth]/route"',
|
|
'import { authOptions } from "../../auth/options"'
|
|
);
|
|
updated = true;
|
|
}
|
|
|
|
// Other possible relative paths
|
|
const patterns = [
|
|
/import\s*{\s*authOptions\s*}\s*from\s*['"](.*)\/auth\/\[\.\.\.\S+\]\/route['"]/g,
|
|
/import\s*{\s*authOptions\s*}\s*from\s*['"](.*)\[...\S+\]\/route['"]/g
|
|
];
|
|
|
|
for (const pattern of patterns) {
|
|
const matches = content.matchAll(pattern);
|
|
for (const match of matches) {
|
|
const fullMatch = match[0];
|
|
const basePath = match[1];
|
|
const replacement = `import { authOptions } from "${basePath}/auth/options"`;
|
|
content = content.replace(fullMatch, replacement);
|
|
updated = true;
|
|
}
|
|
}
|
|
|
|
if (updated) {
|
|
fs.writeFileSync(filePath, content, 'utf8');
|
|
console.log(`Updated: ${filePath}`);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
} catch (error) {
|
|
console.error(`Error updating ${filePath}:`, error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Main function
|
|
function main() {
|
|
const rootDir = './app';
|
|
const libDir = './lib';
|
|
|
|
const tsFiles = [
|
|
...findTsFiles(rootDir),
|
|
...findTsFiles(libDir)
|
|
];
|
|
|
|
let updatedCount = 0;
|
|
|
|
tsFiles.forEach(file => {
|
|
if (updateImportInFile(file)) {
|
|
updatedCount++;
|
|
}
|
|
});
|
|
|
|
console.log(`\nCompleted! Updated ${updatedCount} files.`);
|
|
}
|
|
|
|
main();
|