Class NpmExecutor

java.lang.Object
org.jdrupes.builder.core.AbstractProvider
org.jdrupes.builder.ext.nodejs.NpmExecutor
All Implemented Interfaces:
Renamable, ResourceProvider

public class NpmExecutor extends AbstractProvider implements Renamable

A provider for execution results from invoking npm. The provider generates resources in response to requests for ExecResult where the request's ResourceRequest.name() matches this provider's name.

  • The provider first checks if a file package.json exists, else it fails. If no directory node_modules exists or package.json is newer than node_modules/.package-lock.json it invokes npm init.

  • Then, the provider retrieves all resources added by required(Stream). While the provider itself does not process these resources, it is assumed that they are processed by the npm command and therefore need to be available.

  • If no arguments were specified, the provider returns an ExecResult that indicates successful invocation. The date of the result is set to the date of node_modules/.package-lock.json.

  • The provider invokes the function configured with generated(Function) and collects all resources. If the generated resources exist and no resource from required is newer then the generated resources found, the provider returns a result that indicates successful invocation. The date of the result is set to the newest date from the generated resources and the (existing) resources are attached.

  • Else, the provider invokes npm, calls the function set with provided again and adds the result to the ExecResult that it returns.

The provider also uses the function set with generated(Function) to determine the resources to be removed when it is invoked with a request for Cleanliness.

The generated resources can also be provided directly in response to a request, see provideResources(ResourceRequest).

This provider is made available as an extension. org.jdrupes:jdbld-ext-nodejs:

  • Constructor Details

    • NpmExecutor

      public NpmExecutor(Project project)
      Initializes a new NPM executor.
      Parameters:
      project - the project
  • Method Details

    • project

      public Project project()
      Returns the project that this provider belongs to.
      Returns:
      the project
    • name

      public NpmExecutor name(String name)
      Name.
      Specified by:
      name in interface Renamable
      Parameters:
      name - the name
      Returns:
      the npm executor
    • nodeJsVersion

      public NpmExecutor nodeJsVersion(String version)
      Sets the node.js version to use. Setting a version is mandatory.
      Parameters:
      version - the version
      Returns:
      the npm executor
    • args

      public NpmExecutor args(String... args)
      Add the given arguments.
      Parameters:
      args - the arguments
      Returns:
      the npm executor
    • required

      public NpmExecutor required(Stream<Resource> resources)
      Add the given Stream of resources to the required resources.
      Parameters:
      resources - the resources
      Returns:
      the npm executor
    • required

      public NpmExecutor required(Path root, String pattern)
      Convenience method to add a FileTree to the required resources. If root is a relative path, it is resolved against the project's directory.
      Parameters:
      root - the root
      pattern - the pattern
      Returns:
      the npm executor
    • required

      public NpmExecutor required(Path root)
      Convenience method to add a FileResource to the required resources. If path is relative, it is resolved against the project's directory.
      Parameters:
      root - the root
      Returns:
      the npm executor
    • generated

      Sets the function used to determine the resources generated by this provider.
      Parameters:
      resources - the resources
      Returns:
      the npm executor
    • provideResources

      Provide the generated resources in response to a request like the given one.
      Parameters:
      proto - defines the kind of request that the npm executor should respond to with the generated resources
      Returns:
      the npm executor
    • doProvide

      protected <T extends Resource> Stream<T> doProvide(ResourceRequest<T> request)
      Description copied from class: AbstractProvider
      Invoked by ResourceProviderSpi.provide(ResourceRequest) after checking if the invocation is allowed.
      Specified by:
      doProvide in class AbstractProvider
      Type Parameters:
      T - the generic type
      Parameters:
      request - the request for resources
      Returns:
      the stream