Files
lobehub/scripts/migrate-spa-navigation.ts
Innei fcdaf9d814 🔧 chore: update eslint v2 configuration and suppressions (#12133)
* v2 init

* chore: update eslint suppressions and package dependencies

- Removed several eslint suppressions related to array sorting and reversing from eslint-suppressions.json to clean up the configuration.
- Updated @lobehub/lint package version from 2.0.0-beta.6 to 2.0.0-beta.7 in package.json for improvements and bug fixes.
- Made minor formatting adjustments in vitest.config.mts and various SKILL.md files for better readability and consistency.

Signed-off-by: Innei <tukon479@gmail.com>

* fix: clean up import statements and formatting

- Removed unnecessary whitespace in replaceComponentImports.ts for improved readability.
- Standardized import statements in contextEngineering.ts and createAgentExecutors.ts by adding missing spaces for consistency.

Signed-off-by: Innei <tukon479@gmail.com>

* chore: update eslint suppressions and clean up code formatting

* 🐛 fix: use vi.hoisted for mock variable initialization

Fix TDZ error in persona service test by using vi.hoisted() to ensure
mock variables are available when vi.mock factory runs.

---------

Signed-off-by: Innei <tukon479@gmail.com>
2026-02-11 13:04:48 +08:00

139 lines
4.3 KiB
TypeScript

#!/usr/bin/env bun
/**
* Script to migrate SPA internal components from @/libs/next/navigation
* to React Router version hooks.
*
* For files in (main) directory:
* - usePathname -> @/app/[variants]/(main)/hooks/usePathname
* - useSearchParams -> @/app/[variants]/(main)/hooks/useSearchParams
* - useRouter -> @/app/[variants]/(main)/hooks/useRouter
*
* @see RFC 147: LOBE-2850 - Phase 3
*/
import { readFile, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
// Files that should be migrated to React Router version
const SPA_FILES = [
// (main) directory files using @/libs/next/navigation
'src/app/[variants]/(main)/community/_layout/Sidebar/Header/Nav.tsx',
'src/app/[variants]/(main)/group/_layout/Sidebar/Header/Nav.tsx',
'src/app/[variants]/(main)/group/_layout/Sidebar/Topic/hooks/useTopicNavigation.ts',
'src/app/[variants]/(main)/group/_layout/Sidebar/Topic/hooks/useThreadNavigation.ts',
'src/app/[variants]/(main)/chat/_layout/Sidebar/Header/Nav.tsx',
'src/app/[variants]/(main)/chat/_layout/Sidebar/Topic/hooks/useTopicNavigation.ts',
'src/app/[variants]/(main)/chat/_layout/Sidebar/Topic/hooks/useThreadNavigation.ts',
'src/app/[variants]/(main)/memory/_layout/Sidebar/Header/Nav.tsx',
];
interface MigrationResult {
changes: string[];
filePath: string;
}
async function migrateFile(relativePath: string): Promise<MigrationResult | null> {
const fullPath = join(__dirname, '..', relativePath);
const content = await readFile(fullPath, 'utf8');
let newContent = content;
const changes: string[] = [];
// Check what hooks are being imported from @/libs/next/navigation
const importMatch = content.match(
/import\s*\{([^}]+)\}\s*from\s*["']@\/libs\/next\/navigation["']/,
);
if (!importMatch) {
console.log(`⏭️ ${relativePath} - No @/libs/next/navigation import found`);
return null;
}
const importedHooks = importMatch[1]
.split(',')
.map((s) => s.trim())
.filter(Boolean);
console.log(`📝 ${relativePath}`);
console.log(` Imported hooks: ${importedHooks.join(', ')}`);
// Build new imports
const newImports: string[] = [];
for (const hook of importedHooks) {
switch (hook) {
case 'usePathname': {
newImports.push(`import { usePathname } from '@/app/[variants]/(main)/hooks/usePathname';`);
changes.push('usePathname -> React Router version');
break;
}
case 'useSearchParams': {
newImports.push(
`import { useSearchParams } from '@/app/[variants]/(main)/hooks/useSearchParams';`,
);
changes.push('useSearchParams -> React Router version');
break;
}
case 'useRouter': {
newImports.push(`import { useRouter } from '@/app/[variants]/(main)/hooks/useRouter';`);
changes.push('useRouter -> React Router version');
break;
}
default: {
// Keep other imports (like notFound, redirect) from next/navigation
console.log(` ⚠️ Unknown hook "${hook}" - keeping original import`);
}
}
}
if (newImports.length === 0) {
console.log(` ⏭️ No hooks to migrate`);
return null;
}
// Replace the old import with new imports
newContent = newContent.replace(
/import\s*\{[^}]+\}\s*from\s*["']@\/libs\/next\/navigation["'];?\n?/,
newImports.join('\n') + '\n',
);
if (newContent !== content) {
await writeFile(fullPath, newContent, 'utf8');
for (const change of changes) {
console.log(`${change}`);
}
return { changes, filePath: relativePath };
}
return null;
}
async function main() {
console.log('🚀 Starting SPA navigation migration...\n');
const results: MigrationResult[] = [];
for (const file of SPA_FILES) {
try {
const result = await migrateFile(file);
if (result) {
results.push(result);
}
} catch (error) {
console.error(`❌ Error processing ${file}:`, error);
}
}
console.log('\n' + '='.repeat(60));
console.log(`📊 Migration Summary:`);
console.log(` - Files processed: ${SPA_FILES.length}`);
console.log(` - Files modified: ${results.length}`);
console.log('\n✨ SPA navigation migration complete!');
}
await main();