View Javadoc
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  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.List;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugins.annotations.Parameter;
29  
30  import io.wcm.maven.plugins.nodejs.installation.NodeInstallationInformation;
31  
32  /**
33   * Wrapper around the execution of a nodejs module.
34   */
35  public class NodeJsTask extends Task {
36  
37    /**
38     * Name of the nodejs module
39     */
40    @Parameter
41    private String moduleName;
42  
43    /**
44     * Name of the module executable
45     */
46    @Parameter
47    private String executableName;
48  
49    /**
50     * Task arguments
51     */
52    @Parameter
53    private String[] arguments;
54  
55    @Override
56    protected List<String> getCommand(NodeInstallationInformation information) throws MojoExecutionException {
57      List<String> commands = new ArrayList<>();
58      commands.add(information.getNodeExecutable().getAbsolutePath());
59      setNodeModule(commands, information);
60      if (arguments != null) {
61        commands.addAll(Arrays.asList(arguments));
62      }
63  
64      return commands;
65    }
66  
67    private void setNodeModule(List<String> commands, NodeInstallationInformation information) throws MojoExecutionException {
68      String modulePath = installModule(information);
69      String moduleExecutable = getModuleExecutable(modulePath);
70      commands.add(moduleExecutable);
71    }
72  
73    private String installModule(NodeInstallationInformation information) throws MojoExecutionException {
74      String modulePath = "";
75      String localInstallationPath = workingDirectory.getAbsolutePath() + File.separator + "node_modules" + File.separator + moduleName;
76      File localInstallation = new File(localInstallationPath);
77  
78      if (!localInstallation.exists()) {
79        String globalInstallationPath = information.getNodeModulesRootPath() + File.separator + "node_modules" + File.separator + moduleName;
80        File moduleInstallation = new File(globalInstallationPath);
81        if (!moduleInstallation.exists()) {
82          NpmInstallTask installTask = new NpmInstallTask();
83          installTask.setLog(getLog());
84          installTask.setArguments(new String[] {
85              "--prefix", information.getNpmPrefixPath(), moduleName
86          });
87          installTask.execute(information);
88        }
89  
90        modulePath = globalInstallationPath;
91      }
92      else {
93        modulePath = localInstallationPath;
94      }
95  
96      return modulePath;
97    }
98  
99    private String getModuleExecutable(String modulePath) {
100     String executable = executableName == null ? moduleName : executableName;
101     return modulePath + File.separator + "bin" + File.separator + executable;
102   }
103 
104   @Override
105   protected boolean isWorkingDirectoryMandatory() {
106     return true;
107   }
108 
109 }