1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
34
35 public class NodeJsTask extends Task {
36
37
38
39
40 @Parameter
41 private String moduleName;
42
43
44
45
46 @Parameter
47 private String executableName;
48
49
50
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 }