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

Update only major versions #1286

Closed
mslowiak opened this issue Mar 27, 2023 · 6 comments · Fixed by #1290
Closed

Update only major versions #1286

mslowiak opened this issue Mar 27, 2023 · 6 comments · Fixed by #1290

Comments

@mslowiak
Copy link
Contributor

mslowiak commented Mar 27, 2023

Hi!
I was playing around with npm-check-updates and did not found an option to update only major versions (excluding potential updates for minor/patch libraries).

Why I need this?
Updating deps update is tedious task for each I have the Jenkins job.
For the minor/patch updates, I would like to deploy update dependencies automatically (automated work)
For the major updates, I want to review the changes before I deploy new application version (manual work)

Example:
application package.json contains following libraries:

  • a - 1.2.0
  • b - 13.0.0
  • c - 5.0.1

There newest versions in artifactory:

  • a - 1.3.0
  • b - 14.1.1
  • c - 5.0.3

Expectation:
Only version of artifact b should be updated from 13.0.0 to 14.1.1 because that is the major change in the example.
Currently there is a target option, but my use case is not supported with any of options there.

Workaround:

  1. Execute npx npm-check-updates --format group
  2. Parse the output from point 1 and get arfifacts names from Major section
  3. Run npm-check-updates with regex based on artifacts found in point 2

Maybe there can be added new target option called majorOnly that will work like I described?

@raineorshine
Copy link
Owner

Hi, thanks for the suggestion.

This would likely be implemented as a new filterResults option which would allow ad hoc conditions based on the old and new versions. --target minor and --target patch, which appear to be closest functionally, actually match against all published versions. I assume you want to stick to the version published to the latest tag.

A dedicated option for this feature is possible if this issue gets more upvotes.

FYI If you use ^ ranges, then you could use --minimal to achieve your desired behavior.

@mslowiak
Copy link
Contributor Author

--target minor and --target patch, which appear to be closest functionally, actually match against all published versions. I assume you want to stick to the version published to the latest tag.

I am not sure if I understand correctly but for my use case I am interested only in major versions.
The --target latest returns majors, minors, and patches.

FYI If you use ^ ranges, then you could use --minimal to achieve your desired behavior.
That would be an option, but taking into account multiple repository changes it's not feasible.

You mentioned the filterResults. Is the work regarding that thing already started?
If there is any option to help develop something in this direction, let me know 👍

@raineorshine
Copy link
Owner

raineorshine commented Mar 27, 2023

The filterResults option (or filterMeta as it has been called), was first proposed in #833. I did some exploration, and it takes some work to get the publish time, so I didn't get anything working for that specific use case.

In your case, the published version is already available, so it's just a matter of wiring up a filter function that is run after the fetch rather than before (like the current filter and filterVersion options). The majorOnly functionality would be implemented "in userland", i.e. in your .ncurc.js file.

Let me know if you're interested in working on this and I can point you towards the code when I'm back at my computer.

@mslowiak
Copy link
Contributor Author

That would be great if you could put some light on the code, maybe that will help to get it done faster 👍

@raineorshine
Copy link
Owner

raineorshine commented Mar 28, 2023

The new filterResults option can be added to https://github.com/raineorshine/npm-check-updates/blob/main/src/cli-options.ts. This makes options.filterResults available throughout the codebase. filterResults will only work in the .ncurc.js file, not on the command line, since it is a function.

I would recommend the following signature for the user-provided filterResults function (see explanation of types below):

export type FilterResultsFunction = (packageName: string, { 
  currentVersion: VersionSpec, 
  currentVersionSemver: SemVer[], 
  upgradedVersion: Version
}) => boolean

(I chose this signature to be somewhat future-proof; removing/changing arguments would require a major version change, but adding keys to the argument object would not.)

I think the best place to filter the upgraded packages is after queryVersions is called, here:

const latestVersions = keyValueBy(latestVersionResults, (dep, result) =>
result?.version
? {
[dep]: result.version,
}
: null,

You can add && (!options.filterResults || options.filterResults(...)) to the keyValueBy and it will filter out upgrades that fail the predicate. You will need to pass the appropriate arguments to the user-provided filterResults function:

  • currentVersion - currentDependencies[dep] (available in scope)
  • currentVersionSemver - parseRange(currentDependencies[dep]) (parseRange is exported from 'semver-utils')
  • upgradedVersion - result.version (available in scope)

Let me know if you need any additional guidance!

@mslowiak
Copy link
Contributor Author

Wow, that is amazing @raineorshine 🚀

I will take a detailed look into code, probably on the weekend.
Thanks for the tips! I will get back to you if I spot any obstacles or have related questions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants