NodeJsTask.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2014 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.maven.plugins.nodejs.mojo;

  21. import java.io.File;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.List;

  25. import org.apache.maven.plugin.MojoExecutionException;
  26. import org.apache.maven.plugins.annotations.Parameter;

  27. import io.wcm.maven.plugins.nodejs.installation.NodeInstallationInformation;

  28. /**
  29.  * Wrapper around the execution of a nodejs module.
  30.  */
  31. public class NodeJsTask extends Task {

  32.   /**
  33.    * Name of the nodejs module
  34.    */
  35.   @Parameter
  36.   private String moduleName;

  37.   /**
  38.    * Name of the module executable
  39.    */
  40.   @Parameter
  41.   private String executableName;

  42.   /**
  43.    * Task arguments
  44.    */
  45.   @Parameter
  46.   private String[] arguments;

  47.   @Override
  48.   protected List<String> getCommand(NodeInstallationInformation information) throws MojoExecutionException {
  49.     List<String> commands = new ArrayList<>();
  50.     commands.add(information.getNodeExecutable().getAbsolutePath());
  51.     setNodeModule(commands, information);
  52.     if (arguments != null) {
  53.       commands.addAll(Arrays.asList(arguments));
  54.     }

  55.     return commands;
  56.   }

  57.   private void setNodeModule(List<String> commands, NodeInstallationInformation information) throws MojoExecutionException {
  58.     String modulePath = installModule(information);
  59.     String moduleExecutable = getModuleExecutable(modulePath);
  60.     commands.add(moduleExecutable);
  61.   }

  62.   private String installModule(NodeInstallationInformation information) throws MojoExecutionException {
  63.     String modulePath = "";
  64.     String localInstallationPath = workingDirectory.getAbsolutePath() + File.separator + "node_modules" + File.separator + moduleName;
  65.     File localInstallation = new File(localInstallationPath);

  66.     if (!localInstallation.exists()) {
  67.       String globalInstallationPath = information.getNodeModulesRootPath() + File.separator + "node_modules" + File.separator + moduleName;
  68.       File moduleInstallation = new File(globalInstallationPath);
  69.       if (!moduleInstallation.exists()) {
  70.         NpmInstallTask installTask = new NpmInstallTask();
  71.         installTask.setLog(getLog());
  72.         installTask.setArguments(new String[] {
  73.             "--prefix", information.getNpmPrefixPath(), moduleName
  74.         });
  75.         installTask.execute(information);
  76.       }

  77.       modulePath = globalInstallationPath;
  78.     }
  79.     else {
  80.       modulePath = localInstallationPath;
  81.     }

  82.     return modulePath;
  83.   }

  84.   private String getModuleExecutable(String modulePath) {
  85.     String executable = executableName == null ? moduleName : executableName;
  86.     return modulePath + File.separator + "bin" + File.separator + executable;
  87.   }

  88.   @Override
  89.   protected boolean isWorkingDirectoryMandatory() {
  90.     return true;
  91.   }

  92. }