diff --git a/.changeset/silly-donkeys-shout.md b/.changeset/silly-donkeys-shout.md new file mode 100644 index 000000000000..a1879cdc773d --- /dev/null +++ b/.changeset/silly-donkeys-shout.md @@ -0,0 +1,5 @@ +--- +"svelte-migrate": patch +--- + +fix: don't downgrade versions when bumping dependencies diff --git a/packages/migrate/README.md b/packages/migrate/README.md index 83f0892ad1dc..fbb6685678f4 100644 --- a/packages/migrate/README.md +++ b/packages/migrate/README.md @@ -3,6 +3,7 @@ A CLI for migrating Svelte(Kit) codebases. Run it using + ``` npx svelte-migrate [migration] ``` diff --git a/packages/migrate/migrations/package/migrate_pkg.js b/packages/migrate/migrations/package/migrate_pkg.js index eeb751f1d97e..3e5f5b6ab273 100644 --- a/packages/migrate/migrations/package/migrate_pkg.js +++ b/packages/migrate/migrations/package/migrate_pkg.js @@ -34,10 +34,10 @@ function analyze(config, file) { const dest = svelte_extension ? name.slice(0, -svelte_extension.length) + '.svelte' : name.endsWith('.d.ts') - ? name - : name.endsWith('.ts') - ? name.slice(0, -3) + '.js' - : name; + ? name + : name.endsWith('.ts') + ? name.slice(0, -3) + '.js' + : name; return { name, diff --git a/packages/migrate/migrations/routes/migrate_page_js/index.js b/packages/migrate/migrations/routes/migrate_page_js/index.js index e30b005a57f2..2fdb5e66d942 100644 --- a/packages/migrate/migrations/routes/migrate_page_js/index.js +++ b/packages/migrate/migrations/routes/migrate_page_js/index.js @@ -90,8 +90,8 @@ export function migrate_page(content, filename) { const message = is_string_like(nodes.error) ? nodes.error.getText() : is_new(nodes.error, 'Error') - ? /** @type {string | undefined} */ (nodes.error.arguments[0]?.getText()) - : false; + ? /** @type {string | undefined} */ (nodes.error.arguments[0]?.getText()) + : false; if (message !== false) { automigration( diff --git a/packages/migrate/migrations/routes/migrate_page_server/index.js b/packages/migrate/migrations/routes/migrate_page_server/index.js index db5f77b69cbc..d6556e7ac744 100644 --- a/packages/migrate/migrations/routes/migrate_page_server/index.js +++ b/packages/migrate/migrations/routes/migrate_page_server/index.js @@ -46,8 +46,8 @@ export function migrate_page_server(content, filename) { const load_name = match?.[3] ? `LayoutServerLoad.${match[3]}` : match?.[2] - ? 'LayoutServerLoad' - : 'PageServerLoad'; + ? 'LayoutServerLoad' + : 'PageServerLoad'; const has_load = file.exports.map.has('GET'); const has_action = non_get_methods.some((method) => file.exports.map.has(method)); diff --git a/packages/migrate/migrations/routes/migrate_server/index.js b/packages/migrate/migrations/routes/migrate_server/index.js index eeaa902ee965..d9092d554651 100644 --- a/packages/migrate/migrations/routes/migrate_server/index.js +++ b/packages/migrate/migrations/routes/migrate_server/index.js @@ -147,7 +147,7 @@ export function migrate_server(content) { init.length > 0 ? `new Response(${body}, {${join_whitespace}${init.join( `,${join_whitespace}` - )}${end_whitespace}})` + )}${end_whitespace}})` : `new Response(${body})`; if (safe_body) { diff --git a/packages/migrate/migrations/svelte-4/migrate.spec.js b/packages/migrate/migrations/svelte-4/migrate.spec.js index d3c66b5dc46b..f0c34cddd1da 100644 --- a/packages/migrate/migrations/svelte-4/migrate.spec.js +++ b/packages/migrate/migrations/svelte-4/migrate.spec.js @@ -363,7 +363,6 @@ test('Update package.json', () => { ); }); - test('Does not downgrade versions', () => { const result = update_pkg_json_content(`{ "devDependencies": { diff --git a/packages/migrate/package.json b/packages/migrate/package.json index 51aa21f224f9..f3f936e44678 100644 --- a/packages/migrate/package.json +++ b/packages/migrate/package.json @@ -41,6 +41,8 @@ }, "scripts": { "test": "vitest run --silent", - "check": "tsc" + "check": "tsc", + "lint": "prettier --check .", + "format": "pnpm lint --write" } } diff --git a/packages/migrate/utils.js b/packages/migrate/utils.js index a01be4bde3f6..71ec9e44e979 100644 --- a/packages/migrate/utils.js +++ b/packages/migrate/utils.js @@ -215,23 +215,30 @@ export function update_pkg(content, updates) { * @param {'dependencies' | 'devDependencies' | undefined} [insert] */ function update_pkg(name, version, additional = '', insert) { - if (pkg.dependencies?.[name]) { - const existing_range = pkg.dependencies[name]; - - if (semver.validRange(existing_range) && !semver.subset(existing_range, version)) { - log_migration(`Updated ${name} to ${version} ${additional}`); - pkg.dependencies[name] = version; + /** + * @param {string} type + */ + const updateVersion = (type) => { + const existingRange = pkg[type]?.[name]; + + if ( + existingRange && + semver.validRange(existingRange) && + !semver.subset(existingRange, version) + ) { + // Check if the new version range is an upgrade + const minExistingVersion = semver.minVersion(existingRange); + const minNewVersion = semver.minVersion(version); + + if (minExistingVersion && minNewVersion && semver.gt(minNewVersion, minExistingVersion)) { + log_migration(`Updated ${name} to ${version}`); + pkg[type][name] = version; + } } - } - - if (pkg.devDependencies?.[name]) { - const existing_range = pkg.devDependencies[name]; + }; - if (semver.validRange(existing_range) && !semver.subset(existing_range, version)) { - log_migration(`Updated ${name} to ${version} ${additional}`); - pkg.devDependencies[name] = version; - } - } + updateVersion('dependencies'); + updateVersion('devDependencies'); if (insert && !pkg[insert]?.[name]) { if (!pkg[insert]) pkg[insert] = {}; diff --git a/packages/migrate/utils.spec.js b/packages/migrate/utils.spec.js index cd9437e2968f..a75641e6055f 100644 --- a/packages/migrate/utils.spec.js +++ b/packages/migrate/utils.spec.js @@ -1,73 +1,93 @@ -import { assert, test } from 'vitest'; -import { update_pkg } from './utils.js'; - -test('Inserts package at correct position (1)', () => { - const result = update_pkg( - `{ - "dependencies": { - "a": "1", - "z": "3", - "c": "4" - } -}`, - [['b', '2', '', 'dependencies']] - ); - - assert.equal( - result, - `{ - "dependencies": { - "a": "1", - "b": "2", - "z": "3", - "c": "4" - } -}` - ); -}); - -test('Inserts package at correct position (2)', () => { - const result = update_pkg( - `{ - "dependencies": { - "a": "1", - "b": "2" - } -}`, - [['c', '3', '', 'dependencies']] - ); - - assert.equal( - result, - `{ - "dependencies": { - "a": "1", - "b": "2", - "c": "3" - } -}` - ); -}); - -test('Inserts package at correct position (3)', () => { - const result = update_pkg( - `{ - "dependencies": { - "b": "2", - "c": "3" - } -}`, - [['a', '1', '', 'dependencies']] - ); - - assert.equal( - result, - `{ - "dependencies": { - "a": "1", - "b": "2", - "c": "3" - } -}` - ); -}); +import { assert, test } from 'vitest'; +import { update_pkg } from './utils.js'; + +test('Inserts package at correct position (1)', () => { + const result = update_pkg( + `{ + "dependencies": { + "a": "1", + "z": "3", + "c": "4" + } +}`, + [['b', '2', '', 'dependencies']] + ); + + assert.equal( + result, + `{ + "dependencies": { + "a": "1", + "b": "2", + "z": "3", + "c": "4" + } +}` + ); +}); + +test('Inserts package at correct position (2)', () => { + const result = update_pkg( + `{ + "dependencies": { + "a": "1", + "b": "2" + } +}`, + [['c', '3', '', 'dependencies']] + ); + + assert.equal( + result, + `{ + "dependencies": { + "a": "1", + "b": "2", + "c": "3" + } +}` + ); +}); + +test('Inserts package at correct position (3)', () => { + const result = update_pkg( + `{ + "dependencies": { + "b": "2", + "c": "3" + } +}`, + [['a', '1', '', 'dependencies']] + ); + + assert.equal( + result, + `{ + "dependencies": { + "a": "1", + "b": "2", + "c": "3" + } +}` + ); +}); + +test('Does not downgrade versions', () => { + const result = update_pkg( + `{ + "devDependencies": { + "@sveltejs/kit": "^2.4.3" + } +}`, + [['@sveltejs/kit', '^2.0.0']] + ); + + assert.equal( + result, + `{ + "devDependencies": { + "@sveltejs/kit": "^2.4.3" + } +}` + ); +});