|
| 1 | +import colors from 'kleur'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import process from 'node:process'; |
| 4 | +import prompts from 'prompts'; |
| 5 | +import semver from 'semver'; |
| 6 | +import glob from 'tiny-glob/sync.js'; |
| 7 | +import { bail, check_git, update_svelte_file } from '../../utils.js'; |
| 8 | +import { transform_svelte_code, update_pkg_json } from './migrate.js'; |
| 9 | + |
| 10 | +export async function migrate() { |
| 11 | + if (!fs.existsSync('package.json')) { |
| 12 | + bail('Please re-run this script in a directory with a package.json'); |
| 13 | + } |
| 14 | + |
| 15 | + const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); |
| 16 | + |
| 17 | + const svelte_dep = pkg.devDependencies?.svelte ?? pkg.dependencies?.svelte; |
| 18 | + if (svelte_dep && semver.validRange(svelte_dep) && semver.gtr('5.0.0', svelte_dep)) { |
| 19 | + console.log( |
| 20 | + colors |
| 21 | + .bold() |
| 22 | + .red('\nYou need to upgrade to Svelte version 5 first (`npx sv migrate svelte-5`).\n') |
| 23 | + ); |
| 24 | + process.exit(1); |
| 25 | + } |
| 26 | + |
| 27 | + const kit_dep = pkg.devDependencies?.['@sveltejs/kit'] ?? pkg.dependencies?.['@sveltejs/kit']; |
| 28 | + if (kit_dep && semver.validRange(kit_dep) && semver.gtr('2.0.0', kit_dep)) { |
| 29 | + console.log( |
| 30 | + colors |
| 31 | + .bold() |
| 32 | + .red('\nYou need to upgrade to SvelteKit version 2 first (`npx sv migrate sveltekit-2`).\n') |
| 33 | + ); |
| 34 | + process.exit(1); |
| 35 | + } |
| 36 | + |
| 37 | + console.log( |
| 38 | + colors |
| 39 | + .bold() |
| 40 | + .yellow( |
| 41 | + '\nThis will update files in the current directory\n' + |
| 42 | + "If you're inside a monorepo, don't run this in the root directory, rather run it in all projects independently.\n" |
| 43 | + ) |
| 44 | + ); |
| 45 | + |
| 46 | + const use_git = check_git(); |
| 47 | + |
| 48 | + const response = await prompts({ |
| 49 | + type: 'confirm', |
| 50 | + name: 'value', |
| 51 | + message: 'Continue?', |
| 52 | + initial: false |
| 53 | + }); |
| 54 | + |
| 55 | + if (!response.value) { |
| 56 | + process.exit(1); |
| 57 | + } |
| 58 | + |
| 59 | + const folders = await prompts({ |
| 60 | + type: 'multiselect', |
| 61 | + name: 'value', |
| 62 | + message: 'Which folders should be migrated?', |
| 63 | + choices: fs |
| 64 | + .readdirSync('.') |
| 65 | + .filter( |
| 66 | + (dir) => fs.statSync(dir).isDirectory() && dir !== 'node_modules' && !dir.startsWith('.') |
| 67 | + ) |
| 68 | + .map((dir) => ({ title: dir, value: dir, selected: true })) |
| 69 | + }); |
| 70 | + |
| 71 | + if (!folders.value?.length) { |
| 72 | + process.exit(1); |
| 73 | + } |
| 74 | + |
| 75 | + update_pkg_json(); |
| 76 | + |
| 77 | + // For some reason {folders.value.join(',')} as part of the glob doesn't work and returns less files |
| 78 | + const files = folders.value.flatMap( |
| 79 | + /** @param {string} folder */ (folder) => |
| 80 | + glob(`${folder}/**`, { filesOnly: true, dot: true }) |
| 81 | + .map((file) => file.replace(/\\/g, '/')) |
| 82 | + .filter( |
| 83 | + (file) => |
| 84 | + !file.includes('/node_modules/') && |
| 85 | + // We're not transforming usage inside .ts/.js files since you can't use the $store syntax there, |
| 86 | + // and therefore have to either subscribe or pass it along, which we can't auto-migrate |
| 87 | + file.endsWith('.svelte') |
| 88 | + ) |
| 89 | + ); |
| 90 | + |
| 91 | + for (const file of files) { |
| 92 | + update_svelte_file( |
| 93 | + file, |
| 94 | + (code) => code, |
| 95 | + (code) => transform_svelte_code(code) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + console.log(colors.bold().green('✔ Your project has been migrated')); |
| 100 | + |
| 101 | + console.log('\nRecommended next steps:\n'); |
| 102 | + |
| 103 | + const cyan = colors.bold().cyan; |
| 104 | + |
| 105 | + const tasks = [ |
| 106 | + "install the updated dependencies ('npm i' / 'pnpm i' / etc) " + use_git && |
| 107 | + cyan('git commit -m "migration to $app/state"') |
| 108 | + ].filter(Boolean); |
| 109 | + |
| 110 | + tasks.forEach((task, i) => { |
| 111 | + console.log(` ${i + 1}: ${task}`); |
| 112 | + }); |
| 113 | + |
| 114 | + console.log(''); |
| 115 | + |
| 116 | + if (use_git) { |
| 117 | + console.log(`Run ${cyan('git diff')} to review changes.\n`); |
| 118 | + } |
| 119 | +} |
0 commit comments