Skip to content

Commit 64cfdaf

Browse files
committed
refactor(migration): improve framework shim detection logic for better accuracy
1 parent d2624ad commit 64cfdaf

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

packages/cli/src/migration/bin.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,11 @@ async function collectMigrationPlan(
530530
const frameworkShimFrameworks: Framework[] = [];
531531
for (const framework of allDetectedFrameworks) {
532532
const anyMissingShim =
533-
!hasFrameworkShim(rootDir, framework) ||
534-
(packages ?? []).some((pkg) => !hasFrameworkShim(path.join(rootDir, pkg.path), framework));
533+
(detectFramework(rootDir).includes(framework) && !hasFrameworkShim(rootDir, framework)) ||
534+
(packages ?? []).some((pkg) => {
535+
const pkgPath = path.join(rootDir, pkg.path);
536+
return detectFramework(pkgPath).includes(framework) && !hasFrameworkShim(pkgPath, framework);
537+
});
535538
if (anyMissingShim) {
536539
const addShim = await confirmFrameworkShim(framework, options.interactive);
537540
if (addShim) {

packages/cli/src/migration/migrator.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -732,16 +732,32 @@ function getEnvDtsPath(projectPath: string): string {
732732
}
733733

734734
export function hasFrameworkShim(projectPath: string, framework: Framework): boolean {
735-
const envDtsPath = getEnvDtsPath(projectPath);
736-
if (!fs.existsSync(envDtsPath)) {
737-
return false;
738-
}
739-
740-
const content = fs.readFileSync(envDtsPath, 'utf-8');
741-
if (framework === 'astro') {
742-
return content.includes('astro/client');
735+
const dirsToScan = [projectPath, path.join(projectPath, 'src')];
736+
for (const dir of dirsToScan) {
737+
if (!fs.existsSync(dir)) {
738+
continue;
739+
}
740+
let entries: string[];
741+
try {
742+
entries = fs.readdirSync(dir);
743+
} catch {
744+
continue;
745+
}
746+
for (const entry of entries) {
747+
if (!entry.endsWith('.d.ts')) {
748+
continue;
749+
}
750+
const content = fs.readFileSync(path.join(dir, entry), 'utf-8');
751+
if (framework === 'astro') {
752+
if (content.includes('astro/client')) {
753+
return true;
754+
}
755+
} else if (content.includes(`'*.${framework}'`) || content.includes(`"*.${framework}"`)) {
756+
return true;
757+
}
758+
}
743759
}
744-
return content.includes(`'*.${framework}'`) || content.includes(`"*.${framework}"`);
760+
return false;
745761
}
746762

747763
export function addFrameworkShim(

0 commit comments

Comments
 (0)