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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor update_pkg function in utils.js
  • Loading branch information
codenoid committed Jan 23, 2024
commit 5ce6300219f4c701f9ce3c430c34014f81ee53b8
111 changes: 59 additions & 52 deletions packages/migrate/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,58 +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) {
/**
* @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
if (semver.gt(semver.minVersion(version), semver.minVersion(existingRange))) {
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 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