|
| 1 | +import { execSync } from 'node:child_process'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +const UI_DIR = 'src/lib/components/ui'; |
| 6 | +const HOOKS_DIR = 'src/lib/hooks'; |
| 7 | + |
| 8 | +// Non-shadcn directories to keep |
| 9 | +const EXCLUDE = ['multi-select-combobox']; |
| 10 | + |
| 11 | +// Unwanted dependencies that the CLI tends to add |
| 12 | +const UNWANTED_DEPS = ['mode-watcher']; |
| 13 | + |
| 14 | +function run(command, options = {}) { |
| 15 | + execSync(command, { stdio: 'inherit', shell: true, ...options }); |
| 16 | +} |
| 17 | + |
| 18 | +// Collect shadcn component names |
| 19 | +const components = fs |
| 20 | + .readdirSync(UI_DIR, { withFileTypes: true }) |
| 21 | + .filter((entry) => entry.isDirectory() && !EXCLUDE.includes(entry.name)) |
| 22 | + .map((entry) => entry.name); |
| 23 | + |
| 24 | +if (components.length === 0) { |
| 25 | + console.error('No shadcn components found to update.'); |
| 26 | + process.exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +console.log(`Found ${components.length} shadcn components: ${components.join(' ')}`); |
| 30 | + |
| 31 | +// Delete all shadcn component directories |
| 32 | +console.log('Deleting shadcn components...'); |
| 33 | +for (const name of components) { |
| 34 | + fs.rmSync(path.join(UI_DIR, name), { recursive: true, force: true }); |
| 35 | +} |
| 36 | +fs.rmSync(path.join(HOOKS_DIR, 'is-mobile'), { recursive: true, force: true }); |
| 37 | + |
| 38 | +// Re-add all components |
| 39 | +console.log('Re-adding components via shadcn CLI...'); |
| 40 | +run(`pnpm dlx shadcn-svelte@latest add --yes --overwrite ${components.join(' ')}`); |
| 41 | + |
| 42 | +// Remove unwanted dependencies |
| 43 | +console.log('Cleaning up dependencies...'); |
| 44 | +const packageJson = fs.readFileSync('package.json', 'utf-8'); |
| 45 | +for (const dep of UNWANTED_DEPS) { |
| 46 | + if (packageJson.includes(`"${dep}"`)) { |
| 47 | + console.log(` Removing unwanted dependency: ${dep}`); |
| 48 | + run(`pnpm remove ${dep}`); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// Format to reduce diff noise |
| 53 | +console.log('Formatting...'); |
| 54 | +run('pnpm run format'); |
| 55 | + |
| 56 | +// Apply custom modifications |
| 57 | +console.log('Applying custom modifications...'); |
| 58 | + |
| 59 | +function patch(filePath, replacements) { |
| 60 | + let source = fs.readFileSync(filePath, 'utf-8'); |
| 61 | + for (const [pattern, replacement] of replacements) { |
| 62 | + source = source.replace(pattern, replacement); |
| 63 | + } |
| 64 | + fs.writeFileSync(filePath, source); |
| 65 | +} |
| 66 | + |
| 67 | +// Sidebar: ease-in-out and duration-300 |
| 68 | +patch(path.join(UI_DIR, 'sidebar/sidebar.svelte'), [ |
| 69 | + [/duration-200 ease-linear/g, 'duration-300 ease-in-out'], |
| 70 | +]); |
| 71 | + |
| 72 | +// Sidebar submenu: ml-* instead of mx-*, pl-* instead of px-* |
| 73 | +patch(path.join(UI_DIR, 'sidebar/sidebar-menu-sub.svelte'), [ |
| 74 | + [/mx-3\.5(.*)px-2\.5/g, 'ml-3.5$1pl-2.5'], |
| 75 | +]); |
| 76 | + |
| 77 | +// Sonner: use color-scheme-state instead of mode-watcher |
| 78 | +patch(path.join(UI_DIR, 'sonner/sonner.svelte'), [ |
| 79 | + [ |
| 80 | + 'import { mode } from "mode-watcher";', |
| 81 | + "import { colorScheme } from '$lib/state/color-scheme-state.svelte';", |
| 82 | + ], |
| 83 | + ['theme={mode.current}', 'theme={colorScheme.value}'], |
| 84 | +]); |
| 85 | + |
| 86 | +// Slider: add cursor-w-resize to thumb |
| 87 | +patch(path.join(UI_DIR, 'slider/slider.svelte'), [ |
| 88 | + [/select-none disabled:pointer/, 'cursor-w-resize select-none disabled:pointer'], |
| 89 | +]); |
| 90 | + |
| 91 | +// Toggle group: suppress state_referenced_locally warnings |
| 92 | +patch(path.join(UI_DIR, 'toggle-group/toggle-group.svelte'), [ |
| 93 | + [/\tsetToggleGroupCtx/, '\t// svelte-ignore state_referenced_locally\n\tsetToggleGroupCtx'], |
| 94 | +]); |
| 95 | + |
| 96 | +// Final format and check |
| 97 | +console.log('Final format...'); |
| 98 | +run('pnpm run format'); |
| 99 | + |
| 100 | +console.log('Running checks...'); |
| 101 | +run('pnpm run check'); |
| 102 | + |
| 103 | +console.log('Done! Review the git diff before committing.'); |
0 commit comments