node-gradle / gradle-node-plugin

Gradle plugin for integrating NodeJS in your build. :rocket:
Apache License 2.0
601 stars 117 forks source link

Add support for .nvmrc file #224

Open ppfeiler opened 2 years ago

ppfeiler commented 2 years ago

Would it be possible integrate this plugin with the .nvmrc file? A lot of projects and developers are using .nvm for managing the required node version per project with that .nvmrc file.

It would be awesome if the plugin could support it because then you don't have to maintain 2 places (build.gradle and .nvmrc) for the needed node version.

deepy commented 2 years ago

Would the .nvmrc reside in the root of the project or in the project where the plugin is actually installed?

It's a good idea and at a minimum there'll be an example of how to do this

ppfeiler commented 2 years ago

Usually the .nvmrc files is next to the (main) package.json. So where the plugin is installed. But it could be configurable with an property.

I tried to add .nvmrc support in my project (just reading the content of nvmrc and set it as the nodeVersion variable) but nvm has a little bit different version mapping. For example 16 is a valid version for .nvm which gets referenced to latest 16.x.x. The plugin can not handle node versions specified as 16.

deepy commented 2 years ago

Ah if they allow those version numbers that's going to be tricky (but not in the future!).

I'm currently doing some prototyping around moving the node/npm downloading/setup into Gradle's Shared Build Services and with that 16 should become a valid version.

If all goes well it's going to ship as an experimental and disabled-by-default feature in the next major. At that point there's going to be an util that allows you to transform a given version into an actual version (i.e. 16 would turn into 16.14.2)

deepy commented 2 years ago

Although if you are very crafty and like workarounds you could use something like https://github.com/node-gradle/gradle-node-plugin/issues/54#issuecomment-581950868 :grinning:

If you parse the links on https://nodejs.org/dist/ and turn those into versions, then you could also parse the nvm file and count the dots in the version, if they're less than 3 add .+ at the end and It Will Work™

Well, mostly, there's two camps on what 16 means and 16.+ will get always get you the latest version of 16 that can be found

wickkidd commented 1 year ago

Being able to use renovate with gradle-node would be awesome. Using common files like .nvmrc and .node-version would probably be all that's needed.

https://docs.renovatebot.com/node/

I filed an issue with them as well https://github.com/renovatebot/renovate/issues/18746

deepy commented 1 year ago

If you're ok with full version numbers in there you could use the same pattern as https://github.com/node-gradle/gradle-node-plugin/issues/232 version and npmVersion are both properties so they could be calculated from a file https://github.com/node-gradle/gradle-node-plugin/blob/8d4e9910274a1b26dcdb761d2892b73efef54826/src/main/kotlin/com/github/gradle/node/NodeExtension.kt#L43

ppfeiler commented 1 year ago

Hi @deepy . Any update for this topic? Is there anything where I can help?

deepy commented 1 year ago

This is a little tricky to implement right now since it lets you use versions like 16 and if you've got 16.13.1 installed locally you probably don't want it to download 16.19.0 Although this is likely an acceptable tradeoff for getting it implemented sooner rather than later.

I've started hacking on something in a branch to add something similar to Gradle's Toolchains for JVM projects which would simplify this a lot since detecting locally installed versions is a big part of it. And also it'd be a pretty cool feature and being excited about developing things is great. Unfortunately this is a larger piece of work than most items in this plugin and it requires way more design than most fixes so chipping away at it slowly hasn't gone great.

If you're good at designing APIs I'd greatly appreciate help here as I've had limited time to spend lately. My initial goal of adding this as an option and letting it exist side-by-side with current functionality also brought me a big plate of spaghetti but every now and then I rebase and force-push this branch, it's going to get a lot cleaner when I release version 4 and drop Gradle 5 support though

ppfeiler commented 1 year ago

This is a little tricky to implement right now since it lets you use versions like 16 and if you've got 16.13.1 installed locally you probably don't want it to download 16.19.0 Although this is likely an acceptable tradeoff for getting it implemented sooner rather than later.

Good point. Really good point. I thought that nvm always uses the latest 16 version of npm.

So IMHO nvm and the gradle-node-plugin should behave the same, as this could lead to unexpected build results.

So I would suggest, that the plugin uses the version from the .nvmrc file if its an exact version as an first draft. AS soon as you drop Gradle 5 support, we could rethink this and maybe also allow 16 as an valid version.

wickkidd commented 1 year ago

This is a little tricky to implement right now since it lets you use versions like 16 and if you've got 16.13.1 installed locally you probably don't want it to download 16.19.0

I don't think that should be an issue. If someone simply puts 18 for .node-version then they are already implying that they are fine with the minor/patch being different between instances. They have the ability to specify the exact version in the file.