Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: version range handling in update_pkg function #11716

Merged
merged 20 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silly-donkeys-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-migrate": minor
---

fix: version range handling in update_pkg function
codenoid marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 21 additions & 1 deletion packages/migrate/migrations/sveltekit-2/migrate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { assert, test } from 'vitest';
import {
transform_code,
update_svelte_config_content,
update_tsconfig_content
update_tsconfig_content,
update_pkg_json_content
} from './migrate.js';
import { read_samples } from '../../utils.js';

Expand Down Expand Up @@ -30,3 +31,22 @@ for (const sample of read_samples(new URL('./tsjs-samples.md', import.meta.url))
assert.equal(actual, sample.after);
});
}

test('Does not downgrade versions', () => {
codenoid marked this conversation as resolved.
Show resolved Hide resolved
const result = update_pkg_json_content(`{
"devDependencies": {
"@sveltejs/adapter-vercel": "^5.1.0",
"typescript": "github:idk"
}
}`);
assert.equal(
result,
`{
"devDependencies": {
"@sveltejs/adapter-vercel": "^5.1.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"typescript": "github:idk"
}
}`
);
});
107 changes: 59 additions & 48 deletions packages/migrate/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,54 +205,65 @@ export function posixify(str) {
* @param {Array<[string, string, string?, ('dependencies' | 'devDependencies')?]>} updates
*/
export function update_pkg(content, updates) {
const indent = content.split('\n')[1].match(/^\s+/)?.[0] || ' ';
const pkg = JSON.parse(content);

/**
* @param {string} name
* @param {string} version
* @param {string} [additional]
* @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;
}
}

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;
}
}

if (insert && !pkg[insert]?.[name]) {
if (!pkg[insert]) pkg[insert] = {};

// Insert the property in sorted position without adjusting other positions so diffs are easier to read
const sorted_keys = Object.keys(pkg[insert]).sort();
const index = sorted_keys.findIndex((key) => name.localeCompare(key) === -1);
const insert_index = index !== -1 ? index : sorted_keys.length;
const new_properties = Object.entries(pkg[insert]);
new_properties.splice(insert_index, 0, [name, version]);
pkg[insert] = Object.fromEntries(new_properties);

log_migration(`Added ${name} version ${version} ${additional}`);
}
}

for (const update of updates) {
update_pkg(...update);
}

return JSON.stringify(pkg, null, indent);
const indent = content.split('\n')[1].match(/^\s+/)?.[0] || ' ';
codenoid marked this conversation as resolved.
Show resolved Hide resolved
const pkg = JSON.parse(content);

/**
* @param {string} name
* @param {string} version
* @param {string} [additional]
* @param {'dependencies' | 'devDependencies' | undefined} [insert]
*/
function update_pkg(name, version, additional = '', insert) {
/**
* @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;
}
}
};

updateVersion('dependencies');
updateVersion('devDependencies');

if (insert && !pkg[insert]?.[name]) {
if (!pkg[insert]) pkg[insert] = {};

// Insert the property in sorted position without adjusting other positions so diffs are easier to read
const sorted_keys = Object.keys(pkg[insert]).sort();
const index = sorted_keys.findIndex((key) => name.localeCompare(key) === -1);
const insert_index = index !== -1 ? index : sorted_keys.length;
const new_properties = Object.entries(pkg[insert]);
new_properties.splice(insert_index, 0, [name, version]);
pkg[insert] = Object.fromEntries(new_properties);

log_migration(`Added ${name} version ${version} ${additional}`);
}
}

for (const update of updates) {
update_pkg(...update);
}

return JSON.stringify(pkg, null, indent);
}

const logged_migrations = new Set();
Expand Down
Loading