You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte-kit-vice/scripts/fix-context-imports.ts

136 lines
8.3 KiB

/**
* Actualiza imports de contextos que ya no existen
*/
import { readFileSync, writeFileSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const terraDir = join(__dirname, '../src');
// Mapeo de nombres de contextos antiguos a nuevos patrones
const contextMap: Record<string, { class: string; newPattern: string }> = {
'AccordionRootContext': { class: 'AccordionRoot', newPattern: 'AccordionRoot.ctx' },
'AccordionItemContext': { class: 'AccordionItemState', newPattern: 'AccordionItemState.ctx' },
'AvatarRootContext': { class: 'AvatarRootState', newPattern: 'AvatarRootState.ctx' },
'CalendarCellContext': { class: 'CalendarCellState', newPattern: 'CalendarCellState.ctx' },
'CalendarRootContext': { class: 'CalendarRootState', newPattern: 'CalendarRootState.ctx' },
'CheckboxGroupContext': { class: 'CheckboxGroupState', newPattern: 'CheckboxGroupState.ctx' },
'CheckboxRootContext': { class: 'CheckboxRootState', newPattern: 'CheckboxRootState.ctx' },
'CollapsibleRootContext': { class: 'CollapsibleRootState', newPattern: 'CollapsibleRootState.ctx' },
'CommandGroupContainerContext': { class: 'CommandGroupContainerState', newPattern: 'CommandGroupContainerState.ctx' },
'CommandListContext': { class: 'CommandListState', newPattern: 'CommandListState.ctx' },
'CommandRootContext': { class: 'CommandRootState', newPattern: 'CommandRootState.ctx' },
'DateFieldRootContext': { class: 'DateFieldRootState', newPattern: 'DateFieldRootState.ctx' },
'DatePickerRootContext': { class: 'DatePickerRootState', newPattern: 'DatePickerRootState.ctx' },
'DateRangeFieldRootContext': { class: 'DateRangeFieldRootState', newPattern: 'DateRangeFieldRootState.ctx' },
'DateRangePickerRootContext': { class: 'DateRangePickerRootState', newPattern: 'DateRangePickerRootState.ctx' },
'DialogRootContext': { class: 'DialogRootState', newPattern: 'DialogRootState.ctx' },
'EditableRootContext': { class: 'EditableRootState', newPattern: 'EditableRootState.ctx' },
'FieldRootContext': { class: 'FieldRootState', newPattern: 'FieldRootState.ctx' },
'FileUploadItemContext': { class: 'FileUploadItemState', newPattern: 'FileUploadItemState.ctx' },
'FileUploadRootContext': { class: 'FileUploadRootState', newPattern: 'FileUploadRootState.ctx' },
'FormFieldContext': { class: 'FormFieldState', newPattern: 'FormFieldState.ctx' },
'FormRootContext': { class: 'FormRootState', newPattern: 'FormRootState.ctx' },
'LinkPreviewRootContext': { class: 'LinkPreviewRootState', newPattern: 'LinkPreviewRootState.ctx' },
'MenuCheckboxGroupContext': { class: 'MenuCheckboxGroupState', newPattern: 'MenuCheckboxGroupState.ctx' },
'MenuContentContext': { class: 'MenuContentState', newPattern: 'MenuContentState.ctx' },
'MenuMenuContext': { class: 'MenuMenuState', newPattern: 'MenuMenuState.ctx' },
'MenuRadioGroupContext': { class: 'MenuRadioGroupState', newPattern: 'MenuRadioGroupState.ctx' },
'MenuRootContext': { class: 'MenuRootState', newPattern: 'MenuRootState.ctx' },
'MenubarMenuContext': { class: 'MenubarMenuState', newPattern: 'MenubarMenuState.ctx' },
'MenubarRootContext': { class: 'MenubarRootState', newPattern: 'MenubarRootState.ctx' },
'NumberFieldRootContext': { class: 'NumberFieldRootState', newPattern: 'NumberFieldRootState.ctx' },
'PaginationRootContext': { class: 'PaginationRootState', newPattern: 'PaginationRootState.ctx' },
'PopoverRootContext': { class: 'PopoverRootState', newPattern: 'PopoverRootState.ctx' },
'RadioGroupRootContext': { class: 'RadioGroupRootState', newPattern: 'RadioGroupRootState.ctx' },
'RangeCalendarCellContext': { class: 'RangeCalendarCellState', newPattern: 'RangeCalendarCellState.ctx' },
'RatingGroupRootContext': { class: 'RatingGroupRootState', newPattern: 'RatingGroupRootState.ctx' },
'ScrollAreaScrollbarAxisContext': { class: 'ScrollbarAxis', newPattern: 'ScrollbarAxis.ctx' },
'ScrollAreaScrollbarSharedContext': { class: 'ScrollAreaScrollbarSharedState', newPattern: 'ScrollAreaScrollbarSharedState.ctx' },
'ScrollAreaScrollbarVisibleContext': { class: 'ScrollAreaScrollbarVisibleState', newPattern: 'ScrollAreaScrollbarVisibleState.ctx' },
'ScrollAreaScrollbarContext': { class: 'ScrollAreaScrollbarState', newPattern: 'ScrollAreaScrollbarState.ctx' },
'ScrollAreaRootContext': { class: 'ScrollAreaRootState', newPattern: 'ScrollAreaRootState.ctx' },
'SelectContentContext': { class: 'SelectContentState', newPattern: 'SelectContentState.ctx' },
'SelectGroupContext': { class: 'SelectGroupState', newPattern: 'SelectGroupState.ctx' },
'SelectRootContext': { class: 'SelectRoot', newPattern: 'SelectRoot.ctx' },
'SliderRootContext': { class: 'SliderRoot', newPattern: 'SliderRoot.ctx' },
'SpinnerRootContext': { class: 'SpinnerRootState', newPattern: 'SpinnerRootState.ctx' },
'SplitterRootContext': { class: 'SplitterRootState', newPattern: 'SplitterRootState.ctx' },
'StepperItemContext': { class: 'StepperItemState', newPattern: 'StepperItemState.ctx' },
'StepperRootContext': { class: 'StepperRootState', newPattern: 'StepperRootState.ctx' },
'SwitchRootContext': { class: 'SwitchRootState', newPattern: 'SwitchRootState.ctx' },
'TabsRootContext': { class: 'TabsRootState', newPattern: 'TabsRootState.ctx' },
'TagsInputRootContext': { class: 'TagsInputRootState', newPattern: 'TagsInputRootState.ctx' },
'TagsInputTagContext': { class: 'TagsInputTagState', newPattern: 'TagsInputTagState.ctx' },
'TimeFieldRootContext': { class: 'TimeFieldRootState', newPattern: 'TimeFieldRootState.ctx' },
'TimeRangeFieldRootContext': { class: 'TimeRangeFieldRootState', newPattern: 'TimeRangeFieldRootState.ctx' },
'ToastRootContext': { class: 'ToastRootState', newPattern: 'ToastRootState.ctx' },
'ToggleGroupRootContext': { class: 'ToggleGroupRootState', newPattern: 'ToggleGroupRootState.ctx' },
'ToolbarGroupContext': { class: 'ToolbarGroupState', newPattern: 'ToolbarGroupState.ctx' },
'ToolbarRootContext': { class: 'ToolbarRootState', newPattern: 'ToolbarRootState.ctx' },
'TooltipProviderContext': { class: 'TooltipProviderState', newPattern: 'TooltipProviderState.ctx' },
'TooltipRootContext': { class: 'TooltipRootState', newPattern: 'TooltipRootState.ctx' },
'FloatingTooltipRootContext': { class: 'FloatingRootState', newPattern: 'FloatingRootState.ctx' },
'FloatingContentContext': { class: 'FloatingContentState', newPattern: 'FloatingContentState.ctx' },
};
function findAllFiles(dir: string): string[] {
const files: string[] = [];
const entries = readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory() && !entry.name.startsWith('.') && entry.name !== 'node_modules') {
files.push(...findAllFiles(fullPath));
} else if (entry.isFile() && (entry.name.endsWith('.svelte') || entry.name.endsWith('.ts'))) {
files.push(fullPath);
}
}
return files;
}
function fixFile(filePath: string): boolean {
const content = readFileSync(filePath, 'utf-8');
let modified = content;
// Para cada contexto en el mapa
for (const [oldContext, info] of Object.entries(contextMap)) {
// 1. Eliminar el contexto de los imports si no se usa elsewhere en el archivo
const importRegex = new RegExp(`,?\\s*${oldContext}\\s*,?\\s*`, 'g');
const hasOldImport = new RegExp(`\\b${oldContext}\\b`).test(content);
if (!hasOldImport) continue;
// 2. Reemplazar referencias al contexto con el nuevo patrón
modified = modified.replace(new RegExp(`\\b${oldContext}\\.set\\(`, 'g'), `${info.newPattern}.set(`);
modified = modified.replace(new RegExp(`\\b${oldContext}\\.get\\(`, 'g'), `${info.newPattern}.get(`);
modified = modified.replace(new RegExp(`\\b${oldContext}\\.getOr\\(`, 'g'), `${info.newPattern}.getOr(`);
}
if (modified !== content) {
writeFileSync(filePath, modified, 'utf-8');
return true;
}
return false;
}
const files = findAllFiles(terraDir);
let modified = 0;
console.log('🔧 Actualizando referencias a contextos...\n');
for (const file of files) {
if (fixFile(file)) {
modified++;
const relPath = file.replace(/.*src\\/, '').replace(/.*src\//, '');
console.log(` ✅ ${relPath}`);
}
}
console.log(`\n📊 Resumen:`);
console.log(` Modificados: ${modified}`);
console.log(` Total procesados: ${files.length}`);

Powered by TurnKey Linux.