fix update interactive to keep npm aliases (#23903)

### What does this PR do?

fixes #23901

### How did you verify your code works?

with a test

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Michael H
2025-11-03 21:12:24 +11:00
committed by GitHub
parent bdaab89253
commit d76fad3618
2 changed files with 77 additions and 3 deletions

View File

@@ -1981,11 +1981,40 @@ fn updateNamedCatalog(
}
fn preserveVersionPrefix(original_version: string, new_version: string, allocator: std.mem.Allocator) !string {
if (original_version.len > 0) {
const first_char = original_version[0];
if (original_version.len > 1) {
var orig_version = original_version;
var alias: ?string = null;
// Preserve npm: prefix
if (strings.withoutPrefixIfPossibleComptime(original_version, "npm:")) |after_npm| {
if (strings.lastIndexOfChar(after_npm, '@')) |i| {
alias = after_npm[0..i];
if (i + 2 < after_npm.len) {
orig_version = after_npm[i + 1 ..];
}
} else {
alias = after_npm;
}
}
// Preserve other version prefixes
const first_char = orig_version[0];
if (first_char == '^' or first_char == '~' or first_char == '>' or first_char == '<' or first_char == '=') {
const second_char = orig_version[1];
if ((first_char == '>' or first_char == '<') and second_char == '=') {
if (alias) |a| {
return try std.fmt.allocPrint(allocator, "npm:{s}@{c}={s}", .{ a, first_char, new_version });
}
return try std.fmt.allocPrint(allocator, "{c}={s}", .{ first_char, new_version });
}
if (alias) |a| {
return try std.fmt.allocPrint(allocator, "npm:{s}@{c}{s}", .{ a, first_char, new_version });
}
return try std.fmt.allocPrint(allocator, "{c}{s}", .{ first_char, new_version });
}
if (alias) |a| {
return try std.fmt.allocPrint(allocator, "npm:{s}@{s}", .{ a, new_version });
}
}
return try allocator.dupe(u8, new_version);
}