Skip to content

Support gradle-node-plugin for prettier #728

Description

@breucode

It would be great, if spotless supported https://github.com/node-gradle/gradle-node-plugin to download npm for prettier.
This would make it optional to install npm.

Activity

  1. dweiss commented on Dec 16, 2020

    @dweiss
    Contributor

    I second this wholeheartedly. It really helps to keep things consistent because you can reuse the same npm for multiple needs.

    A workaround would be to allow a lazy setup (at execution time) of npm binary. Then the wiring to gradle-node-plugin can be done manually. I've tried a custom npm-setup task and made spotlessInternalRegisterDependencies depend on it but I have no idea how configuration of spotless steps could be altered at execution time (I don't think I can set the npm path then, can I?).

  2. nedtwigg commented on Dec 16, 2020

    @nedtwigg
    Member

    I don't think I can set the npm path then, can I?

    You can set the npm path (docs, code). I think it has to be set at configuration-time, not execution time.

    Happy to take a PR to improve this situation.

  3. dweiss commented on Dec 17, 2020

    @dweiss
    Contributor

    Sorry, I wasn't clear - I know npm path can be set at configuration time. I don't think it can be set afterward, at execution time. This is the difference that would make it possible to run with gradle-node-plugin because then you wire up a dependency between spotless and gradle-node-plugin's npminstall, add a doFirst clause to spotless task and set up the npm path pointing at the right location.

    My experiment shows this is currently impossible because npm path resolution takes place eagerly at configuration time.

  4. nedtwigg commented on Dec 17, 2020

    @nedtwigg
    Member

    doFirst runs after @TaskAction, which is surprising. It's only "first" compared to doLast actions, unfortunately. But agreed, there would be more flexibility if you could set the exe at execution time, and I'm happy to merge a PR that does that.

    FYI, I use this tiny little buildsrc plugin which lazily installs node at configuration time, to workaround this and other similar issues.

  5. dweiss commented on Dec 17, 2020

    @dweiss
    Contributor

    Really? Well, even if then you could just add a task dependency in between to set the exe path - you get the gist. I didn't think about using buildsrc to set up node beforehand, good tip. May look into this, eventually. I'd love to provide a patch but I'm swamped in work already, eh.

  6. rnorth commented on Aug 10, 2021

    @rnorth

    Thank you for the great plugin - it's really useful!

    Just to share, we're facing this same challenge and have a working setup - but some minor caveats.

    This works for us:

    plugins {
        ...
        id 'com.diffplug.spotless' version '5.14.2'
        id "com.github.node-gradle.node" version "2.2.4"
    }
    
    spotless {
        java {
            prettier(...)
                    .npmExecutable("$node.variant.npmExec")
                    .config(...)
        }
    }
    
    spotlessInternalRegisterDependencies.dependsOn(nodeSetup)
    

    The caveats are:

    • spotlessInternalRegisterDependencies.dependsOn(nodeSetup) seems to be the only reliable way to make all potential spotless invocations work. The Internal in the name makes this uncomfortable, so I'd be keen to know if there's a better way to specify this task dependency.
    • (Not relevant to spotless, but maybe of interest) node-gradle recently removed the property which exposes the path to npm, so we're locked to a specific version of that plugin at present.
  7. nedtwigg commented on Jan 26, 2023

    @nedtwigg
    Member

    Thanks to a series of great PRs from @simschla, this is now possible in plugin-gradle 6.14.0. See this or this example.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions